我在一个特定的应用场合,需要让ComboBox禁止获焦和禁止弹出下拉,并且需要保留DropDown的样式,不想Enabled=false改变控件外观。
我试过想TextBox那样截取WM_SETFOCUS消息,但在ComboBox中却不行。大家有没有什么好办法呢?

解决方案 »

  1.   

    没遇到过这种情况不知道设置ComboBox的Enabled为falsh后再用GDI+绘制它的外观行不?
      

  2.   

    panel来模拟combobox
    禁止下拉
    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c88c.aspx#q962q
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
            int GW_CHILD = 5;        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
            public const int EM_SETREADONLY = 0xcf;        private void Form1_Load(object sender, EventArgs e)
            {
                this.comboBox1.Items.AddRange(new string[] { "1", "2", "3" });
                this.comboBox1.SelectedIndex = 0;
                IntPtr editHandle = GetWindow(comboBox1.Handle, GW_CHILD);
                myText mt = new myText(editHandle);
            }
        }    public class myText : NativeWindow
        {
            public myText(IntPtr hwnd)
            {
                this.AssignHandle(hwnd);
            }        protected override void WndProc(ref Message m)
            {
                if (m.Msg == 7)
                {
                    return;
                }
                base.WndProc(ref m);
            }
        }
    }
    http://www.cnblogs.com/liukemng/archive/2010/12/04/1896469.html
      

  4.   

    楼上的方法可行,谢谢!楼上可否解释一下以下代码为什么不行呢?
    Public Class ocxComboBox
      Inherits Windows.Forms.ComboBox
      '获焦消息
      Dim WM_SETFOCUS As Integer = &H7  Protected Overrides Sub WndProc(ByRef msg As System.Windows.Forms.Message)   
      If msg.Msg = WM_SETFOCUS Then
      Return
      End If
      MyBase.WndProc(msg)
      End SubEnd Class
      

  5.   

    直接继承ComboBox,然后overrride它的WndPro方法就可以了,不用太费事。
    public class MyComboBox : ComboBox
    {
        private bool _允许下拉 = true;    public bool 允许下拉
        {
            get { return _允许下拉; }
            set { _允许下拉 = value; }
        }    protected override void WndProc(ref Message m)
        {
            if (m.Msg == 7 && !允许下拉)
                return;        base.WndProc(ref m);
        }
    }编译了以后,你的工具栏上最顶部的一个Panel里就出现了自己的控件,用它来替代ComboBox就行了。
      

  6.   

     public class MyComboBox : ComboBox
        {
            private bool _允许下拉 = true;        public bool 允许下拉
            {
                get { return _允许下拉; }
                set { _允许下拉 = value; }
            }        protected override void WndProc(ref Message m)
            {
                if (m.Msg == 7)
                {
                    Console.WriteLine(m.HWnd.ToString());
                    Console.WriteLine(this.Handle.ToString());
                    return;
                }
                base.WndProc(ref m);
            }
        }comboBox其实是一个嵌套控件(复合控件)在DropDownList状态时;他由下拉列表,和ComboBox本身组成
      DropDown状态时ComboBox中多了一个edit就是.net下的TextBox那个输入状态是由edit控制的;
    直接消息重载里面获得焦点时的句柄是comboBox本身的句柄,并不是TextBox的句柄,所以没有拦截到TextBox的消息
      

  7.   


    我把您的代码试了一下,这个MyComboBox的编辑框仍然可以落焦的呀?!您的写法其实跟我原来的一样,我也有编译也是不行。
      

  8.   

    键盘部分,可以重写 ProcessKeyEventArgs 让它根据情况返回true。诸如此类的吧。