SetWindowSubclass 设置窗口子类回调

2021-05-07 03:27

阅读:431

标签:class   事件   windows   rtu   背景色   div   ble   visible   not   

今天想给一个 Static 控件添加一个鼠标相关的事件:移入,移出,按下,松开事件

一开始以为在

while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
...
}

这里捕获,发现不行。搞了半天,也没达到效果。

后来无意发现一个API:SetWindowSubclass 

LRESULT CALLBACK MySubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (uMsg)
    {
        case WM_NCDESTROY:
        {
            RemoveWindowSubclass(hWnd, &MySubclassProc, uIdSubclass);
            break;
        }
        case WM_MOUSELEAVE:
        {
            //printf("WM_MOUSELEAVE\n");

            SendMessage(hwnd_lbl_back, STM_SETIMAGE,  IMAGE_BITMAP, (LPARAM) hBitmap_back); //设置图标

            break;
        }
        case WM_MOUSEMOVE:
        {
            //printf("WM_MOUSEMOVE\n");

            //为了捕获 WM_MOUSELEAVE
            TRACKMOUSEEVENT tme = {0};
            tme.cbSize = sizeof(tme);
            tme.hwndTrack = hWnd;
            tme.dwFlags = TME_LEAVE;

            // tme.dwFlags = TME_LEAVE | TME_HOVER;
            // tme.dwHoverTime = 1;

            TrackMouseEvent(&tme);

            //--------------------------

            if(wParam & MK_LBUTTON) //按下
            {
                SendMessage(hwnd_lbl_back, STM_SETIMAGE,  IMAGE_BITMAP, (LPARAM) hBitmap_back_pressed); //设置图标
            }
            else
            {
                SendMessage(hwnd_lbl_back, STM_SETIMAGE,  IMAGE_BITMAP, (LPARAM) hBitmap_back_over); //设置图标
            }

            break;
        }
        case WM_LBUTTONDOWN: //按下
        {
            //printf("WM_LBUTTONDOWN\n");
            SendMessage(hwnd_lbl_back, STM_SETIMAGE,  IMAGE_BITMAP, (LPARAM) hBitmap_back_pressed); //设置图标
            break;
        }
        case WM_LBUTTONUP: //松开
        {
            //printf("WM_LBUTTONUP\n");
            SendMessage(hwnd_lbl_back, STM_SETIMAGE,  IMAGE_BITMAP, (LPARAM) hBitmap_back_over); //设置图标
            break;
        }
    }

    return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}

 

hwnd_lbl_back = CreateWindowEx(0, "static", "", //
                                   WS_CHILD | WS_VISIBLE | SS_NOTIFY | SS_BITMAP,  //SS_GRAYFRAME 使用背景色  | WS_DISABLED
                                   4, // xpos
                                   4, // ypos
                                   28, //width
                                   28, //height
                                   hwnd_frame, (HMENU)id_lbl_back, g_hInstance,  NULL) ;

SetWindowSubclass(hwnd_lbl_back, &MySubclassProc, 0, (DWORD_PTR) REF_BACK); //设置窗口子类回调

问题解决,SetWindowSubclass () 这么好用的API,我还是刚刚知道,第一次使用。

惭愧惭愧

SetWindowSubclass 设置窗口子类回调

标签:class   事件   windows   rtu   背景色   div   ble   visible   not   

原文地址:https://www.cnblogs.com/personnel/p/14726133.html


评论


亲,登录后才可以留言!