私は最終的に、特定のウィンドウが存在するかどうかを確認し、それが存在するイベントでアクティブに設定する何かを書き込もうとしています。FindWindow を使用して、文字通りのウィンドウ名を見つけることができました。
int hWnd = FindWindow(null, "121226-000377 - company - Oracle RightNow CX Cloud Service");
if (hWnd > 0) //If found
{
SetForegroundWindow(hWnd); //Activate it
}
else
{
MessageBox.Show("Window Not Found!");
}
タイトルの先頭の数字は変化し、二度と同じになることはありません。そのため、正規表現を使用して、アクティブなウィンドウが上記のような名前構造を持っているかどうかを確認しようとしましたが、数字は変化する可能性があります。これに有効な正規表現がありますが、実装方法がわかりません。私は試した:
int hWnd = FindWindow(null, @"^\d+-\d+\s.*?RightNow CX");
if (hWnd > 0) //If found
{
SetForegroundWindow(hWnd); //Activate it
}
else
{
MessageBox.Show("Window Not Found!");
}
しかし、それは継続的に失敗します。では、FindWindow/SetForegroundWindow コマンドを使用して、正規表現を使用してチェックするにはどうすればよいでしょうか。
更新 ~~~~ 私はベストアンサーを選択しましたが、誰かが興味を持っている場合に備えて、これを機能させる方法の実際のコードを次に示します。
protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
{
int size = GetWindowTextLength(hWnd);
if (size++ > 0 && IsWindowVisible(hWnd))
{
StringBuilder sb = new StringBuilder(size);
GetWindowText(hWnd, sb, size);
Match match = Regex.Match(sb.ToString(), @"^\d+-\d+\s.*?RightNow CX",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
ActivateRNT(sb.ToString());
}
else
{
//this gets triggered for every single failure
}
//do nothing
}
return true;
}
private static void ActivateRNT(string rnt)
{
//Find the window, using the CORRECT Window Title, for example, Notepad
int hWnd = FindWindow(null, rnt);
if (hWnd > 0) //If found
{
SetForegroundWindow(hWnd); //Activate it
}
else
{
MessageBox.Show("Window Not Found!");
}
}
必要なウィンドウが存在しない場合に EnumWindows メソッドでテストしてアラートをスローする方法をまだ理解する必要がありますが、それについては後で考えます。