更新的完整代码
我正在尝试一个简单的代码示例,它返回聚焦窗口的窗口句柄,但似乎无法取回文本框中的值。
class ApplicationExecution
{
public static bool MyWindow()
{
var activatedHandle = GetForegroundWindow();
if (activatedHandle == IntPtr.Zero)
{
return false; // No window is currently activated
}
var procId = Process.GetCurrentProcess().Id;
int activeProcId;
GetWindowThreadProcessId(activatedHandle, out activeProcId);
return activeProcId == procId;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
}
我试着像这样访问它
ApplicationExecution esma = new ApplicationExecution();
textbox1.text = esma;
也
textBox1.Text = Convert.ToString(ApplicationsWPF.ApplicationExecution.MyWindow());
我得到真或假,而不是函数应该返回的 GetForegroundWindow 值