• WPF:窗口居中置顶


    主窗口居中置顶

    public MainWindow()
    {
        InitializeComponent();
        WindowStartupLocation = WindowStartupLocation.CenterScreen;
        this.Topmost = true;
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        IntPtr hwnd = new WindowInteropHelper(this).Handle;
        TopMostTool.SetTopmost(hwnd);
    }
    
    using System;
    using System.Runtime.InteropServices;
    /// <summary>
    /// 置顶帮助类
    /// </summary>
    public class TopMostTool
    {
        public static int SW_SHOW = 5;
        public static int SW_NORMAL = 1;
        public static int SW_MAX = 3;
        public static int SW_HIDE = 0;
        public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);    //窗体置顶
        public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);    //取消窗体置顶
        public const uint SWP_NOMOVE = 0x0002;    //不调整窗体位置
        public const uint SWP_NOSIZE = 0x0001;    //不调整窗体大小
        public bool isFirst = true;
    
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
    
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow);
    
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        /// <summary>
        /// 查找子窗口
        /// </summary>
        /// <param name="hwndParent"></param>
        /// <param name="hwndChildAfter"></param>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
    
        /// <summary>
        /// 窗体置顶,可以根据需要传入不同的值(需要置顶的窗体的名字Title)
        /// </summary>
        public static void SetTopWindow()
        {
            IntPtr frm = FindWindow(null, "窗体的名字Title");    // 程序中需要置顶的窗体的名字
            if (frm != IntPtr.Zero)
            {
                SetWindowPos(frm, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    
                var child = FindWindowEx(frm, IntPtr.Zero, null, "子窗体的名字Title");
            }
        }
    
        public static void SetTopmost(IntPtr handle)
        {
            SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
        }
    }
    

    子窗口居中显示

    var dlg = new ChildDlg()
    {
        Background = Brushes.Black,
        Opacity = 0.8,
        AllowsTransparency = true,
        WindowStyle = WindowStyle.None,
        WindowState = WindowState.Normal,
        Topmost = true,
        WindowStartupLocation = WindowStartupLocation.CenterOwner,
        Owner = mainWindow,
        Width = mainWindow.Width,
        Height  = mainWindow.Height,
    };
    dlg.ShowDialog();
    
  • 相关阅读:
    span 固定宽度且与其它元素同一行的样式设置
    height、clientHeight、scrollHeight、offsetHeight区别
    jquery选择器多值情况处理(取select列表选项的值)
    用Javascript取float型小数点后两位,例22.127456取成22.13,如何做?
    【JS获取与设置】FCKeditor编辑器的值
    浅析java位运算符计算方式
    分治策略(算法)
    Java GUI入门教程
    Java面向对象知识总结
    数据结构(Java)——图的基础算法
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/12524927.html
Copyright © 2020-2023  润新知