Is it possible to disable the hittest on a Windows Forms window, and if so, how do I do it? I want to have a opaque window that cannot be clicked.
是否可以在Windows窗体窗口中禁用hittest,如果是,我该如何操作?我想要一个无法点击的不透明窗口。
Thanks in advance, Christoph
提前谢谢,Christoph
3 个解决方案
#1
If you're talking to a different process, you need to send and retrieve Windows messages.
如果您正在与另一个进程通信,则需要发送和检索Windows消息。
Have a look at this link:
看看这个链接:
Using Window Messages to Implement Global System Hooks in C# http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
使用窗口消息在C#中实现全局系统挂钩http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult (impossible, according to MSDN) to implement in C#. This article attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue.
全局系统挂钩允许应用程序拦截用于其他应用程序的Windows消息。在C#中实现这一点一直很困难(根据MSDN不可能)。本文试图通过在C ++中创建一个DLL包装器来实现全局系统挂钩,该包装器将消息发布到挂钩应用程序的消息队列。
#2
Do you want a window that cannot be moved? Set FormBorderStyle to none.
你想要一个无法移动的窗户吗?将FormBorderStyle设置为none。
#3
Well, I still don't know much about your use case, but I'll take a stab anyway, and provide a simple example.
好吧,我仍然不太了解你的用例,但无论如何我会采取刺,并提供一个简单的例子。
I assume that you want to control something on the main form from your floating form. To do this, you need a reference to your main form from your floating form. You do this by creating a constructor overload in your floating form that accepts an instance of your main form, like this:
我假设您想要从浮动表单控制主窗体上的某些内容。为此,您需要从浮动表单引用主表单。您可以通过在浮动表单中创建一个构造函数重载来接受主表单的实例,如下所示:
public FloatingForm(MainForm mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
The floating form contains a textbox named floatingFormTextBox, and a button named Button1. The partial class for the floating form looks like this:
浮动表单包含一个名为floatingFormTextBox的文本框和一个名为Button1的按钮。浮动表单的部分类如下所示:
public partial class FloatingForm : Form
{
MainForm _mainForm;
public FloatingForm()
{
InitializeComponent();
}
public FloatingForm(MainForm mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
private void button1_Click(object sender, EventArgs e)
{
_mainForm.DoSomething(floatingFormTextBox.Text);
}
}
The main form just contains a textbox named mainFormTextBox. When the main form loads, it creates an instance of the floating form, passing a reference to itself to the floating form's new constructor overload. The partial class for the main form looks like this:
主窗体只包含一个名为mainFormTextBox的文本框。当主窗体加载时,它会创建浮动窗体的实例,将对自身的引用传递给浮动窗体的新构造函数重载。主窗体的部分类如下所示:
public partial class MainForm : Form
{
FloatingForm _floatingForm;
public MainForm()
{
InitializeComponent();
}
public void DoSomething(string text)
{
mainFormTextBox.Text = text;
this.Refresh();
}
private void MainForm_Load(object sender, EventArgs e)
{
_floatingForm = new FloatingForm(this);
_floatingForm.Show();
}
}
Now, when I put some text into the textbox of the floating form and click the button, the text shows up in the textbox of the main form.
现在,当我将一些文本放入浮动表单的文本框中并单击按钮时,文本将显示在主窗体的文本框中。
#1
If you're talking to a different process, you need to send and retrieve Windows messages.
如果您正在与另一个进程通信,则需要发送和检索Windows消息。
Have a look at this link:
看看这个链接:
Using Window Messages to Implement Global System Hooks in C# http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
使用窗口消息在C#中实现全局系统挂钩http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult (impossible, according to MSDN) to implement in C#. This article attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue.
全局系统挂钩允许应用程序拦截用于其他应用程序的Windows消息。在C#中实现这一点一直很困难(根据MSDN不可能)。本文试图通过在C ++中创建一个DLL包装器来实现全局系统挂钩,该包装器将消息发布到挂钩应用程序的消息队列。
#2
Do you want a window that cannot be moved? Set FormBorderStyle to none.
你想要一个无法移动的窗户吗?将FormBorderStyle设置为none。
#3
Well, I still don't know much about your use case, but I'll take a stab anyway, and provide a simple example.
好吧,我仍然不太了解你的用例,但无论如何我会采取刺,并提供一个简单的例子。
I assume that you want to control something on the main form from your floating form. To do this, you need a reference to your main form from your floating form. You do this by creating a constructor overload in your floating form that accepts an instance of your main form, like this:
我假设您想要从浮动表单控制主窗体上的某些内容。为此,您需要从浮动表单引用主表单。您可以通过在浮动表单中创建一个构造函数重载来接受主表单的实例,如下所示:
public FloatingForm(MainForm mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
The floating form contains a textbox named floatingFormTextBox, and a button named Button1. The partial class for the floating form looks like this:
浮动表单包含一个名为floatingFormTextBox的文本框和一个名为Button1的按钮。浮动表单的部分类如下所示:
public partial class FloatingForm : Form
{
MainForm _mainForm;
public FloatingForm()
{
InitializeComponent();
}
public FloatingForm(MainForm mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
private void button1_Click(object sender, EventArgs e)
{
_mainForm.DoSomething(floatingFormTextBox.Text);
}
}
The main form just contains a textbox named mainFormTextBox. When the main form loads, it creates an instance of the floating form, passing a reference to itself to the floating form's new constructor overload. The partial class for the main form looks like this:
主窗体只包含一个名为mainFormTextBox的文本框。当主窗体加载时,它会创建浮动窗体的实例,将对自身的引用传递给浮动窗体的新构造函数重载。主窗体的部分类如下所示:
public partial class MainForm : Form
{
FloatingForm _floatingForm;
public MainForm()
{
InitializeComponent();
}
public void DoSomething(string text)
{
mainFormTextBox.Text = text;
this.Refresh();
}
private void MainForm_Load(object sender, EventArgs e)
{
_floatingForm = new FloatingForm(this);
_floatingForm.Show();
}
}
Now, when I put some text into the textbox of the floating form and click the button, the text shows up in the textbox of the main form.
现在,当我将一些文本放入浮动表单的文本框中并单击按钮时,文本将显示在主窗体的文本框中。