请能给我一个GetWindowText的实例?C#的!

来源:百度知道 编辑:UC知道 时间:2024/05/25 19:01:40
我要获取系统当前活动窗体的标题!
请看好问题!!!要的是实例!

using System;
using System.Text;
using System.Runtime.InteropServices;

class Program
{
[DllImport("user32.dll")]
public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

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

static void Main(string[] args)
{
StringBuilder str = new StringBuilder(512);

GetWindowText(GetForegroundWindow(), str, str.Capacity);

Console.WriteLine(str.ToString());
Console.ReadKey();
}
}

The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.

int GetWindowText(
HWND hWnd,