win32SDK的hello,world程序(二)

时间:2023-03-09 14:54:13
win32SDK的hello,world程序(二)

接上篇,原生的控件都不太好看,所以决定自己画一个,稍微处理下消息就能用了。不过,美化这东西是需要天赋的。即使技术再好,没有对UI布局调整和良好的审美能力,做出来的东西还是很挫。

主要把消息逻辑和画的过程写出来:

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool b;
switch (message) /* handle the messages */
{
case WM_CREATE:
b=false;
break;
case WM_PAINT:
//DrawBackground(hwnd);
DrawButton(hwnd, RGB(, , ));
break;
case WM_LBUTTONDOWN:
{
UINT cx=LOWORD(lParam);
UINT cy=HIWORD(lParam);
if(cx>= && cx<=){
if(cy>= && cy<=){
DrawButton(hwnd, RGB(, , ));b=false;
break;
}
}
}
break;
case WM_LBUTTONUP:
{
UINT cx=LOWORD(lParam);
UINT cy=HIWORD(lParam);
if(cx>= && cx<=){
if(cy>= && cy<=){
DrawButton(hwnd, RGB(, , ));b=true;
MessageBox(hwnd, "CAUTION: YOU JUST CLICKED ONE CUSTOMER-BUTTON", "", MB_ICONINFORMATION);
break;
}
}
}
break;
case WM_MOUSEMOVE:
{
UINT cx=LOWORD(lParam);
UINT cy=HIWORD(lParam);
if(cx>= && cx<=){
if(cy>= && cy<=){
if(!b){DrawButton(hwnd, RGB(, , ));b=true;}
break;
}
}
if(b){DrawButton(hwnd, RGB(, , ));b=false;}
}
break;
case WM_DESTROY:
PostQuitMessage (); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return ;
}

DrawButton的部分:

void DrawPng(HWND hwnd)
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
Gdiplus::GpGraphics *graphics;
Gdiplus::GpImage *image;
WCHAR file[]={L"D:\\alert.png"};
Gdiplus::DllExports::GdipCreateFromHDC(GetDC(hwnd), &graphics);
Gdiplus::DllExports::GdipLoadImageFromFile(file, &image);
Gdiplus::DllExports::GdipDrawImageRect(graphics, image, , , , );
Gdiplus::DllExports::GdipDisposeImage(image);
Gdiplus::DllExports::GdipDeleteGraphics(graphics);
EndPaint(hwnd, &ps);
} void DrawButton(HWND hwnd, COLORREF rgb)
{
PAINTSTRUCT ps;
HDC hdc;
HRGN hrgn;
HBRUSH hbrush;
HFONT of, nf;
LOGFONT lf;
TCHAR fn[]=_T("Couriew New");
RECT rect={,,,};
BeginPaint(hwnd, &ps);
hdc=GetDC(hwnd);
hrgn=CreateRoundRectRgn(, , , , , );
hbrush=CreateSolidBrush(rgb);//RGB(24, 82, 93)
FillRgn(hdc, hrgn, hbrush);
FrameRgn(hdc, hrgn, (HBRUSH)GetStockObject(GRAY_BRUSH), , );
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(, , ));
of=(HFONT)SendMessage(hwnd, WM_GETFONT, (WPARAM)NULL, (LPARAM)NULL);
ZeroMemory(&lf, sizeof(lf));
GetObject(of, sizeof(lf), &lf);
MoveMemory(&lf.lfFaceName, fn, );
nf=(HFONT)CreateFontIndirect(&lf);
SelectObject(hdc, nf);
DrawText(hdc, " Button", -, &rect, DT_SINGLELINE|DT_LEFT|DT_VCENTER);
SelectObject(hdc, of);
DeleteObject(nf);
DeleteObject(hbrush);
DeleteObject(hrgn);
ReleaseDC(hwnd, hdc);
EndPaint(hwnd, &ps);
DrawPng(hwnd);
}

贴张图:

win32SDK的hello,world程序(二)                                 win32SDK的hello,world程序(二)