wpf:窗体、控件句柄;不调整窗口大小,禁用关闭按钮,隐藏标题栏,设置无边框,窗口任意空白地方鼠标拖拽,任务管理器应用程序隐藏图标,背景图;;

窗体、控件句柄

窗体句柄: 
IntPtr hwnd = new WindowInteropHelper(this).Handle;
控件句柄: 
IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(uielement)).Handle;

不调整窗口大小

private void Window_Loaded(object sender, RoutedEventArgs e)
{
	this.ResizeMode = ResizeMode.NoResize;
}

#### 禁用关闭按钮
// 禁用菜单中的关闭按钮,不允许使用Alt + F4
private bool allowClosing = false;
protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);
    HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
    if (hwndSource != null)
    {
        hwndSource.AddHook(HwndSourceHook);
    }
}
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
private const uint MF_BYCOMMAND = 0x00000000;
private const uint MF_GRAYED = 0x00000001;
private const uint SC_CLOSE = 0xF060;
private const int WM_SHOWWINDOW = 0x00000018;
private const int WM_CLOSE = 0x10;
private IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    switch (msg)
    {
        case WM_SHOWWINDOW:
            {
                IntPtr hMenu = GetSystemMenu(hwnd, false);
                if (hMenu != IntPtr.Zero)
                {
                    EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
                }
            }
            break;
        case WM_CLOSE:
            if (!allowClosing)
            {
                handled = true;
            }
            break;
    }
    return IntPtr.Zero;
}

隐藏标题栏

窗口设计中设置两个属性 AllowsTransparency="True" WindowStyle="None"

窗口任意空白地方鼠标拖拽

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    try
    {
        this.DragMove();
    }
    catch { }
}

设置无边框

任务管理器应用程序隐藏图标

背景图

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ImageBrush b = new ImageBrush();
    b.ImageSource = new BitmapImage(new Uri("pack://application:,,,/logo.ico"));
    b.Stretch = Stretch.Fill;
    b.Opacity = 1;
    this.Background = b;
}

posted @ 2020-01-06 11:44  wesson2019  阅读(802)  评论(0编辑  收藏  举报