delphi7做一个页面的鼠标碰到按键,按键就移动,导致鼠标点击不到按键的代码
时间: 2024-10-15 10:21:07 浏览: 42
在Delphi 7中,你可以使用TControl的OnMouseMove事件来实现鼠标移动时控制元素的位置变化,同时阻止默认的鼠标点击事件传递给控件。下面是一个简单的示例:
```delphi
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Mouse;
type
TForm1 = class(TForm)
Button1: TButton; // 假设这是你的按钮
procedure FormCreate(Sender: TObject);
procedure Button1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Cursor := CM_SIZE; // 设置初始鼠标图标为可拖动
end;
// 当鼠标移动到按钮上时,更新其位置并防止默认的点击事件
procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
OldPos: TPoint; // 保存原始位置
begin
OldPos := Button1.ClientPoint; // 获取按钮当前位置
Button1.Left := X - Button1.Width / 2; // 根据新鼠标位置调整按钮左边距
Button1.Top := Y - Button1.Height / 2; // 调整顶部位置
// 阻止默认的鼠标点击事件,这样用户无法直接点击
if (X < Button1.Left + Button1.Width) and
(Y < Button1.Top + Button1.Height) then
begin
PreventMouseEvent(Button1.Handle, WM_LBUTTONDOWN, New mouseEvent(0, 0, X, Y, MA_ACTIVATE)); // 防止左键按下
PreventMouseEvent(Button1.Handle, WM_LBUTTONUP, New mouseEvent(0, 0, X, Y, MA_INACTIVE)); // 防止左键抬起
end;
end;
procedure PreventMouseEvent(hWnd: HWND; wParam, lParam: WPARAM; var newLParam: LPARAM);
begin
SetWindowLongPtr(hWnd, GWLP_MSGRESULT, GetWindowLongPtr(hWnd, GWLP_MSGRESULT) or MAKELONG(wParam, lParam.ToInt64));
end;
end.
```
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)