WPF窗口如何获得一个句柄?
2012-12-15 22:38
405 查看
1) 在窗体事件内获得句柄
private
void
Window_Loaded(object sender,
RoutedEventArgs e)
{
WindowInteropHelper wndHelper
= new
WindowInteropHelper(this);
IntPtr wpfHwnd=wndHelper.Handle;
}
2.修改窗体属性:窗体风格
private
void
Window_Loaded(object sender,
RoutedEventArgs e)
{
WindowInteropHelper wndHelper
= new
WindowInteropHelper(this);
int exStyle
= (int)GetWindowLong(wndHelper.Handle,
(int)GetWindowLongFields.GWL_EXSTYLE);
exStyle |=
(int)ExtendedWindowStyles.WS_EX_TOOLWINDOW;
SetWindowLong(wndHelper.Handle,
(int)GetWindowLongFields.GWL_EXSTYLE,
(IntPtr)exStyle);
}
函数定义:
#region Window styles
[Flags]
public
enum
ExtendedWindowStyles
{
// ...
WS_EX_TOOLWINDOW =
0x00000080,
// ...
}
public
enum
GetWindowLongFields
{
// ...
GWL_EXSTYLE = (-20),
// ...
}
[DllImport("user32.dll")]
public
static
extern
IntPtr
GetWindowLong(IntPtr hWnd,
int nIndex);
public
static
IntPtr
SetWindowLong(IntPtr hWnd,
int nIndex,
IntPtr dwNewLong)
{
int error
= 0;
IntPtr result
= IntPtr.Zero;
// Win32 SetWindowLong doesn't clear error on success
SetLastError(0);
if
(IntPtr.Size
== 4)
{
// use SetWindowLong
Int32 tempResult
= IntSetWindowLong(hWnd, nIndex,
IntPtrToInt32(dwNewLong));
error = Marshal.GetLastWin32Error();
result = new
IntPtr(tempResult);
}
else
{
// use SetWindowLongPtr
result = IntSetWindowLongPtr(hWnd,
nIndex, dwNewLong);
error = Marshal.GetLastWin32Error();
}
if
((result ==
IntPtr.Zero)
&& (error
!= 0))
{
throw
new
System.ComponentModel.Win32Exception(error);
}
return result;
}
[DllImport("user32.dll",
EntryPoint
= "SetWindowLongPtr",
SetLastError
= true)]
private
static
extern
IntPtr
IntSetWindowLongPtr(IntPtr hWnd,
int nIndex,
IntPtr dwNewLong);
[DllImport("user32.dll",
EntryPoint
= "SetWindowLong",
SetLastError
= true)]
private
static
extern
Int32
IntSetWindowLong(IntPtr hWnd,
int nIndex,
Int32 dwNewLong);
private
static
int
IntPtrToInt32(IntPtr intPtr)
{
return
unchecked((int)intPtr.ToInt64());
}
[DllImport("kernel32.dll",
EntryPoint
= "SetLastError")]
public
static
extern
void
SetLastError(int dwErrorCode);
#endregion
转载网址:http://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher
private
void
Window_Loaded(object sender,
RoutedEventArgs e)
{
WindowInteropHelper wndHelper
= new
WindowInteropHelper(this);
IntPtr wpfHwnd=wndHelper.Handle;
}
2.修改窗体属性:窗体风格
private
void
Window_Loaded(object sender,
RoutedEventArgs e)
{
WindowInteropHelper wndHelper
= new
WindowInteropHelper(this);
int exStyle
= (int)GetWindowLong(wndHelper.Handle,
(int)GetWindowLongFields.GWL_EXSTYLE);
exStyle |=
(int)ExtendedWindowStyles.WS_EX_TOOLWINDOW;
SetWindowLong(wndHelper.Handle,
(int)GetWindowLongFields.GWL_EXSTYLE,
(IntPtr)exStyle);
}
函数定义:
#region Window styles
[Flags]
public
enum
ExtendedWindowStyles
{
// ...
WS_EX_TOOLWINDOW =
0x00000080,
// ...
}
public
enum
GetWindowLongFields
{
// ...
GWL_EXSTYLE = (-20),
// ...
}
[DllImport("user32.dll")]
public
static
extern
IntPtr
GetWindowLong(IntPtr hWnd,
int nIndex);
public
static
IntPtr
SetWindowLong(IntPtr hWnd,
int nIndex,
IntPtr dwNewLong)
{
int error
= 0;
IntPtr result
= IntPtr.Zero;
// Win32 SetWindowLong doesn't clear error on success
SetLastError(0);
if
(IntPtr.Size
== 4)
{
// use SetWindowLong
Int32 tempResult
= IntSetWindowLong(hWnd, nIndex,
IntPtrToInt32(dwNewLong));
error = Marshal.GetLastWin32Error();
result = new
IntPtr(tempResult);
}
else
{
// use SetWindowLongPtr
result = IntSetWindowLongPtr(hWnd,
nIndex, dwNewLong);
error = Marshal.GetLastWin32Error();
}
if
((result ==
IntPtr.Zero)
&& (error
!= 0))
{
throw
new
System.ComponentModel.Win32Exception(error);
}
return result;
}
[DllImport("user32.dll",
EntryPoint
= "SetWindowLongPtr",
SetLastError
= true)]
private
static
extern
IntPtr
IntSetWindowLongPtr(IntPtr hWnd,
int nIndex,
IntPtr dwNewLong);
[DllImport("user32.dll",
EntryPoint
= "SetWindowLong",
SetLastError
= true)]
private
static
extern
Int32
IntSetWindowLong(IntPtr hWnd,
int nIndex,
Int32 dwNewLong);
private
static
int
IntPtrToInt32(IntPtr intPtr)
{
return
unchecked((int)intPtr.ToInt64());
}
[DllImport("kernel32.dll",
EntryPoint
= "SetLastError")]
public
static
extern
void
SetLastError(int dwErrorCode);
#endregion
转载网址:http://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher
相关文章推荐
- 如何通过窗口句柄来获得一个程序窗口的大小
- 如何在其它程序窗口(已知窗口的句柄)加上一个按钮
- 判断特定窗口是否存在、线程中获取获得一个顶层窗口的句柄
- 如何获得窗口句柄(HWND )和改变窗口属性
- 如何获得另外一个应用程序中的控件句柄。 - 日志 - tomore - 问友源码空间
- BCB如何获得当前所有可视窗口的窗口标题和窗口句柄
- 如何获得窗口句柄(HWND )和改变窗口属性
- 如何获得窗口句柄(HWND )和改变窗口属性
- autoIt DllCall 如何获得句柄和对窗口操作
- 如何获得窗口句柄以及WIN32+API参考大全
- 已知IWebBrowser2接口,如何获得其Internet Explorer_Server窗口句柄?
- 建立控制台程序--获得已经打开的一个窗口的句柄--给这个窗口发送一个关闭命令
- 如何获得窗口句柄(FindWindow函数的使用)
- 如何从Internet Explorer_Server窗口句柄获得IHTMLDocument
- 如何获得窗口句柄以及WIN32+API参考大全
- dll中如何获得调用者的窗口?得到窗口句柄那么DC也就可以得到了!
- 启动一个进程并获得其主窗口句柄
- [VC++]如何利用this获得窗口句柄
- 已知道 一个窗口的句柄, 如何扑捉该窗口的onresize 事件,并在onresize 事件中写入自己的代码?
- VC 如何在一个对话框中获得另一个对话框句柄()