代码之家  ›  专栏  ›  技术社区  ›  anno

使用CB_SETCURSEL时如何发送CBN_SELCHANGE消息?

  •  3
  • anno  · 技术社区  · 15 年前

    当使用 CB_SETCURSEL 改变 消息未被发送。

    如何通知控件选择已更改?

    我在地图上找到的 Sexchange 网站,一个非常好的网站 丑陋的 乱劈:

    SendMessage( hwnd, 0x014F/*CB_SHOWDROPDOWN*/, 1, 0 );
    SendMessage( hwnd, 0x014E/*CB_SETCURSEL*/, ItemIndex, 0 );
    SendMessage( hwnd, 0x0201/*WM_LBUTTONDOWN*/, 0, -1 );
    SendMessage( hwnd, 0x0202/*WM_LBUTTONUP*/, 0, -1 );
    

    现在就可以了。。。不是真的。

    为了解决我的问题,我将在评论中遵循肯的建议。

    3 回复  |  直到 15 年前
        1
  •  3
  •   Ken White    15 年前

    你没有指明你在用什么语言;如果你这样做的话,它会让你更容易找到解决办法。

    // Send the CB_SETCURSEL message to the combobox
    PostMessage(ComboBox1.Handle, CB_SETCURSEL, Whatever, WhateverElse);
    
    // Directly call the OnChange() handler, which is the equivalent to CBN_SELCHANGE
    ComboBox1Change(nil);
    
        2
  •  5
  •   Jason Plank Maksim Kondratyuk    13 年前

    这可能会帮助下一个人:

    [DllImport("User32.dll", EntryPoint = "SendMessage")]
    private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
    
    [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
    private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
    
    [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
    private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
    
    public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
    {
       if (IntPtr.Size == 8)
            return GetWindowLongPtr64(hWnd, nIndex);
       else
            return GetWindowLongPtr32(hWnd, nIndex);
    }
    
    static int MakeWParam(int loWord, int hiWord)
    {
         return (loWord & 0xFFFF) + ((hiWord & 0xFFFF) << 16);
    }
    
    public const int CB_SETCURSEL = 0x014E;
    public const int CBN_SELCHANGE = 0x0001;
    
    public enum GWL
    {
                GWL_WNDPROC = (-4),
                GWL_HINSTANCE = (-6),
                GWL_HWNDPARENT = (-8),
                GWL_STYLE = (-16),
                GWL_EXSTYLE = (-20),
                GWL_USERDATA = (-21),
                GWL_ID = (-12)
    }
    
    public static IntPtr Hwnd_select_control_parent = IntPtr.Zero;
    public static IntPtr Hwnd_select_control = IntPtr.Zero;
    
    static void changeit()
    
    {
    
    // Google WinSpy for tips on how to figure out how to get window handles from known ctrl_id
    
       Hwnd_select_control = 14298; // or whatever the handle of the combo box is
    
    // Get the parent of the selectbox control
    
       Hwnd_select_control_parent = GetWindowLongPtr(service_window_control, (int)GWL.GWL_HWNDPARENT);
    
    // Get the control id of the selectbox if you don't already have it
    
       IntPtr nID = GetWindowLongPtr(Hwnd_select_control, (int)GWL.GWL_ID);
       int ctrl_id = nID.ToInt32();
    
    // Change the combo box to the value "My Value"
    
       SendMessage(Hwnd_select_control, CB_SETCURSEL, "My Value", null);
    
    // low ID is the ctrl_id of the combo box, high id is CBN_SELCHANGE
    
       int send_cbn_selchange = MakeWParam(ctrl_id, CBN_SELCHANGE); 
    
    // Send the WM_COMMAND to the parent, not the control itself
    
       SendMessage(Hwnd_serviceselect_control_parent, 0x111 /* WM_COMMAND */, send_cbn_selchange, Hwnd_serviceselect_control.ToInt32()); 
    
    
    }
    
        3
  •  0
  •   Ernie    7 年前

    我刚刚发现将这些SendMessages调用到组合框两次是有效的。。。我知道这并不完美,但对我来说很有效。(用VB6编写)

        For looper = 1 To 2
    
            bVal = SendMessage(inHandle, COMBO.CB_SHOWDROPDOWN, True, 0)
            count = SendMessage(inHandle, COMBO.CB_SETCURSEL, 1, 0)
            count = SendMessage(inHandle, WIND.WM_LBUTTONDOWN, 0, -1)
    
        Next