QT中異形視窗的繪製(winEvent處理WM_NCHITTEST訊息)

來源:互聯網
上載者:User

標籤:

  這裡討論的只是Windows平台上的實現。
  在QT中繪製異形視窗,只要設定 windowFlag 為 CustomizeWindowHint,再結合setMask()就可以做出各種奇形怪狀的視窗。相對來說比較麻煩的, 是進行視窗拖動和縮放的處理。
  在 Windows SDK 和 MFC 中比較容易,只要處理 WM_NCHITTEST,返回相應的測試值就可以了。幸運的是,QT中也提供了直接處理各平台訊息的方法,在 Windows下只需要重載winEvent方法。

  下面給出了範例程式碼:

// include <windows.h>bool MyDialog::winEvent(MSG* msg, long* result){    const int captionHeight = 25;    const int frameWidth = 6;        if (msg->message != WM_NCHITTEST) return false;    QPoint pos = mapFromGlobal(QCursor::pos());    int w = width();    int h = height();    if (QRect(frameWidth, captionHeight, w-frameWidth-frameWidth, h-captionHeight-frameWidth).contains(pos))    {        *result = HTCLIENT;    }    else if (QRect(0, 0, w, captionHeight).contains(pos))    {        *result = HTCAPTION;    }    else if (QRect(0, captionHeight, frameWidth, h-captionHeight-frameWidth).contains(pos))    {        *result = HTLEFT;    }    else if (QRect(w-frameWidth, captionHeight, frameWidth, h-captionHeight-frameWidth).contains(pos))    {        *result = HTRIGHT;    }     else if  (QRect(frameWidth, h-frameWidth, w-frameWidth-frameWidth, frameWidth).contains(pos))    {        *result = HTBOTTOM;    }     else if (QRect(0, h-frameWidth, frameWidth, frameWidth).contains(pos))    {        *result = HTBOTTOMLEFT;    }    else if (QRect(w-frameWidth, h-frameWidth, frameWidth, frameWidth).contains(pos))    {        *result = HTBOTTOMRIGHT;    }        return true;}

參考:http://www.cppblog.com/eXile/archive/2007/12/09/38084.html

QT中異形視窗的繪製(winEvent處理WM_NCHITTEST訊息)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.