Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Some example code that may help you with activating an Rdp remote application
- // Possibly incomplete as this is taken from a complete application but it hopefully will
- // help. If you have any improvements to suggest, email [email protected]
- // No copyright in the below code is claimed (though it is an original work based upon code from a number
- // of different sources).
- function FindRdpWindowHandleCallback(handle: THandle; lParam: LPARAM): Boolean; stdcall;
- function isMainWindow(handle: HWND): Boolean;
- begin
- result := (GetWindow(handle, GW_OWNER) = HWND(0)) and (IsWindowVisible(handle) or IsIconic(handle));
- end;
- var
- handleData: PHandleData;
- processId: ULONG;
- hwClassName: Array[0..255] of Char;
- begin
- if GetClassName(handle, hwClassName, SizeOf(hwClassName)) <> 0 then begin
- // TXxxYyyZzz is the class of a form created by the DLL, which manages communication
- // via the virtual channels.
- if (StrComp(hwClassName, 'TXxxYyyZzz') = 0) then
- // If we have an exact class name match, we have our window
- result := true
- else begin
- handleData := PHandleData(lParam);
- processId := 0;
- // If no exact match, try comparing process ids. If we have a match,
- // it is possibly the window we are looking for.
- GetWindowThreadProcessId(handle, &processId);
- if (handleData^.processId = processId) and isMainWindow(handle) then begin
- handleData^.bestHandle := handle;
- result := false;
- end else
- result := true
- end;
- end else
- result := true;
- end;
- function ForceForegroundWindow(lnHWND: HWND): Boolean;
- var
- nForeThread: Cardinal;
- nAppThread: Cardinal;
- begin
- nForeThread := GetWindowThreadProcessId(GetForegroundWindow(), NIL);
- nAppThread := GetCurrentThreadId();
- if nForeThread <> nAppThread then begin
- if AttachThreadInput(nForeThread, nAppThread, True) then begin
- if BringWindowToTop(lnHWND) then
- result := ShowWindow(lnHWND, 0)
- else
- result := False;
- AttachThreadInput(nForeThread, nAppThread, False);
- end else
- result := False;
- end else begin
- if BringWindowToTop(lnHWND) then
- result := ShowWindow(lnHWND, 0)
- else
- result := False;
- end;
- end;
- function TRdpChannel.GetRdpHandle: HWND;
- begin
- // Usually we have the Rdp window handle becaause we create the process
- if IsWindow(FRdpHandle) then
- result := FRdpHandle
- else begin
- // If the locally maintained Rdp handle no longer refers to a window, attempt to
- // locate an open RDP manager window (created by the DLL)
- GetWindowThreadProcessId(FCallManager.Handle, FHandleData.processId);
- FHandleData.bestHandle := 0;
- EnumWindows(@FindRdpWindowHandleCallback, NativeInt(@FHandleData));
- FRdpHandle := FHandleData.bestHandle;
- result := FRdpHandle;
- end;
- end;
- procedure TRdpChannel.ShowApplication;
- var
- h: HWND;
- hClassName: Array[0..255] of Char;
- begin
- h := GetRdpHandle;
- SetStatus(5, 'Show application called');
- if IsWindow(h) then begin
- if GetClassName(h, hClassName, SizeOf(hClassName)) <> 0 then begin
- // Attempts to restore a Delphi app, which has a 0 pixel main window
- // and apparently documentation says that it should be restored by
- // sending a WM_SYSCOMMAND message with SC_RESTORE as a parameter. Does
- // not always appear to work if the user clicks the minimise button on the
- // app main window.
- if (StrComp(hClassName, 'TApplication') = 0) then
- PostMessage(h, WM_SYSCOMMAND, SC_RESTORE, 0);
- end;
- if ShowWindow(h, SW_RESTORE) then begin
- if not SetForegroundWindow(h) then
- ForceForegroundWindow(h);
- end if ShowWindow(h, SW_SHOWDEFAULT) then begin
- if not SetForegroundWindow(h) then
- ForceForegroundWindow(h);
- end else begin
- if ShowWindow(h, SW_MINIMIZE) then begin
- if not OpenIcon(h) then
- ForceForegroundWindow(h);
- end else
- ForceForegroundWindow(h);
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment