blez's : blog

code / design / music and more


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;
}
Advertisements


Leave a comment

How to get the system colors using JQuery?

Using CSS2 we can get the current system colors (the OS colors). Here‘s an article for that. CSS knows about the colors, but how can we use them from JavaScript? It’s tricky, because an element first needs to be created to know the actual color. Here’s a workaround that returns the color in hex format:

function GetSystemColor(name) {
    // create an invisible element, so we can get its color
    var el = $("<div style='display: none; color: " + name + "'></div>");
    $("body").append(el); // it must be appended to make css know the actual color
    var color = el.css('color');
    el.remove();
    
    // convert rgb to hex
    color = color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    function hex(x) {
        return ("0" + parseInt(x).toString(16)).slice(-2).toUpperCase();
    }
    
    return "#" + hex(color[1]) + hex(color[2]) + hex(color[3]);
}


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;
        }


1 Comment

How to lighten or darken a color

The proper way to make a ligten or darken color:

/// <summary>
/// Creates color with corrected brightness.
/// </summary>
/// <param name="color">Color to correct.</param>
/// <param name="correctionFactor">The brightness correction factor. Must be between -1 and 1. 
/// Negative values produce darker colors.</param>
/// <returns>
/// Corrected <see cref="Color"/> structure.
/// </returns>
public static Color ChangeColorBrightness(Color color, float correctionFactor)
{
    float red = (float) color.R;
    float green = (float) color.G;
    float blue = (float) color.B;

    if (correctionFactor < 0)
    {
        correctionFactor = 1 + correctionFactor;
        red *= correctionFactor;
        green *= correctionFactor;
        blue *= correctionFactor;
    }
    else
    {
        red = (255 - red) * correctionFactor + red;
        green = (255 - green) * correctionFactor + green;
        blue = (255 - blue) * correctionFactor + blue;
    }

    return Color.FromArgb(color.A, (int) red, (int) green, (int) blue);
}