.NET CF设置下拉框的“DroppedDown”属性

4
3个回答

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;
    }

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

我应该把这段代码放在哪里才能生效?我已经将这段代码放在MyForm_Load事件中combobox.SelectIndex = 0; combobox.Focus();之后,但是这段代码没有任何效果。 - hellboy
以上代码片段是必需的修改,以使引用的文章(问题中的那篇)执行所需的行为。您还需要查看那篇文章。 - tcarvin

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接