Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# get foreground window

    #region Retrieve list of windows

    [DllImport("user32")]
    private static extern int GetWindowLongA(IntPtr hWnd, int index);

    [DllImport("USER32.DLL")]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    [DllImport("USER32.DLL")]
    private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);

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

    private const int GWL_STYLE = -16;

    private const ulong WS_VISIBLE = 0x10000000L;
    private const ulong WS_BORDER = 0x00800000L;
    private const ulong TARGETWINDOW = WS_BORDER | WS_VISIBLE;

    internal class Window
    {
        public string Title;
        public IntPtr Handle;

        public override string ToString()
        {
            return Title;
        }
    }

    private List<Window> windows;

    private void GetWindows()
    {
        windows = new List<Window>();
        EnumWindows(Callback, 0);
    }

    private bool Callback(IntPtr hwnd, int lParam)
    {
        if (this.Handle != hwnd && (GetWindowLongA(hwnd, GWL_STYLE) & TARGETWINDOW) == TARGETWINDOW)
        {
            StringBuilder sb = new StringBuilder(100);
            GetWindowText(hwnd, sb, sb.Capacity);

            Window t = new Window();
            t.Handle = hwnd;
            t.Title = sb.ToString();
            windows.Add(t);
        }

        return true; //continue enumeration
    }

    #endregion
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# remove first three characters from string 
Csharp :: round decimal two places 
Csharp :: unity get game version 
Csharp :: c# settings file 
Csharp :: unity find gameobject with layer 
Csharp :: timespan to integer c# 
Csharp :: c# get last day of month 
Csharp :: array of strings by splitting lines c# 
Csharp :: c# get total milliseconds from datetime 
Csharp :: How to catch Entity Framework Errors C# 
Csharp :: c# list of properties from list of objects 
Csharp :: how to deserialize string array in c# 
Csharp :: transform.position.x unity 
Csharp :: c# get battery level 
Csharp :: socket io connect to namespace 
Csharp :: c# add element to array 
Csharp :: convert-integer-to-binary-in-c-sharp 
Csharp :: how to concert a list into strinf splitted by coma c# 
Csharp :: c# close program 
Csharp :: from string 
Csharp :: if set active == false unity 
Csharp :: remove force unity 
Csharp :: published net core did not have wwwroot 
Csharp :: combobox selected item c# 
Csharp :: how to get an arrays length in c# 
Csharp :: mvc string format 
Csharp :: c# Dictionary contains key case insensitive 
Csharp :: C# trim trailing zero 
Csharp :: c# remove everything after last slash 
Csharp :: c# datetime remove days 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =