ExquisiteWnd

Posted 卡尔特人头狼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExquisiteWnd相关的知识,希望对你有一定的参考价值。

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///ExquisiteWnd.h

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#include <Windows.h>
#include <tchar.h>
#include <string>
#include <vector>

#include <stdio.h>
#include <stdlib.h>
using namespace std;

#include "GDIEncapsulation.h"


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
class CExquisiteWnd
{
public:
CExquisiteWnd(HINSTANCE hInstance, HWND a_hParentWnd, RECT a_rect);
~CExquisiteWnd();

static CExquisiteWnd * FindByWnd(HWND hWnd);

HWND GetWnd();

void SetRect(RECT a_rect);
RECT GetRect();

void SetVisible(bool a_bIfVisible);

void Draw(HDC a_hdc);

private:
HINSTANCE m_hInstance;
HWND m_hWnd;
HWND m_hParentWnd;
};

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///ExquisiteWnd.cpp

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "ExquisiteWnd.h"

TCHAR szWindowClass[] = _T("ExquisiteWndClass");
vector<CExquisiteWnd*> g_vecExquisition;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);

RECT rect;
HDC hdcMem;
HBITMAP hBmpMem;
GetWindowRect(hWnd, &rect);
int cx = (int)(rect.right - rect.left);
int cy = (int)(rect.bottom - rect.top);
hdcMem = CreateCompatibleDC(hdc);
hBmpMem = CreateCompatibleBitmap(hdc, cx, cy);
SelectObject(hdcMem, hBmpMem);
BitBlt(hdcMem, 0, 0, cx, cy,hdc, 0, 0,SRCCOPY);

CExquisiteWnd * pWnd = CExquisiteWnd::FindByWnd(hWnd);
if (pWnd != NULL)
{
pWnd->Draw(hdcMem);
}

BitBlt(hdc, 0, 0, cx, cy, hdcMem, 0, 0, SRCCOPY);
DeleteDC(hdcMem);
DeleteObject(hBmpMem);

EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}

return 0;
}

CExquisiteWnd::CExquisiteWnd(HINSTANCE hInstance, HWND a_hParentWnd, RECT a_rect)
{
// RegisterClassEx
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;// LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("ExquisiteWnd"),
NULL);
return;
}

m_hInstance = hInstance;

//CreateWindow
DWORD wndStyle;
if (a_hParentWnd)
{
wndStyle = (WS_CHILD | WS_VISIBLE);
}
else
{
wndStyle = (WS_POPUP | WS_VISIBLE);
}
m_hWnd = CreateWindowEx(
WS_EX_TRANSPARENT,
szWindowClass,
NULL,
wndStyle,
(int)a_rect.left, (int)a_rect.top,
(int)(a_rect.right - a_rect.left), (int)(a_rect.bottom - a_rect.top),
NULL,
NULL,
hInstance,
NULL
);

if (!m_hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("ExquisiteWnd"),
NULL);

return;
}
RegisterTouchWindow(m_hWnd, TWF_FINETOUCH);
m_hParentWnd = a_hParentWnd;

g_vecExquisition.push_back(this);
}

CExquisiteWnd::~CExquisiteWnd()
{
if (NULL != m_hWnd)
{
UnregisterTouchWindow(m_hWnd);
DestroyWindow(m_hWnd);
}
vector<CExquisiteWnd*>::iterator iter;
for (iter = g_vecExquisition.begin(); iter != g_vecExquisition.end(); iter++)
{
if (this == (*iter))
{
g_vecExquisition.erase(iter);
break;
}
}
}

HWND CExquisiteWnd::GetWnd()
{
return m_hWnd;
}

CExquisiteWnd * CExquisiteWnd::FindByWnd(HWND hWnd)
{
if (NULL == hWnd) return NULL;
for (int i = 0; i < g_vecExquisition.size(); i++)
{
if (g_vecExquisition[i]->GetWnd() == hWnd)
return g_vecExquisition[i];
}
return NULL;
}

void CExquisiteWnd::SetRect(RECT a_rect)
{
MoveWindow(m_hWnd, (int)a_rect.left, (int)a_rect.top, (int)(a_rect.right - a_rect.left), (int)(a_rect.bottom - a_rect.top), TRUE);
}
RECT CExquisiteWnd::GetRect()
{
RECT rWnd;
GetWindowRect(m_hWnd, &rWnd);
return rWnd;
}

void CExquisiteWnd::SetVisible(bool a_bIfVisible)
{
if (a_bIfVisible)
{
ShowWindow(m_hWnd, SW_SHOW);
}
else
{
ShowWindow(m_hWnd, SW_HIDE);
}
}

void CExquisiteWnd::Draw(HDC a_hdc)
{
if (NULL == m_hWnd) return;

RECT rect;
GetWindowRect(m_hWnd, &rect);
int cx = (int)(rect.right - rect.left);
int cy = (int)(rect.bottom - rect.top);
COLORREF colorPen = RGB(0,153,51);
COLORREF colorBrush = RGB(0,153,255);

CGDIEncapsulation::GetInstance()->DrawLinearGradientRectangle(a_hdc, 0, 0, cx, cy, colorPen, colorBrush, 200, 2);
//CGDIEncapsulation::GetInstance()->FillRoundRectangle(a_hdc, 50, 50, cx - 100, cy - 100, RGB(0, 0, 0));
CGDIEncapsulation::GetInstance()->DrawRoundRectangle(a_hdc, 50, 50, cx - 100, cy - 100, RGB(255, 153, 0));

RECT rImg;
rImg.left = 100;
rImg.top = 100;
rImg.right = 100 + 84;
rImg.bottom = 100 + 160;
//CGDIEncapsulation::GetInstance()->DrawImage(a_hdc, _T("Probe_sel.png"), rImg);

CGDIEncapsulation::GetInstance()->ShowText(a_hdc, _T("人生得意须尽欢,莫使金樽空对月!"),
rImg, RGB(153, 255, 0), 22, 4, 1, 1, L"Arial", 0);

}

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///Main.cpp

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "ExquisiteCtrls/ExquisiteWnd.h"

/* *******************************
* WinMain
* Entry Function
* *******************************/
int WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
RECT rMainWnd;
rMainWnd.left = 100;
rMainWnd.top = 60;
rMainWnd.right = (LONG)(100 + 567);
rMainWnd.bottom = (LONG)(60 + 380);
CExquisiteWnd mainWnd(hInstance,NULL, rMainWnd);
HWND hMainWnd = mainWnd.GetWnd();

//ShowWindow
ShowWindow(hMainWnd, nShowCmd);
UpdateWindow(hMainWnd);

//Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}

以上是关于ExquisiteWnd的主要内容,如果未能解决你的问题,请参考以下文章