1

我正在尝试使用此代码示例从 C# (.NET 3.5) Winforms 应用程序控制 Windows XP 屏幕键盘 (OSK.exe):

[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");                            
}

如何确定准确的类名?我假设 CaptionName 是“屏幕键盘”。

4

1 回答 1

3

看起来类名是:“OSKMainClass”

这是我用来找到它的代码。这只是一个简单的 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());
    }

从以下来源获得此信息使用 API 和MSDN GetClassName 函数激活任何窗口

于 2010-08-30T21:36:32.117 回答