How to show a window that acts like a popup menu?

余生长醉 提交于 2019-12-17 23:25:44

问题


When window A is show, I want to show another none-modal popup window B, but:

  1. I don't want window A to become inactive due to window B becomes the front window;
  2. I want that when window B is focused, I pull down a combo box control on window A with one click (generally you have to click twice, one for moving the focus to window A and the second for pulling down the combo box);

As you can see, the window B that I want is something like a more usable popup window like a popup menu (which is less a obstacle than a general none-modal window when you want it to get away by clicking on any other part of the parent window).

Am I clear on my question? Thank you.


回答1:


The easiest solution I had found for "1" is to send a WM_NCACTIVATE to the calling form as soon as the popup-form gets activated (in a WM_ACTIVATE handler), so that the calling form would draw its caption with the active colors. You'll have to have a reference for the calling form in the popup-form to achieve this.

For "2", you can release the popup form in the same WM_ACTIVATE handler, this won't eat the clicks that goes to the calling form.

So sth. like this should go to the popup-form;

type
  TForm2 = class(TForm)
    [..]
  private
    FOwner: TForm;
    procedure WmActivate(var Msg: TWMActivate); message WM_ACTIVATE;
  public
    constructor Create(AOwner: TComponent); override;

[...]
constructor TForm2.Create(AOwner: TComponent);
begin
  if not (AOwner is TForm) then
    raise Exception.Create('Owner should be TForm');
  FOwner := TForm(AOwner);
  inherited;
end;

procedure TForm2.WmActivate(var Msg: TWMActivate);
begin
  SendMessage(FOwner.Handle, WM_NCACTIVATE, Ord(Msg.Active <> WA_INACTIVE), 0);
  inherited;
  if Msg.Active = WA_INACTIVE then
    Release;
end;

and supply the calling form as the owner of the popup-form;

procedure TForm1.Button1Click(Sender: TObject);
var
  PopForm: TForm2;
begin
  PopForm := TForm2.Create(Self);
  [..]


FWIW, I agree with both loursonwinny and Ulrich. IMO a popup-form is more complicated then it seems. Though in the case of SpTBXFormPopupMenu you'd have to install two libraries, TB2K and SpTBXLib. At least browsing the sources could hint on what could get involved.


回答2:


In order to prevent a window from getting focus, you must either specify the WS_EX_NOACTIVATE extended window style (Windows 2000 and up) or handle WM_MOUSEACTIVATE and return MA_NOACTIVATE.




回答3:


I can use this to not lose focus:

SetWindowPos(Form2.Handle, HWND_TOP, 0, 0, 0, 0,
  SWP_SHOWWINDOW or SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOMOVE);

The second part I didn't understand very well.




回答4:


SpTBXFormPopupMenu from the spTBXLib does the job.

Go to http://www.silverpointdevelopment.com/sptbxlib/index.htm and look for "Form Popup"

The key seems to be that the container 'popuped' must inherit TPopupMenu. But the handling is very complex, you can see it by yourself in the code. My recommendation is to use it as is from the package because Robert Lee has done a wonderful job.




回答5:


I found the solution myself here. Thank you all!:

Keep Window Inactive In Appearance Even When Activated




回答6:


I found one that does almost exactly what I want: TAdvStickyPopupMenu



来源:https://stackoverflow.com/questions/2178494/how-to-show-a-window-that-acts-like-a-popup-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!