다른 응용 프로그램을 실행하는 HTTP 서버 응용 프로그램 (데스크탑 응용 프로그램으로 실행되는 서비스 응용 프로그램)이 있으며 나중에 다른 응용 프로그램의 창을 활성화해야합니다. 컴퓨터 X는 모든 응용 프로그램을 포함합니다. 컴퓨터 X의 Opera 웹 브라우저에서 http://192.168.16.21:225/command/TTaskType.ttTestTask을 실행하면 내 WinActivate
기능이 작동합니다. 컴퓨터 Y에서 http://192.168.16.21:225/command/TTaskType.ttTestTask을 실행하면 창이 활성화되지 않습니다. 없음 디버그 출력이 오류를 반환합니다 (FLastErrorY가 인쇄됩니다). 나는 심지어 관리자로 HTTP 서버를 실행하려고했습니다. 그 밖의 무엇을 시도 할 수 있습니까?다른 응용 프로그램의 창 활성화 방법
function WinActivate(const AWinTitle: string): boolean;
var
_WindowHandle: HWND;
_KeyboardState: TKeyboardState;
begin
ResetError;
_WindowHandle := FindWindow(nil, PWideChar(AWinTitle));
FLastError := GetLastError;
SetCursorPos(10, 12);
OutputDebugString(PWideChar('FLastError1: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
if _WindowHandle <> 0 then
begin
//ShowWindow(_WindowHandle, SW_MINIMIZE);
if IsIconic(_WindowHandle) then
begin
ShowWindow(_WindowHandle, SW_RESTORE);
ResetError;
Result := IsIconic(_WindowHandle);
if Result then
Result := WinWaitActive(AWinTitle, 1000);
end
else
Result := SetForegroundWindow(_WindowHandle);
OutputDebugString(PWideChar('FLastErrorX: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
if not Result then
begin
FLastError := GetLastError;
OutputDebugString(PWideChar('FLastError2: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle])));
// Applications might lock focus, so, hack it around
GetKeyBoardState(_KeyboardState);
ShowWindow(_WindowHandle, SW_SHOWNORMAL);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, SPIF_UPDATEINIFILE);
if _KeyboardState[VK_MENU] <> 1 then
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY or 0, 0);
ResetError;
if not SetForegroundWindow(_WindowHandle) then
begin
FLastError := GetLastError;
if not SetForegroundWindow(_WindowHandle) then
begin
FLastError := GetLastError;
OutputDebugString(PWideChar('FLastErrorY: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
end;
end;
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
OutputDebugString(PWideChar('FLastError3: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
Result := FLastError = 0;
end;
if not Result then
begin
SetWindowPos(_WindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOMOVE);
SetWindowPos(_WindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOMOVE);
end;
end;
Result := WinWaitActive(AWinTitle, 1000);
OutputDebugString(PWideChar('Dabar'));
end;
['SetForegroundWindow()'] (https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539)를 읽으십시오. aspx) 설명서. 전경에 창문을 가져 오려면 여러 가지 조건이 충족되어야합니다. 또한'SetForegroundWindow()'와'keybd_event()'는 SetLastError()를 사용하여 에러를보고하지 않기 때문에'FLastError3'의'GetLastError()'의 값은 의미가 없습니다. –
오류 검사가 잘못되었습니다. 이전 호출이 실패한 경우에만 GetLastError를 호출하고 GetLastError가 의미있는 것을 반환한다고 문서에서 알려줍니다. 데스크톱 앱에서 코드를 실행 해보십시오. 아마이 코드는 세션 0에서 실행됩니다. –
@DavidHeffernan 어떤 응용 프로그램입니까? HTTP 서버 또는 윈도우를 활성화해야하는 서버입니까? 나중에 하나는 데스크톱 응용 프로그램입니다. –