狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C# 獲取窗口句柄并且關閉應用程序

admin
2023年9月15日 11:50 本文熱度 554

public class User32API

    {

        private static Hashtable processWnd = null;

        public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);

        static User32API()

        {

            if (processWnd == null)

            {

                processWnd = new Hashtable();

            }

        }

        [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]

        public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

        [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]

        public static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]

        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

        [DllImport("user32.dll", EntryPoint = "IsWindow")]

        public static extern bool IsWindow(IntPtr hWnd);

        [DllImport("kernel32.dll", EntryPoint = "SetLastError")]

        public static extern void SetLastError(uint dwErrCode);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]

        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        Process[] processes;

        const int WM_CLOSE = 0x0010;


        public void GetCurrentWindowHandle()

        {

            Microsoft.Win32.RegistryKey oRegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Amada Software");

            string[] subKeys = oRegistryKey.GetSubKeyNames();

            IntPtr ptrWnd = IntPtr.Zero;

            foreach (string subkey in subKeys)

            {

                processes = Process.GetProcessesByName(subkey);

                foreach (Process proc in processes)

                {

                    uint uiPid = (uint)proc.Id;

                    object objWnd = processWnd[uiPid];

                    if (objWnd != null)

                    {

                        ptrWnd = (IntPtr)objWnd;

                        if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))

                        {

                            SendMessage(ptrWnd, WM_CLOSE, 0, 0);

                        }

                        else

                        {

                            ptrWnd = IntPtr.Zero;

                        }

                    }

                    bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);

                    if (!bResult && Marshal.GetLastWin32Error() == 0)

                    {

                        objWnd = processWnd[uiPid];

                        if (objWnd != null)

                        {

                            ptrWnd = (IntPtr)objWnd;

                            SendMessage(ptrWnd, WM_CLOSE, 0, 0);

                        }

                    }

                }

            }

        }


        private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)

        {

            uint uiPid = 0;

            if (GetParent(hwnd) == IntPtr.Zero)

            {

                GetWindowThreadProcessId(hwnd, ref uiPid);

                if (uiPid == lParam)

                {

                    processWnd[uiPid] = hwnd;

                    SetLastError(0);

                    return false;

                }

            }

            return true;

        }

    }


該文章在 2023/9/15 11:50:01 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved