Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Created August 27, 2012 08:56
Show Gist options
  • Save pinzolo/3486769 to your computer and use it in GitHub Desktop.
Save pinzolo/3486769 to your computer and use it in GitHub Desktop.
[WPF]WindowStyle が None なウィンドウを ALT+TAB で表示させないようにする
public class Native
{
[Flags]
public enum ExtendedWindowStyles
{
WS_EX_TOOLWINDOW = 0x80
}
public enum GetWindowLongFields
{
GWL_EXSTYLE = -20
}
#region GetWindowLontPtr
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
public static IntPtr GetWindowLontPtr(IntPtr hWnd, int nIndex)
{
return IntPtr.Size == 8 ? GetWindowLongPtr64(hWnd, nIndex) : GetWindowLongPtr32(hWnd, nIndex);
}
#endregion
#region SetWindowLongPtr
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
{
return IntPtr.Size == 8 ? SetWindowLongPtr64(hWnd, nIndex, dwNewLong)
: new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
}
#endregion
}
public partial class DummyWindow : Window
{
public DummyWindow()
{
InitializeComponent();
}
private void Window_SourceInitialized(object sender, EventArgs e)
{
var helper = new WindowInteropHelper(this);
var exStyle = Native.GetWindowLontPtr(helper.Handle, (int)Native.GetWindowLongFields.GWL_EXSTYLE).ToInt32();
exStyle = exStyle | (int)Native.ExtendedWindowStyles.WS_EX_TOOLWINDOW;
Native.SetWindowLongPtr(helper.Handle, (int)Native.GetWindowLongFields.GWL_EXSTYLE, new IntPtr(exStyle));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment