有的时候我们要实现一个悬浮窗口,并使该窗口一直显示在桌面的工作区内。即整个窗口要一直显示在屏幕上,不能超出屏幕的上下左右边缘。此功能的实现也不难,我们需要自己写代码来响应窗口的WM_WINDOWPOSCHANGING消息,话不多说,详细代码如下供参考:
新建一个工程,并把下面代码拷贝到工程中,运行……
Delphi/Pascal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.WMWindowPosChanging(var Message: TWMWindowPosChanging); begin inherited; if (Message.WindowPos.y + Message.WindowPos.cy > Screen.WorkAreaHeight) then begin Message.WindowPos.Y := Screen.WorkAreaHeight - Height ; end; if (Message.WindowPos.x + Message.WindowPos.cx > Screen.WorkAreaWidth) then begin Message.WindowPos.X := Screen.WorkAreaWidth - Width ; end; if Message.WindowPos.x < 0 then Message.WindowPos.x := 0; if Message.WindowPos.y < 0 then Message.WindowPos.y := 0; end; end. |
http://bcoder.com/delphi/make-the-window-always-shows-in-desktop-workarea-in-delphi