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(24, 82, 93));
            break;
        case WM_LBUTTONDOWN:
            {
                UINT cx=LOWORD(lParam);
                UINT cy=HIWORD(lParam);
                if(cx>=10 && cx<=100){
                        if(cy>=10 && cy<=50){
                            DrawButton(hwnd, RGB(24, 82, 93));b=false;
                            break;
                        }
                }
            }
            break;
        case WM_LBUTTONUP:
            {
                UINT cx=LOWORD(lParam);
                UINT cy=HIWORD(lParam);
                if(cx>=10 && cx<=100){
                        if(cy>=10 && cy<=50){
                            DrawButton(hwnd, RGB(200, 82, 93));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>=10 && cx<=100){
                    if(cy>=10 && cy<=50){
                        if(!b){DrawButton(hwnd, RGB(200, 82, 93));b=true;}
                        break;
                    }
                }
                if(b){DrawButton(hwnd, RGB(24, 82, 93));b=false;}
            }
            break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* 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 0;
}

DrawButton的部分:

void DrawPng(HWND hwnd)
{
    PAINTSTRUCT ps;
    BeginPaint(hwnd, &ps);
    Gdiplus::GpGraphics *graphics;
    Gdiplus::GpImage *image;
    WCHAR file[256]={L"D:\alert.png"};
    Gdiplus::DllExports::GdipCreateFromHDC(GetDC(hwnd), &graphics);
    Gdiplus::DllExports::GdipLoadImageFromFile(file, &image);
    Gdiplus::DllExports::GdipDrawImageRect(graphics, image, 66, 12, 32, 32);
    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[32]=_T("Couriew New");
    RECT rect={10,10,100,50};
    BeginPaint(hwnd, &ps);
    hdc=GetDC(hwnd);
    hrgn=CreateRoundRectRgn(10, 10, 100, 50, 10, 10);
    hbrush=CreateSolidBrush(rgb);//RGB(24, 82, 93)
    FillRgn(hdc, hrgn, hbrush);
    FrameRgn(hdc, hrgn, (HBRUSH)GetStockObject(GRAY_BRUSH), 1, 1);
    SetBkMode(hdc, TRANSPARENT);
    SetTextColor(hdc, RGB(255, 255, 255));
    of=(HFONT)SendMessage(hwnd, WM_GETFONT, (WPARAM)NULL, (LPARAM)NULL);
    ZeroMemory(&lf, sizeof(lf));
    GetObject(of, sizeof(lf), &lf);
    MoveMemory(&lf.lfFaceName, fn, 32);
    nf=(HFONT)CreateFontIndirect(&lf);
    SelectObject(hdc, nf);
    DrawText(hdc, "    Button", -1, &rect, DT_SINGLELINE|DT_LEFT|DT_VCENTER);
    SelectObject(hdc, of);
    DeleteObject(nf);
    DeleteObject(hbrush);
    DeleteObject(hrgn);
    ReleaseDC(hwnd, hdc);
    EndPaint(hwnd, &ps);
    DrawPng(hwnd);
}

贴张图:

                                 

原文地址:https://www.cnblogs.com/lichmama/p/3873148.html