WPF窗口如何获得一个句柄?

来源:互联网 发布:淘宝客服招聘 可在家 编辑:程序博客网 时间:2024/05/16 12:36

1) 在窗体事件内获得句柄

privatevoidWindow_Loaded(object sender,RoutedEventArgs e)

{

          WindowInteropHelper wndHelper=newWindowInteropHelper(this);

          IntPtr  wpfHwnd=wndHelper.Handle;

}

 

2.修改窗体属性:窗体风格

privatevoidWindow_Loaded(object sender,RoutedEventArgs e)

{

     WindowInteropHelper wndHelper=newWindowInteropHelper(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]
   
publicenumExtendedWindowStyles
   
{
       
// ...
        WS_EX_TOOLWINDOW
= 0x00000080,
       
// ...
   
}

   
publicenumGetWindowLongFields
   
{
       
// ...
        GWL_EXSTYLE
= (-20),
       
// ...
   
}

   
[DllImport("user32.dll")]
   
publicstaticexternIntPtrGetWindowLong(IntPtr hWnd,int nIndex);

   
publicstaticIntPtrSetWindowLong(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
= newIntPtr(tempResult);
       
}
       
else
       
{
           
// use SetWindowLongPtr
            result
= IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
            error
= Marshal.GetLastWin32Error();
       
}

       
if((result==IntPtr.Zero)&&(error!=0))
       
{
           
thrownewSystem.ComponentModel.Win32Exception(error);
       
}

       
return result;
   
}

   
[DllImport("user32.dll",EntryPoint="SetWindowLongPtr",SetLastError=true)]
   
privatestaticexternIntPtrIntSetWindowLongPtr(IntPtr hWnd,int nIndex,IntPtr dwNewLong);

   
[DllImport("user32.dll",EntryPoint="SetWindowLong",SetLastError=true)]
   
privatestaticexternInt32IntSetWindowLong(IntPtr hWnd,int nIndex,Int32 dwNewLong);

   
privatestaticintIntPtrToInt32(IntPtr intPtr)
   
{
       
returnunchecked((int)intPtr.ToInt64());
   
}

   
[DllImport("kernel32.dll",EntryPoint="SetLastError")]
   
publicstaticexternvoidSetLastError(int dwErrorCode);
   
#endregion

 

转载网址:http://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher

原创粉丝点击