实现的功能是:程序运行时,在窗体上的某一区域(可以自己指定)有几个StaticText控件,我要把此区域的任何一个StaticText控件都可以移动到窗体中我定义的另外一个区域,不能超出此区域。其实现方式如下:
bool Word_MouseDown; //定义的全局变量
int Word_OldX,Word_OldY; //全局变量
//Arragge1 是 StaticText控件的名字
//------------------------------------------------------------------------------
void __fastcall TWord_Arg::Arrage1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if (Button == mbLeft)
{
Word_MouseDown = true;
Word_OldX = X;
Word_OldY = Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TWord_Arg::Arrage1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if (Word_MouseDown)
{
TWinControl *c = dynamic_cast(Sender);
if (c)
{
c->Left += X - Word_OldX;
c->Top += Y - Word_OldY;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TWord_Arg::Arrage1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
Word_MouseDown = false;
}
其他的StaticText控件的 MouseDown MouseMove MouseUp事件的响应时一样的。
若要实现把StaticText 控件移动到指定的区域,用鼠标Hook来实现。最开始用坐标来实现,不行。上面的事件响应过程中的坐标是相对坐标,与你的控件的宽度和高度有关的。鼠标Hook实现的方式:
//---------------------------------------------------------------------------
#include
#include
#include
#pragma hdrstop
#pragma argsused
HHOOK hHook = NULL;
HINSTANCE gInst = NULL;
//BOOL __stdcall EnableHook(BOOL Enabled,int Top,int Down);
extern "C" __declspec(dllexport) BOOL __stdcall EnableHook(BOOL Enabled,int Top,int Down);
int iTop,iDown;
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
if (reason==DLL_PROCESS_ATTACH)
gInst = hinst;
else
if(reason==DLL_PROCESS_DETACH)
EnableHook(FALSE,0,0);
return 1;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK MouseFun(int nCode,WPARAM wParam,LPARAM lParam)
{
HWND hWnd;
DWORD dwID;
MOUSEHOOKSTRUCT* pms;
POINT pt;
if(nCode<0)
return CallNextHookEx(hHook,nCode,wParam,lParam);
pms = (MOUSEHOOKSTRUCT*)lParam;
if (pms->pt.x >= Screen->Monitors[0]->Width)
{
if (HIBYTE(GetKeyState(VK_LBUTTON)))
{
if (pms->pt.y > iDown)
SetCursorPos(pms->pt.x,iDown);
if (pms->pt.y < iTop)
SetCursorPos(pms->pt.x,iTop);
}
}
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) BOOL __stdcall EnableHook(BOOL Enabled,int Top,int Down)
{
char buf[33];
iTop = Top;
iDown = Down;
if(Enabled)
{
if(hHook == NULL)
hHook = SetWindowsHookEx(WH_MOUSE,(FARPROC)MouseFun,gInst,0);
return hHook!=NULL;
}
else
{
if(hHook && UnhookWindowsHookEx(hHook))
hHook = NULL;
return hHook == NULL;
}
}
把生成的DLL文件放在你建的工程同一目录下,在应用程序的头部加入下面几行代码:
HINSTANCE inst; //指向鼠标hook结构
typedef void __stdcall (*DLLFUN)(BOOL,int,int);
DLLFUN SetHook;
在窗体的 OnShow事件中加入:
uper = Screen->Monitors[1]->Height * 7/12 ;
lower = Screen->Monitors[1]->Height * 5/12;
//加载同一目录下的指定连接文件--mydll.dll
inst = LoadLibrary("MouseHook.dll");
if(inst==NULL)
{
ShowMessage("DLL: 不能加载!程序退出。");
exit(1);
}
//取得dll中的SetHook函数地址
SetHook = (DLLFUN)GetProcAddress(inst,"EnableHook");
if(SetHook==NULL)
{
ShowMessage("DLL: 函数没找到!程序退出。");
FreeLibrary(inst);
exit(1);
}
//执行指向dll中的SetHook函数
SetHook(TRUE,lower,uper);
其中uper 和 lower 是传入Hook的参数。uper 是定义的区域的上边界,lower是下边界。这样我们的功能就实现了。
阅读(2634) | 评论(0) | 转发(0) |