求教,以下代码为啥挂起了?
unit Unit1;interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure sysmenu(var msg: twmmenuselect); message wm_syscommand;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
i := getsystemmenu(handle, false);
appendmenu(i, mf_separator, 0, nil);
appendmenu(i, mf_string, 100, '窗口置顶');
end;
procedure TForm1.sysmenu(var msg: twmmenuselect);
begin
if msg.IDItem = 100 then
SetWindowPos(Form1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE)
else
SetWindowPos(Form1.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
end;
end.
参考 https://stackoverflow.com/questions/38119347/why-is-a-window-not-coming-to-top-inspite-using-setwindowpos 明白了,
procedure TForm1.sysmenu(var msg: TWMMenuSelect);
begin
if msg.IDItem = 100 then
SetWindowPos(Form1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE)
else
inherited;
if msg.IDItem = 101 then
SetWindowPos(Form1.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE)
else
inherited;
end;
页:
[1]