拖动没有标题栏的窗体
方法一:处理WM_NCHITTEST消息,不过还需要自已处理标题栏系统按钮的事件,有点麻烦。
(1)在.h文件中添加WM_NCHITTEST处理函数
void __fastcall WMNchitTest(TMessage &Message);
(2)添加消息映射
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_NCHITTEST,TMessage,WMNchitTest)
END_MESSAGE_MAP(TForm)(3)编写消息处理代码
void __fastcall TForm1::WMNchitTest(TMessage &Message)
{
if(GetAsyncKeyState(VK_LBUTTON)<0)
Message.Result = HTCAPTION;
else
Message.Result = HTCLIENT;
}
方法二:通过OnMouseMove,OnMouseUp,OnMouseDown来使窗体即时移动
(1)定义bool IsDraging;全局变量来在OnMouseMove事件中判断是否需要进行窗体移动
void __fastcall TForm1::MouseMove(TObject *Sender,TMouseButton Button,TShiftState Shift,int X,int Y)
{
if(IsDraging)
{
this->Left+=X-xx;//移动当前窗体的X坐标
this->Top+=Y-yy;//移动当前窗体的Y坐标
}
}
(2)在OnMouseDown中将IsDraging设为true
void __fastcall TForm1::MouseDown(TObject *Sender,TMouseButton Button,TShiftState Shift,int X,int Y)
{
IsDraging = true;
xx = X;//全局变量,用来保存拖动的开始X坐标
yy = Y;//全局变量,用来保存拖动的开始Y坐标
}
(3)在OnMouseUp中将IsDraging设为false
void __fastcall TForm1::MouseUp(TObject *Sender,TMouseButton Button,TShiftState Shift,int X,int Y)
{
IsDraging = false;
}
方法三:向系统发送消息,告诉系统,鼠标现在在标题栏
void __fastcall TForm1::MouseDown(TObject *Sender,TMouseButton Button,TShitftState Shift,int X,int Y)
{
Refresh();
if(Button == mbLeft)
{
ReleaseCapture();
Perform(WM_SYSCOMMAND,0xf017,0);
}
}
相关资料:
(1)TApplicationEvents


浙公网安备 33010602011771号