如何在编辑控件中放置工具栏?

时间:2015-04-21 06:06:31

标签: c++ winapi mfc gdi common-controls

我的original goal是在编辑控件中放置一个X按钮(后来证明这个编辑控件本身必须是组合框的一部分。)我被建议使用{{3作为指导。

为了让它看起来更“漂亮”,我决定使用工具栏代替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 ++创建的,但它仍处于非活动状态,我没有看到我的按钮(我得到的只是右边的灰色矩形):

enter image description here

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

知道我在这里缺少什么吗?

0 个答案:

没有答案