更新された完全なコード
フォーカスされたウィンドウのウィンドウ ハンドルを返す単純なコード サンプルを試していますが、テキスト ボックスの値を取得できないようです。
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 値ではなく、true または false のいずれかを取得します。