不可聚焦的子窗口



我有一个简单的VCL表单,这个表单位于注入的dll中。Dll注入到具有窗口的进程。我需要使我的窗体不可聚焦,并且总是在父窗口(注入过程的主窗口)之前。

表单创建:

procedure CreateForm;
var
  hWindow: THandle;
  Rect: TRect;
begin
  if GetProcessWindowHandle(GetCurrentProcessId, hWindow) then begin
    FormButtons := TFormButtons.Create(nil);
    GetWindowRect(hWindow, Rect);
    FormButtons.Left := Rect.Left + 50;
    FormButtons.Top  := Rect.Top;
    FormButtons.ShowModal;
    FormButtons.Free;
  end;
end;
procedure DLLEntryPoint(dwReason: DWORD);
begin
  case dwReason of
    DLL_PROCESS_ATTACH: begin
                          CreateForm;
                        end;
    DLL_PROCESS_DETACH: begin
                        end;
  end;
end;
begin
  DLLProc := @DLLEntryPoint;
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.

为表单创建参数:

procedure TFormButtons.CreateParams(var Params: TCreateParams);
var
  hWindow: THandle;
begin
  inherited CreateParams(Params);
  Params.ExStyle := Params.ExStyle or WS_EX_NOACTIVATE;
  if GetProcessWindowHandle(GetCurrentProcessId, hWindow) then
    Params.WndParent := hWindow;
end;

我所拥有的:

  1. 有了WS_EX_NOACTIVATE和没有Params.WndParent := hWindow,我的窗口(TFormButtons)是不可聚焦的,但这不是主窗口的子窗口,当主窗口激活时,我的窗户会留在主窗口下。呆在上面是个坏主意,我的窗户应该只在主窗户之前。

  2. 使用WS_EX_NOACTIVATE和wit Params.WndParent := hWindow,我的子VCL窗口有很好的z顺序,它总是在主窗口之前,但当激活我的窗口时,主窗口总是失去焦点

  3. 还有一个问题:如何在没有ShowModal但有Show的情况下显示我的VCL窗口。如果没有ShowModal,这是不可见的

可能有一个类助手处理WM_NCHITTEST表单的消息。

procedure TFormButtonsHelper.WMNCHitTest(var Msg: TWMNCHitTest);
begin
    if (Msg.Result <> HTHSCROLL) and (Msg.Result <> HTVSCROLL) then
        Msg.Result := HTCLIENT;
end;

终于找到了解决方案。我的表格从来没有得到关注,每次都放在主表格上,并随着主表格移动:

注入的dll:

var
  hWndHook: HHOOK = 0;
  hWndMain: THandle = 0;
  ProcessId: DWORD = 0;
  ThreadId: DWORD = 0;
function CallWndProc(Code: Integer; wParam: WPARAM; CWPStruct: PCWPStruct): LRESULT; stdcall;
var
  Rect: TRect;
begin
  Result := CallNextHookEx(hWndHook, Code, wParam, LPARAM(CWPStruct));
  if (Code = HC_ACTION) then begin
    case CWPStruct.message of
      WM_MOVE: if hWndMain > 0 then begin
        if Assigned(FormButtons) then begin
          GetWindowRect(hWndMain, Rect);
          SetWindowPos(FormButtons.Handle, HWND_TOPMOST, Rect.Left, Rect.Top - 10, 0, 0, SWP_NOACTIVATE or SWP_NOSIZE);
          SetWindowPos(FormButtons.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOMOVE);
        end;
      end;
      WM_ACTIVATE: if (hWndMain > 0) then begin
        if Assigned(FormButtons) then begin
          SetWindowPos(FormButtons.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOMOVE);
          SetWindowPos(FormButtons.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOMOVE);
        end;
      end;
      WM_CLOSE: if (hWndMain > 0) and (hWndMain = CWPStruct.hwnd) then begin
        try
            UnhookWindowsHookEx(hWndHook);
            hWndMain := 0;
        except
          MessageBox(0, 'Error: WM_CLOSE', '', MB_OK);
        end;
      end;
    end;
  end;
end;
function ThreadProc(Params: Pointer): Integer;
begin
  Result := 0;
  try
    ProcessId := GetCurrentProcessId;
    if GetProcessWindowHandle(ProcessId, hWndMain) then
      ThreadId := GetWindowThreadProcessId(hWndMain);
    if hWndHook = 0 then
      hWndHook := SetWindowsHookEx(WH_CALLWNDPROC, @CallWndProc, HInstance, ThreadId);
//    MessageBox(0, PChar('Hook installed!' + sLineBreak +
//                        'Process Id: ' + IntToStr(ProcessId) + sLineBreak +
//                        'Thread Id: ' + IntToStr(ThreadId) + sLineBreak +
//                        'hWndMain: ' + IntToStr(hWndMain) + sLineBreak +
//                        'hWndHook: ' + IntToStr(hWndHook)), '', MB_OK);
    FormButtons := TFormButtons.Create(nil);
    FormButtons.ShowModal;
    Result := 0;
  except
//    Result := ERROR_GEN_FAILURE;
  end;
end;
procedure DLLEntryPoint(dwReason: DWORD);
var
  hThread: THandle;
  ThreadId: UInt32;
begin
  case dwReason of
    DLL_PROCESS_DETACH: begin
//                          MessageBox(0, PChar('DLL_PROCESS_DETACH: ' + IntToStr(ProcessId)), '', MB_OK);
                        end;
    DLL_PROCESS_ATTACH: begin
                          hThread := BeginThread(nil, 0, ThreadProc, nil, 0, ThreadId);
                          if hThread <> 0 then
                            CloseHandle(hThread);
//                          MessageBox(0, PChar('DLL_PROCESS_ATTACH: ' + IntToStr(GetCurrentProcessId)), '', MB_OK);
                        end;
  end;
end;
begin
  DLLProc := @DLLEntryPoint;
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.

为表单创建参数:

procedure TFormButtons.CreateParams(var Params: TCreateParams);
var
  hWindow: THandle;
begin
  inherited CreateParams(Params);
  Params.ExStyle := Params.ExStyle or WS_EX_NOACTIVATE;
end;

相关内容

  • 没有找到相关文章

最新更新