鼠标和键盘控制

     阅读 529 次    更新时间:2014/4/18    
鼠标和键盘控制

鼠标和键盘控制




刚才用鼠标击了哪一个对象。 
  在Win95中,鼠标的右键起到了很大的作用,但是,由于历史的原因,对于右键的使用即使在Delphi中,也还不够有效,下面的程序可以告诉你如何知道刚才鼠标右击的对象名称。首先建立一个popmenu,然后以下的代码就可以告诉你刚才右击的对象名称:PopupMenu1.PopupComponent.ClassName。

察看Shift建是否被按下 
  方法:
      if Shift>=[ssShift] then ShowMessage('OK');

链接的视觉效果 
  要有类似WIN98那样指向字体,该字体就出现下划线,鼠标指针变为一支手指,按下后就打开浏览器或邮件编写器的功能,按下列办法做:
  先在一个窗体中加入一个Label1,加入下列代码
procedure TForm1.Label1MouseMove(Sender: TObject;shift:Tshiftstate;x,y:integer);
begin
Label1.Font.Style:=[fsbold,fsunderline];
Label1.Font.Color:=clYellow;
end;
procedure TForm1.FormMouseMove(Sender: TObject;shift:Tshiftstate;x,y:integer);
begin
Label1.Font.Style:=[fsbold];
Label1.Font.Color :=clmaroon;
end;
procedure TForm1.Label1Click(Sender: TObject);
begin
Shellexecute(handle,nil,pchar('mailto:guihong@163.net'),nil,nil,sw_shownormal);
end;
再将Label1.Cursor设为crHandPoint,Label1的OnMouseMove事件设为Label1MouseMove, Form1的OnMouseMove事件设为FormMouseMove,Label1的OnClick事件设为Label1Click,那么就这种效果了。

如何知道刚才鼠标右击的对象名称 
在Win95中,鼠标的右键起到了很大的作用,但是,由于历史的原因,对于右键的使用即使在Delphi中,也还不够有效,下面的程序可以告诉你如何知道刚才鼠标右击的对象名称。首先建立一个popmenu,然后以下的代码就可以告诉你刚才右击的对象名称:
PopupMenu1.PopupComponent.ClassName

模拟按键 
让 WIN95 模拟按了一个按键,例如按下 ENTER或者 TAB 键?
PostMessage(Object.Handle, WM_KEYDOWN, VK_TAB, 0);

程序中使用自定义的鼠标 
一.建立工程与一个资源档
用Image Editor编辑一个鼠游标
  (Fild | New | Resource File)
新建一个 CURSOR_1 的 CURSOR, 设定好它的 Hot Spot
  (Cursor | Set Hot Spot)
存档时注意要和建立的Project存在同一个目录
在本例我们先假定为 MyCursor.res
二. 程序部分
定义一个常数crMyCursor, 这个常数您必须设成大於零
的任何整数, 以 LoadCursor() 函数将自订的鼠标资源
load 进来, 以下为源代码:
// unit.pas
unit Unit1;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes,
Graphics, Controls, Forms, Dialogs;
const
crMyCursor = 1; (* 宣告一个常数 *)
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
{$R mycursor.res}//这行$R不可少, 否则自订的鼠游标就出不来了
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
//将鼠标资源 load 进来
Screen.Cursors[crMyCursor] := LoadCursor (hInstance,'CURSOR_1');
Cursor := crMyCursor;//指定 form1 的 cursor 为自订鼠标
Button1.Cursor := crMyCursor;//指定Button1的cursor为自订鼠标
end;
end.

将鼠标锁定在一定范围 
如何将鼠标锁定在一定的范围内呢?
请在Form中放置二个 Button, 然後分别为这两个按钮定义OnClick响应事件如下:
// 限制
procedure TForm1.Button1Click(Sender: TObject);
var
rtButton2: TRect;
begin
rtButton2 := Button2.BoundsRect;
MapWindowPoints(handle, 0, rtButton2, 2); // 座标换算
ClipCursor(@rtButton2); // 限制鼠标移动区域
end;
// 还原
procedure TForm1.Button2Click(Sender: TObject);
var
rtScreen: TRect;
begin
rtScreen := Rect(0, 0, Screen.Width, Screen.Height);
ClipCursor(@rtScreen);
end;

怎样识别鼠标在DBGRID的那一行标题上按下了? 
How To indentify MouseDown On DBGrid Label?
I have a problem i can't find a way to know on witch column on the title
of a DBGrid the mouse was pressed (or DoubleClick).
回答:
Use the OnTitleClick event handler to detect the click. Then Column.ID
will give you the column number.

发出一个Alt+Down 组合键 
问:Send [Alt]+[Down]?
答:
Use the keybd_event API function to fake keyboard events. Note that each
key down event needs a matching key up or you mess up the key state array.
  keybd_event( VK_MENU, MapVirtualKey( VK_MENU, 0 ), 0 , 0 );
    // Alt down
  keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN, 0 ), 0 , 0 );
    // down arrow key down
  keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN, 0 ), KEYEVENTF_KEYUP , 0 );
    // down arrow key up
  keybd_event( VK_MENU, MapVirtualKey( VK_MENU, 0 ), KEYEVENTF_KEYUP , 0 );
    // Alt key up
These key events will go to the window currently having the input focus.
 
 
 

Copyright 2003-2008 All Rights Reserved 自由风工作室 版权没有 [湘ICP备06002185号]
.