程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 學習 Message(2): 發送 WM_MOUSEMOVE 消息

學習 Message(2): 發送 WM_MOUSEMOVE 消息

編輯:Delphi

本例效果圖:

學習 Message(2): 發送 WM_MOUSEMOVE 消息

  代碼文件:

unit Unit1;

interface

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

type
 TForm1 = class(TForm)
  Panel1: TPanel;
  Button1: TButton;
  Button2: TButton;
  Button3: TButton;
  procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  procedure Button3Click(Sender: TObject);
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

{鼠標在 Panel1 中移動時, 我做了如下處理:}
{1、顯示鼠標在 Panel1 中的坐標}
{2、顯示是否同時按住了 Shift、Ctrl、Alt}
procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
 Y: Integer);
var
 List: TStringList;
begin
 List := TStringList.Create;
 if ssShift in Shift then List.Add('Shift');
 if ssCtrl in Shift then List.Add('Ctrl');
 if ssAlt  in Shift then List.Add('Alt');

 if List.Count > 0 then
  Panel1.Caption := Format('%s: %d, %d', [List.CommaText, X, Y])
 else
  Panel1.Caption := Format('%d, %d', [X, Y]);

 List.Free;
end;

{向 Panel1 發送 WM_MOUSEMOVE 消息}
{第一個消息參數是 0, 表示沒有按任何輔助鍵}
{第二個消息參數是 0, 相當於把鼠標移動到(0,0)坐標}
procedure TForm1.Button1Click(Sender: TObject);
begin
 Panel1.Perform(WM_MOUSEMOVE, 0, 0);
end;

{向 Panel1 發送 WM_MOUSEMOVE 消息}
{第二個消息參數在 WM_MOUSEMOVE 消息中表示鼠標坐標位置; 參數是32位整數, 低16位是X, 高16位是Y}
{這裡給的坐標是控件的中心點}
procedure TForm1.Button2Click(Sender: TObject);
var
 x,y,LParam: Integer;
begin
 x := Panel1.ClIEntWidth div 2;
 y := Panel1.ClIEntHeight div 2;
 LParam := y shl 16 or x;
 Panel1.Perform(WM_MOUSEMOVE, 0, LParam);
end;

{向 Panel1 發送 WM_MOUSEMOVE 消息}
{消息的第一個參數是表示正在按下哪個輔助鍵和鼠標的狀態}
{這裡發送的消息是: 按著 Shift 鍵, 鼠標移動到 (20,10) 處}
procedure TForm1.Button3Click(Sender: TObject);
const
 x = 20;
 y = 10;
begin
 Panel1.Perform(WM_MOUSEMOVE, MK_SHIFT, y shl 16 or x);
end;

end.

  窗體文件:

object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClIEntHeight = 198
 ClIEntWidth = 255
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 PixelsPerInch = 96
 TextHeight = 13
 object Panel1: TPanel
  Left = 8
  Top = 8
  Width = 237
  Height = 121
  Caption = 'Panel1'
  TabOrder = 0
  OnMouseMove = Panel1MouseMove
 end
 object Button1: TButton
  Left = 8
  Top = 152
  Width = 75
  Height = 25
  Caption = 'Button1'
  TabOrder = 1
  OnClick = Button1Click
 end
 object Button2: TButton
  Left = 89
  Top = 152
  Width = 75
  Height = 25
  Caption = 'Button2'
  TabOrder = 2
  OnClick = Button2Click
 end
 object Button3: TButton
  Left = 170
  Top = 152
  Width = 75
  Height = 25
  Caption = 'Button3'
  TabOrder = 3
  OnClick = Button3Click
 end
end



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved