zoukankan      html  css  js  c++  java
  • 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);
    }

    贴张图:

                                     

  • 相关阅读:
    Python学习笔记之操作yalm
    Python学习笔记之多线程
    Python学习笔记之网络编程
    Python学习笔记之面对象与错误处理
    Python学习笔记之装饰器
    Python学习笔记之内置模块
    Python学习笔记之函数与正则
    PAT甲级1049. Counting Ones
    PAT甲级1045. Favorite Color Stripe
    PAT甲级1034. Head of a Gang
  • 原文地址:https://www.cnblogs.com/lichmama/p/3873148.html
Copyright © 2011-2022 走看看