Zac*_*son 9 .net c# wpf winapi handle
我需要访问我的一些WPF窗口的Win32窗口句柄,以便我可以处理Win32激活消息.我知道我可以使用PresentationSource.FromVisual或WindowInteropHelper获取Win32窗口句柄,但是如果尚未创建WPF窗口,我遇到了问题.
如果我使用PresentationSource.FromVisual并且尚未创建窗口,则返回PresentationSourcenull.如果我使用WindowInteropHelper并且尚未创建窗口,则Handle属性为IntPtr.Zero(null).
我打过电话this.Show(),并this.Hide()在窗口上之前,我试图访问手柄.然后我可以拿到手柄,但是窗口上的窗口瞬间闪烁(丑陋!).
有谁知道强制创建WPF窗口的方法?在Windows窗体中,这就像访问Form.Handle属性一样简单.
编辑:我最终选择了Chris Taylor的答案.在这里它是,如果它帮助其他人:
static void InitializeWindow(Window window)
{
// Get the current values of the properties we are going to change
double oldWidth = window.Width;
double oldHeight = window.Height;
WindowStyle oldWindowStyle = window.WindowStyle;
bool oldShowInTaskbar = window.ShowInTaskbar;
bool oldShowActivated = window.ShowActivated;
// Change the properties to make the window invisible
window.Width = 0;
window.Height = 0;
window.WindowStyle = WindowStyle.None;
window.ShowInTaskbar = false;
window.ShowActivated = false;
// Make WPF create the window's handle
window.Show();
window.Hide();
// Restore the old values
window.Width = oldWidth;
window.Height = oldHeight;
window.WindowStyle = oldWindowStyle;
window.ShowInTaskbar = oldShowInTaskbar;
window.ShowActivated = oldShowActivated;
}
// Use it like this:
InitializeWindow(myWpfWindow);
Run Code Online (Sandbox Code Playgroud)
一种选择是将窗口状态设置为最小化,并且在显示窗口之前不显示在任务栏中。尝试这样的事情。
IntPtr hWnd;
WindowInteropHelper helper = new WindowInteropHelper(wnd);
WindowState prevState = wnd.WindowState;
bool prevShowInTaskBar = wnd.ShowInTaskbar;
wnd.ShowInTaskbar = false;
wnd.WindowState = WindowState.Minimized;
wnd.Show();
hWnd = helper.Handle;
wnd.Hide();
wnd.ShowInTaskbar = prevShowInTaskBar;
wnd.WindowState = prevState;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6201 次 |
| 最近记录: |