8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

Winforms-如何使 MessageBox 显示在 MainForm 的中心?

BioData41 1月前

24 0

Winforms-如何让对话框显示在 MainForm 的中心?这与基于普通窗口默认设置相反,后者将它们显示在屏幕的中心。在我的例子中,我有一个小的

Winforms-如何使对话框显示在 MainForm 的中心?这与基于普通窗口默认设置(将对话框显示在屏幕中心)相反。

在我的例子中,我有一个小的主窗体,例如,它可能被放置在一个角落里,MessageBox 弹出窗口显示在看似很远的地方。

帖子版权声明 1、本帖标题:Winforms-如何使 MessageBox 显示在 MainForm 的中心?
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由BioData41在本站《winforms》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 事实证明,该类还适用于其他两种情况。我有一个 FolderBrowserDialog,我想让它变大,并且让它出现在父对话框的左上角附近(靠近我单击以打开它的按钮)。

    我复制了 CenterWinDialog 类并创建了两个新类。一个类更改对话框大小,另一个类将其位置更改为与父窗体的特定偏移量。这是用法:

            using (new OffsetWinDialog(this) { PreferredOffset = new Point(75, 75 )})
            using (new SizeWinDialog(this)   { PreferredSize   = new Size(400, 600)})
            {
                DialogResult result = dlgFolderBrowser.ShowDialog();
                if (result == DialogResult.Cancel)
                    return;
            }
    

    这两个类是基于原类的。

    class OffsetWinDialog : IDisposable
    {
        private int mTries = 0;
        private Form mOwner;
    
        public OffsetWinDialog(Form owner)
        {
            mOwner = owner;
            owner.BeginInvoke(new MethodInvoker(findDialog));
        }
    
        public Point PreferredOffset { get; set; }
    
        private void findDialog()
        {
            // Enumerate windows to find the message box
            if (mTries < 0) 
                return;
            EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
            if (EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero))
            {
                if (++mTries < 10)
                    mOwner.BeginInvoke(new MethodInvoker(findDialog));
            }
        }
        private bool checkWindow(IntPtr hWnd, IntPtr lp)
        {
            // Checks if <hWnd> is a dialog
            StringBuilder sb = new StringBuilder(260);
            GetClassName(hWnd, sb, sb.Capacity);
            if (sb.ToString() != "#32770") return true;
            // Got it
            Rectangle frmRect = new Rectangle(mOwner.Location, mOwner.Size);
            RECT dlgRect;
            GetWindowRect(hWnd, out dlgRect);
            MoveWindow(hWnd,
                frmRect.Left   + PreferredOffset.X,
                frmRect.Top    + PreferredOffset.Y,
                dlgRect.Right  - dlgRect.Left,
                dlgRect.Bottom - dlgRect.Top, 
                true);
            return false;
        }
        public void Dispose()
        {
            mTries = -1;
        }
    
        // P/Invoke declarations
        private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
        [DllImport("user32.dll")]
        private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
        [DllImport("kernel32.dll")]
        private static extern int GetCurrentThreadId();
        [DllImport("user32.dll")]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
        [DllImport("user32.dll")]
        private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc);
        [DllImport("user32.dll")]
        private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);
        private struct RECT { public int Left; public int Top; public int Right; public int Bottom; }
    }
    

    class SizeWinDialog : IDisposable
    {
        private int mTries = 0;
        private Form mOwner;
    
        public SizeWinDialog(Form owner)
        {
            mOwner = owner;
            mOwner.BeginInvoke(new Action(findDialog));
        }
    
        public Size PreferredSize { get; set; }
    
        private void findDialog()
        {
            // Enumerate windows to find the message box
            if (mTries < 0) 
                return;
            EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
            if (EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero))
            {
                if (++mTries < 10) 
                    mOwner.BeginInvoke(new MethodInvoker(findDialog));
            }
        }
        private bool checkWindow(IntPtr hWnd, IntPtr lp)
        {
            // Checks if <hWnd> is a dialog
            StringBuilder sb = new StringBuilder(260);
            GetClassName(hWnd, sb, sb.Capacity);
            if (sb.ToString() != "#32770") 
                return true;
            // Got it
            Rectangle frmRect = new Rectangle(mOwner.Location, mOwner.Size);
            RECT dlgRect;
            GetWindowRect(hWnd, out dlgRect);
            SetWindowPos(new HandleRef(this, hWnd), new HandleRef(), dlgRect.Left, dlgRect.Top, PreferredSize.Width, PreferredSize.Height, 20 | 2);
            return false;
        }
        public void Dispose()
        {
            mTries = -1;
        }
    
        // P/Invoke declarations
        private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
        [DllImport("user32.dll")]
        private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
        [DllImport("kernel32.dll")]
        private static extern int GetCurrentThreadId();
        [DllImport("user32.dll")]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
        [DllImport("user32.dll")]
        private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc);
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool SetWindowPos(HandleRef hWnd, HandleRef hWndInsertAfter, int x, int y, int cx, int cy,
            int flags);
    
        private struct RECT { public int Left; public int Top; public int Right; public int Bottom; }
    }
    
返回
作者最近主题: