2013-03-15 84 views

回答

4

您可以采用与参考文章中相同的方法并发送消息。

而不是使用const int CB_SHOWDROPDOWN = 0x14F为您的消息。

从该参考样本,修改了一下:

Message.Create(comboBox.Handle, CB_SHOWDROPDOWN , (IntPtr)1, IntPtr.Zero); // to open 

Message.Create(comboBox.Handle, CB_SHOWDROPDOWN , (IntPtr)0, IntPtr.Zero); // to close 
+0

刚编辑以添加所需投 – tcarvin 2013-03-15 15:57:16

+0

我应该在哪里放置这段代码才能生效?我在combobox.SelectIndex = 0后将此代码放入MyForm_Load事件中; combobox.Focus();而这段代码没有任何作用。 – hellboy 2013-09-24 09:10:55

+0

上面的代码片段是使被引用文章(问题中的文章)执行所需行为所需的修改。你也需要检查一下。 – tcarvin 2013-09-24 12:42:27

7

使用CB_SHOWDROPDOWN = 0x014F消息:

public const int CB_GETDROPPEDSTATE = 0x0157; 
    public static bool GetDroppedDown(ComboBox comboBox) 
    { 
     Message comboBoxDroppedMsg = Message.Create(comboBox.Handle, CB_GETDROPPEDSTATE, IntPtr.Zero, IntPtr.Zero); 
     MessageWindow.SendMessage(ref comboBoxDroppedMsg); 
     return comboBoxDroppedMsg.Result != IntPtr.Zero; 
    } 

    public const int CB_SHOWDROPDOWN = 0x014F; 
    public static bool ToogleDropDown(ComboBox comboBox) 
    { 
     int expand = GetDroppedDown(comboBox) ? 0 : 1; 
     int size = Marshal.SizeOf(new Int32()); 
     IntPtr pBool = Marshal.AllocHGlobal(size); 
     Marshal.WriteInt32(pBool, 0, expand); // last parameter 0 (FALSE), 1 (TRUE) 
     Message comboBoxDroppedMsg = Message.Create(comboBox.Handle, CB_SHOWDROPDOWN, pBool, IntPtr.Zero); 
     MessageWindow.SendMessage(ref comboBoxDroppedMsg); 
     Marshal.FreeHGlobal(pBool); 
     return comboBoxDroppedMsg.Result != IntPtr.Zero; 
    } 
+0

它的工作原理!谢谢。 – hellboy 2013-09-24 10:16:44

0

一点更加改进:

public bool ToogleDropDown() 
{ 
    int expand = GetDroppedDown() ? 0 : 1; 
    //int size = Marshal.SizeOf(new Int32()); 
    //IntPtr pBool = Marshal.AllocHGlobal(size); 
    //Marshal.WriteInt32(pBool, 0, expand); // last parameter 0 (FALSE), 1 (TRUE) 
    Message comboBoxDroppedMsg = Message.Create(this.Handle, CB_SHOWDROPDOWN, (IntPtr)expand, IntPtr.Zero); 
    MessageWindow.SendMessage(ref comboBoxDroppedMsg); 
    //Marshal.FreeHGlobal(pBool); 
    return comboBoxDroppedMsg.Result != IntPtr.Zero; 
}