Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Code xác định ứng dụng đang tương tác ( Window is currently activated)

17:01 11-06-2017 754 lượt xem 7 bình luận 23:08 13-06-2017

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);

        public static bool ApplicationIsActivated()
        {
            var activatedHandle = GetForegroundWindow();
            if (activatedHandle == IntPtr.Zero)
            {
                return false;       // No window is currently activated
            }

            var procId = Process.GetCurrentProcess().Id;
            int activeProcId;
            GetWindowThreadProcessId(activatedHandle, out activeProcId);

            return activeProcId == procId;
        }

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
nguyendinhsen đã bình luận 23:08 13-06-2017

Code em lụm trên stackoverflow chủ yếu giải quyết vấn đề sau:
VD: em mở 2 file Autocad thì em cần xác định là mình đang làm việc với file đang chạy tool có phải đang là  file đang tương tác trực tiếp ko. Thử mấy cái thì thấy hàm này dễ dùng

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 23:38 11-06-2017

bạn có thể giải thích cụ thể code này hoạt động ra sao hem bạn

Câu hỏi mới nhất