Question

I am attempting to use this code sample to control the Windows XP On-Screen Keyboard (OSK.exe) from a C# (.NET 3.5) Winforms application:

[DllImport("User32.dll")]public static extern Int32 SetForegroundWindow(int hWnd);  
[DllImport("user32.dll")]public static extern int FindWindow(string lpClassName, string lpWindowName);
private void BringToFront(string className,string CaptionName)        
{            
   SetForegroundWindow(FindWindow(className,CaptionName));        
}

private void Form1_Load(object sender, EventArgs e)        
{            
   BringToFront("Notepad", "Untitled - Notepad");                            
}

How do I determine the accurate className? I assume that the CaptionName is 'On-Screen Keyboard'.

Was it helpful?

Solution

Looks like the classname is: "OSKMainClass"

Here is the code I used to find this. It's just a simple C# Forms App

    [DllImport("User32.dll")]
    public static extern Int32 SetForegroundWindow(int hWnd);
    [DllImport("user32.dll")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll")]
    public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int hWnd =  FindWindow(null, "On-Screen Keyboard");
        StringBuilder buffer = new StringBuilder(128);
        GetClassName(hWnd, buffer, buffer.Capacity);
        MessageBox.Show(buffer.ToString());
    }

Got this from the following sources Activate Any Window With API and MSDN GetClassName function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top