我正在尝试在WinForms中创建一个自定义的AutoCompleteTextBox控件。与(string).Contains相反,basic TextBox提供的AutoComplete只提供(string).StartsWith的结果。
我在停靠在单独Form中的ListBox中显示自动完成搜索结果。我可以防止Form最初使用via窃取焦点:
protected override bool ShowWithoutActivation
{
get { return true; }
}然后,我可以通过重写WndProc方法来完全阻止Form获得焦点,方法如下:
protected override void WndProc( ref Message m )
{
base.WndProc( ref m );
switch ( m.Msg )
{
case WM_MOUSEACTIVATE:
m.Result = ( IntPtr ) MA_NOACTIVATEANDEAT;
break;
default:
break;
}执行此操作时,Form中包含的ListBox将接收MouseMoved事件,但不会接收MouseClicked事件。
Chaning MA_NOACTIVATEANDEAT to just MA_NOACTIVATE会将鼠标事件传递给ListBox,但随后会导致ListBox上的点击从ListBox所在的Form上窃取焦点-将其传递给ListBox所在的“浮动”Form。
有没有什么方法可以防止‘浮动’Form从'main‘Form中窃取焦点,同时仍然在ListBox中获取MouseClick事件
发布于 2012-06-22 06:33:16
将你的浮动表单转换成一个Usercontrol,如下所示:-在它上面拉一个ListBox到Click事件来模拟你的焦点场景。
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class InactiveForm : UserControl
{
private const int WS_EX_TOOLWINDOW = 0x00000080;
private const int WS_EX_NOACTIVATE = 0x08000000;
private const int WS_EX_TOPMOST = 0x00000008;
[DllImport("user32")]
public static extern int SetParent
(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32")]
public static extern int ShowWindow
(IntPtr hWnd, int nCmdShow);
protected override CreateParams CreateParams
{
get
{
CreateParams p = base.CreateParams;
p.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
p.Parent = IntPtr.Zero;
return p;
}
}
public InactiveForm()
{
InitializeComponent();
}
public new void Show()
{
if (this.Handle == IntPtr.Zero) base.CreateControl();
SetParent(base.Handle, IntPtr.Zero);
ShowWindow(base.Handle, 1);
}
private void OnListBoxClicked(object sender, EventArgs e)
{
MessageBox.Show("Clicked List Box on floating control");
}
}
}MainForm中的代码(附加了一个按钮及其单击处理程序):-
这将使用ListBox调用浮动控件。
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
InactiveForm f = new InactiveForm();
f.Show();
}
}
}当您将UserControl (无边框窗体)显示为浮动窗体时,您将注意到it wont receive focus even if you clicked (selected) any of its children。在本例中,子对象是用户控件上的ListBox。
发布于 2013-01-29 23:58:51
除了Angshuman Agarwal之前的回答:如果你希望当UserControl显示为浮动表单时,主表单不停用,修改一些代码到你的UserControl:
private Control _mControl;
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
public new void Show(Control c)
{
if (c == null) throw new ArgumentNullException();
_mControl = c;
if (this.Handle == IntPtr.Zero) base.CreateControl();
SetParent(base.Handle, IntPtr.Zero);
ShowWindow(base.Handle, 1);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x86) //WM_NCACTIVATE
{
if (m.WParam != IntPtr.Zero) //activate
{
SendMessage(_mControl.Handle, 0x86, (IntPtr)1, IntPtr.Zero);
}
this.DefWndProc(ref m);
return;
}
base.WndProc(ref m);
}发布于 2014-01-31 00:08:13
我已经创建了类似的东西,我的解决方案与lAbstract建议的类似。浮动窗体有一个事件DoReturnFocus,自定义TextBox (本例中为AutoCompleteTextBox控件)订阅该事件,并简单地将焦点设置回自身。为了防止主窗体出现在浮动窗体的前面,我将主窗体设置为浮动窗体的所有者,因为拥有的窗体永远不会显示在其所有者窗体的后面。
有关Form.Owner Property的更多信息
https://stackoverflow.com/questions/11143905
复制相似问题