2013-03-15 43 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; 
}