逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

From: http://blog.csdn.net/kfbyj/article/details/9284923


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

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

  1. 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. {  
  3.     Q_OBJECT  
  4. public:  
  5.     MainWindow(QWidget *parent = 0);  
  6.     ~MainWindow();  
  7.   
  8. protected:  
  9.     bool winEvent(MSG *message, long *result);  
  10. };  
CPP实现

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

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

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

因为

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

  1. xPos > 18 && xPos < 22 其实就是我们假定的边框了。  
  1.   
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2013-07-11 19:45 逛奔的蜗牛 阅读(3868) 评论(0)  编辑 收藏 引用 所属分类: Qt

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理