winform操做windows系統計算器

 

winform對系統計算器的調用,啓動,最大化最小化顯示,在mainwindow設置topmost=true時,正常顯示計算器並置頂。函數

        /// <summary>
        /// 獲取窗體的句柄函數
        /// </summary>
        /// <param name="lpClassName">窗口類名</param>
        /// <param name="lpWindowName">窗口標題名</param>
        /// <returns>返回句柄</returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>
        /// 窗體的顯示控制
        /// </summary>
        /// <param name="hWnd">句柄</param>
        /// <param name="nCmdShow">
        /// HIDE = 0
        /// NORMAL = 1
        /// MAXIMIZE = 3
        /// SHOWNOACTIVATE = 4
        /// SHOW = 5
        /// MINIMIZE = 6
        /// RESTORE = 9
        /// SHOWDEFAULT = 10</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow);

        /// <summary>
        /// 更改子窗口,彈出窗口或頂級窗口的大小,位置和Z順序。這些窗口是根據其在屏幕上的外觀排序的。最頂部的窗口得到最高排名,而且是Z順序中的第一個窗口。
        /// </summary>
        /// <param name="hWnd">窗口的句柄</param>
        /// <param name="hWndlnsertAfter">在Z順序中位於定位的窗口以前的窗口的句柄  0; 在前面 1; 在後面 -1; 在前面, 位於任何頂部窗口的前面 -2; 在前面, 位於其餘頂部窗口的後面</param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="cx"></param>
        /// <param name="cy"></param>
        /// <param name="Flags">窗口尺寸和定位的標誌</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);

            //啓動計算器
            public void start_Calc()
            {
                AppLog.Info($"start calc ...");

                // 獲取查找窗體句柄(經過窗體標題名)
                IntPtr calcHandle = FindWindow(null, "計算器");
                if (calcHandle == IntPtr.Zero)
                {
                    AppLog.Info($"pre start calc ...");
                    pcalc = Process.Start("calc.exe");
                    Thread.Sleep(800);
                    calcHandle = FindWindow(null, "計算器");
                }
                
                //將計算器顯示爲正常狀態
                bool resultSetWindowState = ShowWindowAsync(calcHandle, 1);
                AppLog.Info($"set calc window status:{resultSetWindowState}");

                //將計算器顯示在最上層
                bool result = SetWindowPos(calcHandle, new IntPtr(-1), 0, 0, 0, 0, 1 | 2);
                AppLog.Info($"set calc status:{result}");
            }
相關文章
相關標籤/搜索