궁극적으로 특정 윈도우가 존재하는지 확인하는 무언가를 작성하려고 시도하고있는 것입니다. FindWindow를 사용하여 리터럴 윈도우 이름을 찾을 수있었습니다.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 명령을 사용하여 정규식을 사용하여 확인하는 방법은 무엇입니까?
UPDATE ~~~~ 내가 가장 적합한 대답을 선택했는데 여기에 관심이있는 사람을 위해 어떻게 작동하는지 실제 코드가 나와 있습니다.
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 방법으로 테스트하는 방법을 알아 내야하지만 나중에 걱정 것이다.
하고, RegEx 테스트에 합격하면 FindWindow에 이름으로 전달한 다음 활성 창으로 설정했습니다. 내 솔루션에 대한 수정 된 OP를 참조하십시오. –