Qt 无标题无边框程序的拖动和改变大小

本文出自:http://blog.csdn.net/kfbyj/article/details/9284923

最近做项目遇到的问题,总结下。

有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的标题栏以及最大化、最小化、关闭,菜单按钮,我们就需要

  1. setWindowFlags(Qt::FramelessWindowHint); 
setWindowFlags(Qt::FramelessWindowHint);
但是这样过后,我们就不能拖动窗口改变位置,以及拖动边缘改变窗口大小了。

有两种方案处理这种情况:

1.自己对mouseMoveEvent,mousePressEvent,mouseReleaseEvent 等事件进行处理。

2.Qt可以处理windows的消息。大家重新实现bool winEvent(MSG *message, long *result);(在此又一次感觉Qt的NB)


我刚开始使用第一种方法去实现的。移动窗口很容易做,大家可以去看看这个大大写的,比网上其他版本问题少些。

http://blog.csdn.net/aqtata/article/details/8902889

在窗口边缘按下鼠标拖动改变窗口大小就比较麻烦了。

我是这样做的:

在mousePressEvent 按下设置m_bPressed为真。

在mouseMoveEvent中m_bPressed为真且event->x() 在窗口边缘 及处理算出鼠标移动的增量 然后不断resize窗口。

至于如何为边缘的断定,就自己设定一个 差值 假如 在窗口边缘 ±4个px 就算在在该边缘就处理该resize。

这样做缺点很多,1.拖快了不行,很容易超过该差值 , 2.窗口抖动的厉害,一直在resize,3.要处理太多情况


鉴于上诉缺点于是乎就到处问人百度google。有了第二种方法:

第二种方法很好用,效果和有标题边框程序一样~~~

Qt居然可以处理windows消息。。

这里我们要重新实现winEvent ( MSG * message, long * result )

该虚函数在QWidget和QWizard以及QSizeGrip以及他们的子类中都可以实现。

如果你想停止Qt处理消息就返回true,并且设置result到你想要保存的值返回给window处理。否者的话返回false。

这里我们主要想处理WM_NCHITTEST消息。

The WM_NCHITTEST message is sent to a window in order to determine what part of the window corresponds to a particular screen coordinate. This can happen, for example, when the cursor moves, when a mouse button is pressed or released, or in response to a call to a function such as WindowFromPoint. If the mouse is not captured, the message is sent to the window beneath the cursor. Otherwise, the message is sent to the window that has captured the mouse.

A window receives this message through its WindowProc function.

WM_NCHITTEST的消息响应函数会根据鼠标当前的坐标来判断鼠标命中了窗口的哪个部位,消息响应函数的返回值指出了部位,例如它可能会返回HTCAPTION,或者HTCLIENT等。(其返回值有很多,请查阅MSDN)。

知道这个就好了,我们还是要判断下鼠标的位置,然后通过该位置保存到result给window处理。

其实就是我们的程序没有边框不能发送这些消息,我们把它告诉windows,然后windows帮我们处理拖动,改变大小等效果。所以效果和有边框有标题程序效果一样的。

头文件申明:

  1. class MainWindow : public QMainWindow 
  2.     Q_OBJECT 
  3. public
  4.     MainWindow(QWidget *parent = 0); 
  5.     ~MainWindow(); 
  6.  
  7. protected
  8.     bool winEvent(MSG *message,long *result); 
  9. }; 

CPP实现

  1. bool MainWindow::winEvent(MSG *message,long *result) 
  2.     switch(message->message) 
  3.     { 
  4.     case WM_NCHITTEST: 
  5.         int xPos = GET_X_LPARAM(message->lParam) -this->frameGeometry().x(); 
  6.         int yPos = GET_Y_LPARAM(message->lParam) -this->frameGeometry().y(); 
  7.         if(this->childAt(xPos,yPos) == 0) 
  8.         { 
  9.             *result = HTCAPTION; 
  10.         }else
  11.             return false
  12.         } 
  13.         if(xPos > 18 && xPos < 22) 
  14.             *result = HTLEFT; 
  15.         if(xPos > (this->width() - 22) && xPos < (this->width() - 18)) 
  16.             *result = HTRIGHT; 
  17.         if(yPos > 18 && yPos < 22) 
  18.             *result = HTTOP; 
  19.         if(yPos > (this->height() - 22) && yPos < (this->height() - 18)) 
  20.             *result = HTBOTTOM; 
  21.         if(xPos > 18 && xPos < 22 && yPos > 18 && yPos < 22) 
  22.             *result = HTTOPLEFT; 
  23.         if(xPos > (this->width() - 22) && xPos < (this->width() - 18) && yPos > 18 && yPos < 22) 
  24.             *result = HTTOPRIGHT; 
  25.         if(xPos > 18 && xPos < 22 && yPos > (this->height() - 22) && yPos < (this->height() - 18)) 
  26.             *result = HTBOTTOMLEFT; 
  27.         if(xPos > (this->width() - 22) && xPos < (this->width() - 18) && yPos > (this->height() - 22) && yPos < (this->height() - 18)) 
  28.             *result = HTBOTTOMRIGHT; 
  29.  
  30.         return true
  31.     } 
  32.     return false

把各种边界情况保存到result给windows处理,我们就省去很多事情,我想windows肯定比我们自己实现的效果要好多了。

以上的18 以及 22 是我对程序的边缘进行判断的范围。

因为

我做了边框阴影。阴影边框设定为20px所以在

  1. xPos > 18 && xPos < 22 其实就是我们假定的边框了。 
xPos > 18 && xPos < 22 其实就是我们假定的边框了。

以上是转载别人的日志,自己试验得出一些结果做一下记载:

这里注意: 要实现在window下的拖动边缘改变窗口大小了,只需要实现winEvent这个函数即可,这里是这是边框位置为20,我们可以设置为2则xPos > 0 && xypos < 4即可,如果用setupUi()来加载控件则不能实现效果,具体为什么没有研究

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值