兔八哥极品软件园    运行: 4502天 | 文章:640 篇 | 评论:506 条 | 碎语:1条

(模态)子线程中的MessageBox在主线程之上显示

作者:admin 发布于:2012-3-29 14:07 Thursday 分类:.NET


1.创建类,继承接口IWin32Window

public class WindowWrapper : System.Windows.Forms.IWin32Window
{
        private System.IntPtr _hwnd;
        public WindowWrapper(System.IntPtr handle)
        {
            _hwnd = handle;
        }
        public System.IntPtr Handle
        {
            get { return _hwnd; }
        }
} 

2.然后封装系统获取主窗体API

[System. Runtime. InteropServices. DllImport ( "user32")]
public static extern System. IntPtr GetForegroundWindow(); 

3.调用MessageBox

System.IntPtr IntPart;
IntPart = GetForegroundWindow();
WindowWrapper ParentFrm = new WindowWrapper(IntPart);
MessageBox.Show(ParentFrm, "测试OK!", "提示", MessageBoxButtons.OK,MessageBoxIcon.Information);

标签: MessageBox


Powered by 兔八哥极品软件 苏ICP备12049267号 sitemap