功能描述:单鼠标移动到特定区域,自动弹出一个工具栏或者菜单,鼠标移动到工具栏上,选择相应的内容,若未作出选择,单鼠标离开新弹出的工具栏,该工具栏自动关闭,无需鼠标单击即能自动关闭,采用了Hook随时获取鼠标坐标,该方法如果热点离弹出位置较远的话,可以增加一个时钟延长二级工具栏的存在时间,以便鼠标滑入工具栏区域。
下载源代码
// 头文件
//---------------------------------------------------------------------------
#ifndef unitMainH
#define unitMainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TformMain : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TPanel *Panel1;
TPanel *Panel2;
TEdit *Edit2;
TPanel *Panel3;
TSpeedButton *SpeedButton1;
TSpeedButton *SpeedButton2;
TSpeedButton *SpeedButton3;
TSpeedButton *SpeedButton4;
TSpeedButton *SpeedButton5;
TSpeedButton *SpeedButton6;
private: // User declarations
int SpeedButton4X,SpeedButton4Y;
int Panel2X,Panel2Y;
public: // User declarations
__fastcall TformMain(TComponent* Owner);
__fastcall ~TformMain();
};
//---------------------------------------------------------------------------
extern PACKAGE TformMain *formMain;
//---------------------------------------------------------------------------
#endif
--------------------------------------------------------------------------------------------------------------------------------------------------
// CPP 文件
//---------------------------------------------------------------------------
#include <vcl.h>
#include <psapi.h>
#include <SysUtils.hpp>
#pragma hdrstop
#include "unitMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TformMain *formMain;
//---------------------------------------------------------------------------
HHOOK hHook;
//---------------------------------------------------------------------------
LRESULT CALLBACK LowLevelMouseProc(int code, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
__fastcall TformMain::TformMain(TComponent* Owner)
: TForm(Owner)
{
// 创建 Hook
hHook = SetWindowsHookEx(WH_MOUSE_LL, (HOOKPROC) LowLevelMouseProc, GetModuleHandle(NULL), 0);
// 隐藏需要弹出的菜单
Panel2->Visible = false;
}
//---------------------------------------------------------------------------
__fastcall TformMain::~TformMain()
{
UnhookWindowsHookEx(hHook);
}
//---------------------------------------------------------------------------
LRESULT CALLBACK LowLevelMouseProc(int code, WPARAM wParam, LPARAM lParam)
{
// Hook 得到实时鼠标位置
// 创建一个坐标结构
TPoint Point;
// API 得到一个鼠标位置
GetCursorPos(&Point);
int x = Point.x;
int y = Point.y;
// 输出到 Edit (调试用)
formMain->Edit1->Text = IntToStr(x) + ", " + IntToStr(y);
{
// 控制 Panel
// Panel3 的绝对坐标
TPoint pointPanel3;
ClientToScreen((HWND)formMain->Panel3->Handle, &pointPanel3);
// Panel2 的绝对坐标
TPoint pointPanel2;
ClientToScreen((HWND)formMain->Panel2->Handle, &pointPanel2);
// 鼠标如果在 Panel3 区域内
if(x >= pointPanel3.x && y >= pointPanel3.y
&& x <= pointPanel3.x + formMain->Panel3->Width && y <= pointPanel3.y + formMain->Panel3->Height)
{
// 显示 Panel2
if (formMain->Panel2->Visible == false)
{
formMain->Panel2->Visible = true;
}
}
// 鼠标如果处于 Panel2 范围内
else if(x >= pointPanel2.x && y >= pointPanel2.y
&& x <= pointPanel2.x + formMain->Panel2->Width && y <= pointPanel2.y + formMain->Panel2->Height)
{
// 正在显示菜单
}
// 不在任何指定范围内
else
{
// 关闭菜单
if (formMain->Panel2->Visible == true)
{
formMain->Panel2->Visible = false;
}
}
// 取得坐标
int t1 = pointPanel2.x + formMain->Panel2->Width;
int t2 = pointPanel2.y + formMain->Panel2->Height;
// 坐标输出 (调试用)
formMain->Edit2->Text = IntToStr(t1) + ", " + IntToStr(t2);
}
// 进行下一次捕获
return CallNextHookEx(hHook, code, wParam, lParam);
}
//---------------------------------------------------------------------------
下载源代码