什么是PostMessage的相当于自我,Windows窗体?窗体、自我、PostMessage、Windows

2023-09-05 23:43:34 作者:沉沦

我在写的 System.Windows.Forms.Control的派生的自定义控制。

I'm writing a custom control derived from System.Windows.Forms.Control.

的控制是使用 Control.KeyDown 事件,看按键:我要处理一些按键(例如<按Ctrl> -K )的热键,这使我推出一个对话框。

The control is using the Control.KeyDown event, to watch keystrokes: I should handle some keystrokes (for example <Ctrl>-K) as hotkeys, which cause me to launch a dialog box.

如果我启动从我的的onkeydown 事件处理程序的对话框中,在我设置显示对话框 KeyEventArgs.Sup pressKey preSS 和返回(所以我未能晚饭preSS的 K 键preSS)。相反,我想从的onkeydown 事件处理程序后返回,并启动对话。要做到这一点,在我从的onkeydown 事件处理我需要再次莫名其妙地调用,具有某种事件对话框启动返回。

If I launch the dialog from my onKeyDown event handler, the dialog is displayed before I set KeyEventArgs.SuppressKeyPress to true and return (and so I'm failing to suppress the K keypress). Instead, I'd like to return from the onKeyDown event handler, and launch the dialog afterwards. To do this, after I return from the onKeyDown event handler I need to be invoked again somehow, with some kind of 'launch the dialog' event.

在Win32中,我可以生成此事件,通过使用 PostMessage的 API,发送注册的窗口消息对自己说:任何$ P $之后我会收到此消息在我的消息队列pvious消息,并把它作为信号来启动我的对话框。在这里,但是我不能使用 PostMessage的函数(也不是的WndProc 方法),因为我想用严格的管理的API(无需 SecurityPermissionFlag ::不受管理的code )。

On Win32, I could generate this event by using the PostMessage API, to send a registered window message to myself: I would receive this message right after any previous message in my message queue, and use it as the signal to launch my dialog. Here however I can't use PostMessage function (nor the WndProc method) because I want to use strictly managed APIs (without needing SecurityPermissionFlag::UnmanagedCode).

那么,什么将是管理的,用于一个线程(我的UI线程)来调度异步回调:某种也许定时器吗?某种自我? - 调用

So what would be the managed equivalent, for a thread (my UI thread) to schedule an asynchronous callback: perhaps a timer of some kind? Some kind of self-Invoke?

推荐答案

我的第一个念头是设置苏尔pressKey preSS属性的在的打开对话框,但是这不是真正回答你的问题。如果你真的需要从事件处理程序打开该对话框之前返回,看看BeginInvoke方法。

My first thought was "set the SurpressKeyPress property before opening the dialog", but that's not really answering your question. If you really need to return from the event handler before opening the dialog, take a look at the BeginInvoke method.

你可以做这样的事情:

...
this.BeginInvoke(new InvokeDelegate(showDlg));
KeyEventArgs.SuppressKeyPress = true;
...


public void showDlg()
{
   // create and show dialog here
}
 
精彩推荐
图片推荐