3

最初的目标是在编辑控件中放置一个 X 按钮(后来证明这个编辑控件本身必须是组合框的一部分。)有人建议我使用这篇文章作为指导。

为了让它看起来更“漂亮”,我决定为那个 X 按钮使用一个工具栏。所以我想出了以下代码。下面是控件在 ComboBox 中的初始化方式:

//Initialization
#define EDIT_X_TOOLBAR_BTN_W 10
#define EDIT_X_TOOLBAR_BTN_H 11

//Find edit control handle using GetComboBoxInfo() API
COMBOBOXINFO cbi2 = {0};
cbi2.cbSize = sizeof(cbi2);
GetComboBoxInfo(hComboWnd, &cbi2);
HWND hEditCtrl = cbi2.hwndItem;

HINSTANCE hInst = ::GetModuleHandle(NULL);

//Create toolbar as a child of the edit control
hWndToolbar = ::CreateWindowEx(0, TOOLBARCLASSNAME, NULL, 
    WS_CHILD | WS_VISIBLE | 
    CCS_NODIVIDER | CCS_NOPARENTALIGN | 
    CCS_NORESIZE | CCS_NOMOVEY | TBSTYLE_FLAT | TBSTYLE_TRANSPARENT,
    0, 0, 0, 0, 
    hEditCtrl,
    (HMENU)ID_CMD_EDIT_X_TOOLBAR, hInst, 0);

::SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
::SendMessage(hWndToolbar, TB_SETBITMAPSIZE, 0, MAKELONG(EDIT_X_TOOLBAR_BTN_W, EDIT_X_TOOLBAR_BTN_H));
::SendMessage(hWndToolbar, TB_SETBUTTONSIZE, 0, MAKELONG(EDIT_X_TOOLBAR_BTN_W, EDIT_X_TOOLBAR_BTN_H));

//Load bitmap
TBADDBITMAP tbab = {0};
tbab.hInst = hInst;
tbab.nID = IDB_BITMAP_TOOLBAR_X;
VERIFY(::SendMessage(hWndToolbar, TB_ADDBITMAP, 1, (LPARAM)&tbab) != -1);

TBBUTTON tbbs[1] = {0};
tbbs[0].iBitmap = 0;
tbbs[0].idCommand = CMD_EDIT_X_TOOLBAR_CLOSE;
tbbs[0].fsState = TBSTATE_ENABLED;
tbbs[0].fsStyle = TBSTYLE_BUTTON;
tbbs[0].iString = -1;

VERIFY(::SendMessage(hWndToolbar, TB_ADDBUTTONS, SIZEOF(tbbs), (LPARAM)tbbs));

//Subclass edit control
oldProc = (WNDPROC)::SetWindowLong(hEditCtrl, GWL_WNDPROC, (LONG)wndProcEditCtrl);

//And redraw the edit control
::SetWindowPos(hEditCtrl, NULL, 0, 0, 0, 0, 
    SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);

这是编辑控件的子类窗口过程:

CRect rcToolbar;

LRESULT CALLBACK wndProcEditCtrl(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_NCCALCSIZE:
        {
            if(wParam)
            {
                //First let edit box process it
                ::CallWindowProc(oldProc, hWnd, msg, wParam, lParam);

                RECT* pRc = (RECT*)lParam;

                //Define toolbar size & move it
                int nTB_w = pRc->bottom - pRc->top;
                int nTB_h = nTB_w;

                //Adjust the edit's client area
                pRc->right -= nTB_w;

                //Set toolbar rect
                rcToolbar.SetRect(pRc->right, pRc->top, pRc->right + nTB_w, pRc->top + nTB_h);

                //And move the toolbar
                ::MoveWindow(hWndToolbar, pRc->right, pRc->top, nTB_w, nTB_h, TRUE);

                return 0;
            }
        }
        break;

    case WM_NCHITTEST:
        {
            POINT pnt;
            pnt.x = GET_X_LPARAM(lParam);
            pnt.y = GET_Y_LPARAM(lParam);

            if(::ScreenToClient(hWnd, &pnt))
            {
                if(rcToolbar.PtInRect(pnt))
                {
                    return HTBORDER;
                }
            }
        }
        break;
    }

    return ::CallWindowProc(oldProc, hWnd, msg, wParam, lParam);
}

问题是我可以看到工具栏已创建(通过 Spy++),但它仍然处于非活动状态,并且我没有看到我的按钮(我得到的只是右侧的灰色矩形):

在此处输入图像描述

为了确保我可以自己创建工具栏,如果我将其父窗口从编辑控件(即hEditCtrl)更改为主对话框窗口,它会显示(在错误的位置)并且可以很好地响应点击。所以我的结论是我必须在我的子类中阻止一些消息。问题是哪些?

知道我在这里想念什么吗?

4

0 回答 0