久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

如何讓MessageBox.Show的在父窗口上居中顯示...

 蝸牛之窩 2009-11-17
如何在.NET Windows Form編程中讓MessageBox.Show()彈出的消息框在父窗口上居中顯示?
方法一:由于在VS2008中MessageBox共有21個(gè)重載方法,,且沒(méi)有一個(gè)方法可以指定顯示的位置,。這21個(gè)靜態(tài)方法,實(shí)際上是調(diào)用的Windows API,,你可以嘗試直接調(diào)用該API來(lái)實(shí)現(xiàn),。
方法二推薦):由于MessageBox不能繼承和重寫(xiě),所以可以自己定義一個(gè)MessageBox窗體,,注意設(shè)置DialogResult屬性,,設(shè)置StartPosition為CenterParent在父窗口居中,當(dāng)然你可以設(shè)置Left和Top屬性,,使消息窗口中任意位置,。使用ShowDialog方法顯示與MessageBox.Show類(lèi)似的模態(tài)窗口,。
方法三:如果你一定要用MessageBox.Show方法,可以使用Windows API FindWindow函數(shù)通過(guò)Title找到MessageBox窗口的句柄,,通過(guò)API函數(shù)MoveWindow設(shè)置窗口的位置,。下面為MessageBoxEx.cs的代碼,可以直接使用MessageBoxEx.Show()方法實(shí)現(xiàn)MessageBox.Show()的功能,,并在父窗口上居中顯示:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication3
{
        struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        };
        //實(shí)現(xiàn)MessageBox居中owner窗體顯示
        class MessageBoxEx
        {
            public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
            [DllImport("user32.dll")]
            private static extern IntPtr SetWindowsHookEx(int hookid,
                HookProc pfnhook, IntPtr hinst, int threadid);
            [DllImport("user32.dll")]
            private static extern IntPtr CallNextHookEx(IntPtr hhook,
                int code, IntPtr wparam, IntPtr lparam);
            [DllImport("kernel32.dll")]
            private static extern IntPtr GetModuleHandle(string modName);
            [DllImport("user32.dll")]
            private static extern bool UnhookWindowsHookEx(IntPtr hhook);
            [DllImport("user32.dll")]
            private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
            [DllImport("user32.dll")]
            private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
            [DllImport("user32.dll")]
            private static extern bool MoveWindow(
                IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
            private const int WH_CBT = 5;
            private const int HCBT_ACTIVATE = 5;
            private const int GW_OWNER = 4;
            private static IntPtr hookHandle = IntPtr.Zero;
            private static RECT GetOwnerRect(IntPtr hwnd)
            {
                RECT ownerRect = new RECT();
                IntPtr ownerHwnd = GetWindow(hwnd, GW_OWNER);
                GetWindowRect(ownerHwnd, ref ownerRect);
                return ownerRect;
            }
            private static IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                switch (nCode)
                {
                    case HCBT_ACTIVATE:
                        RECT vRectangle = new RECT();
                        RECT ownerRect = GetOwnerRect(wParam);
                        GetWindowRect(wParam, ref vRectangle);
                        int width = vRectangle.right - vRectangle.left;
                        int height = vRectangle.bottom - vRectangle.top;
                        int ownerWidth = ownerRect.right - ownerRect.left;
                        int ownerHeight = ownerRect.bottom - ownerRect.top;
                        int left = Math.Max(ownerRect.left + (ownerWidth - width) / 2, 0);
                        int top = Math.Max(ownerRect.top + (ownerHeight - height) / 2, 0);
                        MoveWindow(wParam,
                            left,
                            top,
                            width, height, false);
                        UnhookWindowsHookEx(hookHandle);
                        break;
                }
                return CallNextHookEx(hookHandle, nCode, wParam, lParam);
            }
            private static void Lock()
            {
                hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback),
                    GetModuleHandle(null), 0);
            }
            //根據(jù)需要重載
            public static DialogResult Show(string text)
            {
                Lock();
                return MessageBox.Show(text);
            }
            public static DialogResult Show(IWin32Window owner, string text)
            {
                Lock();
                return MessageBox.Show(owner, text);
            }
            public static DialogResult Show(string text, string caption)
            {
                Lock();
                return MessageBox.Show(text, caption);
            }
            public static DialogResult Show(IWin32Window owner, string text, string caption)
            {
                Lock();
                return MessageBox.Show(owner, text, caption);
            }
            public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
            {
                Lock();
                return MessageBox.Show(text, caption, buttons);
            }
            public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
            {
                Lock();
                return MessageBox.Show(owner, text, caption, buttons);
            }
            public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
            {
                Lock();
                return MessageBox.Show(text, caption, buttons, icon);
            }
            public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
            {
                Lock();
                return MessageBox.Show(owner, text, caption, buttons, icon);
            }
            public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
            {
                Lock();
                return MessageBox.Show(text, caption, buttons, icon, defaultButton);
            }
        }
}
轉(zhuǎn)載請(qǐng)保留本文鏈接: http://user.qzone.qq.com/707043659/blog/1247672380

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多