blez's : blog

code / design / music and more


Leave a comment

Get the color under cursor in hex

The following code gets the pixel color at position. HexConvert converts the color to web hex format.
Continue reading

Advertisements


Leave a comment

Get the currently focused control

It’s easy to get the currently foreground window (using the GetForegroundWindow API), but what about the currently focused child window?
Here’s one way.


        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

        [DllImport("kernel32.dll")]
        static extern uint GetCurrentThreadId();

        [DllImport("user32.dll")]
        static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

        [DllImport("user32.dll")]
        static extern IntPtr GetFocus();

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        public static IntPtr GetCurrentFocusWindow() {
            var remoteThreadId = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
            var currentThreadId = GetCurrentThreadId();
            
            // AttachThreadInput is needed so we can get the handle of a focused window in another app
            AttachThreadInput(remoteThreadId, currentThreadId, true);
            
            // Get the handle of a focused window
            IntPtr focussed = GetFocus();

            // Now detach since we got the focused handle
            AttachThreadInput(remoteThreadId, currentThreadId, false);

            return focussed;
        }


Leave a comment

Detect if a mouse is attached

I had to detect if a mouse is attached. So I googled a lot, but there was no solution, so I made one. I’m not sure how good it is, but it worked in my case. Comment if you find it wrong.

bool HasMouseAttached() {
    ManagementObjectCollection collection;
    using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PointingDevice"))
        collection = searcher.Get();

    bool hasMouse = false;

    foreach (var device in collection) {                
        var name = (string) device.GetPropertyValue("Caption");
        name = name.ToLowerInvariant().Trim();

        if (name == "hid-compliant mouse") continue;
                                
        if (name.Contains("mouse") || name.Contains("input device")) {
            hasMouse = true;
            break;
        }
    }

    collection.Dispose();
    return hasMouse;
}


Leave a comment

How to detect a Metro process?

I was searching for a way to detect if a process is IE 10 metro, not IE 10 desktop, then I saw that metro apps have no windows. I don’t know how Spy++ enums IE metro’s subwindows, but mine code doesn’t. That’s somehow a good thing, cause I can use that for detecting a metro app. I tried it with x86/x64 and console apps. Seems to work fine and detects only metro apps. Write if there’s a bug or a smarter solution.

        public static bool IsMetroApp(Process process) {            
            foreach (ProcessThread threadInfo in process.Threads) {
                var windows = GetWindowHandlesForThread(threadInfo.Id);
                if (windows != null && windows.Length > 0) return false;
            }

            return true;
        }

        private static IntPtr[] GetWindowHandlesForThread(int threadHandle) {
            results.Clear();
            EnumWindows(WindowEnum, threadHandle);
            return results.ToArray();
        }

        private delegate int EnumWindowsProc(IntPtr hwnd, int lParam);

        [DllImport("user32.Dll")]
        private static extern int EnumWindows(EnumWindowsProc x, int y);
        [DllImport("user32")]
        private static extern bool EnumChildWindows(IntPtr window, EnumWindowsProc callback, int lParam);
        [DllImport("user32.dll")]
        public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);

        private static List<IntPtr> results = new List<IntPtr>();

        private static int WindowEnum(IntPtr hWnd, int lParam) {
            int processID = 0;
            int threadID = GetWindowThreadProcessId(hWnd, out processID);
            if (threadID == lParam) {
                results.Add(hWnd);
                EnumChildWindows(hWnd, WindowEnum, threadID);
            }

            return 1;
        }