使用 WM_NCHITTEST 消息判断鼠标所在窗口的部位

本例效果图:

使用 WM_NCHITTEST 消息判断鼠标所在窗口的部位
WM_NCHITTEST 消息返回后, 消息的 Result 参数表示了鼠标所在窗口的部位.

窗体设计步骤: 新建工程后, 随便添加一个菜单; 设置窗体的 AutoScroll 属性为 True, 并添加一个 Panel 放在合适的位置, 以让窗口出现滚动条.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Menus;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    MainMenu1: TMainMenu;
    mnuFile: TMenuItem;
  private
    procedure MyMsg(var msg: TWMNCHitTest); message WM_NCHITTEST;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  arr: array[-2..21] of string = (
    'HTERROR',
    'HTTRANSPARENT',
    'HTNOWHERE',
    'HTCLIENT - 客户区',
    'HTCAPTION - 标题',
    'HTSYSMENU - 系统菜单',
    'HTGROWBOX',
    'HTMENU - 菜单',
    'HTHSCROLL - 水平滚动条',
    'HTVSCROLL - 垂直滚动条',
    'HTMINBUTTON - 最小化按钮',
    'HTMAXBUTTON - 最大化按钮',
    'HTLEFT - 左边界',
    'HTRIG - 右边界',
    'HTTOP - 上边界',
    'HTTOPLEFT - 左上角',
    'HTTOPRIG - 右上角',
    'HTBOTTOM - 下边界',
    'HTBOTTOMLEFT - 左下角',
    'HTBOTTOMRIG - 右下角',
    'HTBORDER',
    'HTOBJECT',
    'HTCLOSE - 关闭按钮',
    'HTHELP');

procedure TForm1.MyMsg(var msg: TWMNCHitTest);
var
  i: Integer;
begin
  Inherited;
  i := msg.Result;
  Text := Format('%d: %s', [i, arr[i]]);
end;

end.

 
 
 
 
 

 

 
  

//另附 WM_NCHITTEST 消息中 Result 参数的所有可能值的列表:
HTERROR       = -2;
HTTRANSPARENT = -1;
HTNOWHERE     = 0;
HTCLIENT      = 1;
HTCAPTION     = 2;
HTSYSMENU     = 3;
HTGROWBOX     = 4;
HTSIZE        = HTGROWBOX;
HTMENU        = 5;
HTHSCROLL     = 6;
HTVSCROLL     = 7;
HTMINBUTTON   = 8;
HTMAXBUTTON   = 9;
HTLEFT        = 10;
HTRIGHT       = 11;
HTTOP         = 12;
HTTOPLEFT     = 13;
HTTOPRIGHT    = 14;
HTBOTTOM      = 15;
HTBOTTOMLEFT  = $10;
HTBOTTOMRIGHT = 17;
HTBORDER      = 18;
HTREDUCE      = HTMINBUTTON;
HTZOOM        = HTMAXBUTTON;
HTSIZEFIRST   = HTLEFT;
HTSIZELAST    = HTBOTTOMRIGHT;
HTOBJECT      = 19;
HTCLOSE       = 20;
HTHELP        = 21;

 
 
 
 
 

 

 
  

你可能感兴趣的:(使用 WM_NCHITTEST 消息判断鼠标所在窗口的部位)