c#显示隐藏的窗体和托盘中的程序

来源:互联网 发布:未来城网络黄金裴雷 编辑:程序博客网 时间:2024/06/01 09:09

将程序最小化到托盘已经是最常用的操作,但在C#中凭借 Form1.Visible=true; 抑或是Form1.TopMost=true; 只能将窗体显示在任务栏中,而不能将窗体直接显示给用户,这无疑是糟糕的用户体验。为了实现上述目的,我们需要借助于win32函数:

        #region win32函数
       
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpSpecifiedClassName, string lpWindowName);

       
        [DllImport("user32.dll ", SetLastError = true)]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

       
        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        public const int SW_RESTORE = 9;
        public static IntPtr formhwnd;
        #endregion

 

IntPtr frm = Form1.Handle; //得到窗体句柄
ShowWindow(frm, 9); //显示窗体
SwitchToThisWindow(frm, true); //切换到窗体

 

 

 

 

0 0
原创粉丝点击