Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 使下拉式组合框的行为类似于编辑控件_Windows_Winapi_Combobox_User32 - Fatal编程技术网

Windows 使下拉式组合框的行为类似于编辑控件

Windows 使下拉式组合框的行为类似于编辑控件,windows,winapi,combobox,user32,Windows,Winapi,Combobox,User32,我将用下拉式组合框替换标准编辑控件。基本上,这个组合框就像一个编辑控件 到目前为止,一切正常,但只有一个显著的区别: 当您单击已包含一些文本且没有焦点的编辑控件时,光标仅位于您单击的位置 但是,当您单击组合框中已包含一些文本且没有焦点的组合框时,整个文本将被选中 这描述了当您单击组合框或编辑控件时发生的情况,其中红色箭头在两个控件都没有焦点时指向: 有没有办法使组合框的行为类似于编辑控件?防止选中整个文本的一种解决方案是,当鼠标左键第一次单击组合框时,将其子类化,并将焦点设置为编辑控件。代

我将用下拉式组合框替换标准编辑控件。基本上,这个组合框就像一个编辑控件

到目前为止,一切正常,但只有一个显著的区别:

  • 当您单击已包含一些文本且没有焦点的编辑控件时,光标仅位于您单击的位置
  • 但是,当您单击组合框中已包含一些文本且没有焦点的组合框时,整个文本将被选中
这描述了当您单击组合框或编辑控件时发生的情况,其中红色箭头在两个控件都没有焦点时指向:


有没有办法使组合框的行为类似于编辑控件?

防止选中整个文本的一种解决方案是,当鼠标左键第一次单击组合框时,将其子类化,并将焦点设置为编辑控件。代码如下:

子类过程:

LRESULT CALLBACK EditSubClassProc(HWND hWnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam,
    UINT_PTR uIdSubclass,
    DWORD_PTR dwRefData
    )
{
    switch (uMsg)
    {
    case WM_DESTROY:
    {
        RemoveWindowSubclass(hWnd, EditSubClassProc, 0);
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    case WM_LBUTTONDOWN:
    {
        if (GetFocus() != hWnd)
        {
            SetFocus(hWnd);
        }
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    default:
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
}
   //  Get the edit window handle to combo box. 
   HWND comboEditHdl = NULL;
   COMBOBOXINFO info = { 0 };
   info.cbSize = sizeof(COMBOBOXINFO);

   if (!GetComboBoxInfo(hwndCombo1, &info))
       return 0;

   comboEditHdl = info.hwndItem;

   if (comboEditHdl)
   {
       SetWindowSubclass(comboEditHdl, EditSubClassProc, 0, NULL);
   }
找到组合框的编辑控制窗口并安装子类回调:

LRESULT CALLBACK EditSubClassProc(HWND hWnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam,
    UINT_PTR uIdSubclass,
    DWORD_PTR dwRefData
    )
{
    switch (uMsg)
    {
    case WM_DESTROY:
    {
        RemoveWindowSubclass(hWnd, EditSubClassProc, 0);
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    case WM_LBUTTONDOWN:
    {
        if (GetFocus() != hWnd)
        {
            SetFocus(hWnd);
        }
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    default:
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
}
   //  Get the edit window handle to combo box. 
   HWND comboEditHdl = NULL;
   COMBOBOXINFO info = { 0 };
   info.cbSize = sizeof(COMBOBOXINFO);

   if (!GetComboBoxInfo(hwndCombo1, &info))
       return 0;

   comboEditHdl = info.hwndItem;

   if (comboEditHdl)
   {
       SetWindowSubclass(comboEditHdl, EditSubClassProc, 0, NULL);
   }

防止选中整个文本的一个解决方案是,当鼠标左键第一次单击组合框时,将其子类化,并将焦点设置为编辑控件。代码如下:

子类过程:

LRESULT CALLBACK EditSubClassProc(HWND hWnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam,
    UINT_PTR uIdSubclass,
    DWORD_PTR dwRefData
    )
{
    switch (uMsg)
    {
    case WM_DESTROY:
    {
        RemoveWindowSubclass(hWnd, EditSubClassProc, 0);
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    case WM_LBUTTONDOWN:
    {
        if (GetFocus() != hWnd)
        {
            SetFocus(hWnd);
        }
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    default:
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
}
   //  Get the edit window handle to combo box. 
   HWND comboEditHdl = NULL;
   COMBOBOXINFO info = { 0 };
   info.cbSize = sizeof(COMBOBOXINFO);

   if (!GetComboBoxInfo(hwndCombo1, &info))
       return 0;

   comboEditHdl = info.hwndItem;

   if (comboEditHdl)
   {
       SetWindowSubclass(comboEditHdl, EditSubClassProc, 0, NULL);
   }
找到组合框的编辑控制窗口并安装子类回调:

LRESULT CALLBACK EditSubClassProc(HWND hWnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam,
    UINT_PTR uIdSubclass,
    DWORD_PTR dwRefData
    )
{
    switch (uMsg)
    {
    case WM_DESTROY:
    {
        RemoveWindowSubclass(hWnd, EditSubClassProc, 0);
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    case WM_LBUTTONDOWN:
    {
        if (GetFocus() != hWnd)
        {
            SetFocus(hWnd);
        }
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    default:
        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
}
   //  Get the edit window handle to combo box. 
   HWND comboEditHdl = NULL;
   COMBOBOXINFO info = { 0 };
   info.cbSize = sizeof(COMBOBOXINFO);

   if (!GetComboBoxInfo(hwndCombo1, &info))
       return 0;

   comboEditHdl = info.hwndItem;

   if (comboEditHdl)
   {
       SetWindowSubclass(comboEditHdl, EditSubClassProc, 0, NULL);
   }

可能是通过对
WM_GETDLGCODE
返回的内容(如上所述)进行子分类和修改,尽管我不确定是否有组合。也许您必须在组合中对编辑控件进行子类化。@JonathanPotter看起来很有希望,thanks@Jabberwocky删除DLGC_HASSETSEL标志对我来说不适用于Combobox。这对你有用吗?@RitaHan MSFT我还没有试过,因为这对我来说现在不是个大问题。您是如何使用DLGC_HASSETSEL的?您是否对组合框或组合框的编辑控件进行了子类化?@Jabberwocky I对组合框的编辑控件进行了子类化,如下所示:
comboEditHdl=FindWindowEx(cmbHdl,NULL,L“edit”,NULL);SetWindowsSubClass(comboEditHdl,RemoveHasSetSelSubclassProc,0,0)case WM_GETDLGCODE:return defsublassproc(hwnd、uiMsg、wParam、lParam)和~DLGC_HASSETSEL
可能通过对
WM_GETDLGCODE
返回的内容进行子分类和修改(如上所述),尽管我不确定是否有组合。也许您必须在组合中对编辑控件进行子类化。@JonathanPotter看起来很有希望,thanks@Jabberwocky删除DLGC_HASSETSEL标志对我来说不适用于Combobox。这对你有用吗?@RitaHan MSFT我还没有试过,因为这对我来说现在不是个大问题。您是如何使用DLGC_HASSETSEL的?您是否对组合框或组合框的编辑控件进行了子类化?@Jabberwocky I对组合框的编辑控件进行了子类化,如下所示:
comboEditHdl=FindWindowEx(cmbHdl,NULL,L“edit”,NULL);SetWindowsSubClass(comboEditHdl,RemoveHasSetSelSubclassProc,0,0)case WM_GETDLGCODE:return defsublassproc(hwnd、uiMsg、wParam、lParam)和~DLGC_HASSETSEL