在 C# 中获取窗口句柄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1402848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:07:37  来源:igfitidea点击:

Getting the handle of window in C#

c#winforms

提问by Joel Barsotti

I'm trying to do some P/Invoke stuff and need the handle of the current window.

我正在尝试做一些 P/Invoke 的事情并且需要当前窗口的句柄。

I found Getting the handle of window in C#

我发现在 C# 中获取窗口的句柄

But it appears that only works in WPF. Is there a winForms equivalent?

但它似乎只适用于 WPF。是否有一个 winForms 等价物?

采纳答案by dtb

Control.Handle

Control.Handle

An IntPtrthat contains the window handle (HWND) of the control.

一个IntPtr包含该控件的窗口句柄(HWND)。

回答by ZokiManas

Try this in your form:

在您的表单中尝试此操作:

IntPtr myHandle = this.Handle;

IntPtr myHandle = this.Handle;

It will return the handle of the form.

它将返回表单的句柄。