zoukankan      html  css  js  c++  java
  • Qt窗体引用window自带阴影边框效果

    <1>.工程pro文件添加Dwmapi.lib

    LIBS += Dwmapi.lib

    <2>.窗体控件添加系统函数

    #ifdef Q_OS_WIN  
    #include <Dwmapi.h>  // Use system shadow frame
    #endif
    
    TMainWindow::TMainWindow(QDialog *parent) 
        : QDialog(parent)
    {
    #ifdef Q_OS_WIN 
        BOOL bEnable = false;
        ::DwmIsCompositionEnabled(&bEnable);
        if (bEnable)
        {
            DWMNCRENDERINGPOLICY ncrp = DWMNCRP_ENABLED;
            ::DwmSetWindowAttribute((HWND)winId(), DWMWA_NCRENDERING_POLICY, &ncrp, sizeof(ncrp));
            MARGINS margins = { -1 };
            ::DwmExtendFrameIntoClientArea((HWND)winId(), &margins);
        }
    #endif    
    }

    <3>.边框拖拽功能

    #define MWS_SYS_DRAG_WIDTH 4      //边框响应鼠标间距
    #define FRAME_MINI_WIDTH   1024   //窗体默认最小宽度
    #define FRAME_MINI_HEIGHT  768    //窗体默认最小高度
    bool TMainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
    {
    #ifdef Q_OS_WIN
        MSG *msg = reinterpret_cast<MSG*>(message);
        switch (msg->message)
        {
            case WM_NCHITTEST:
            {
                QPoint p   = mapFromGlobal(QCursor::pos());
                int xPos   = p.x();
                int yPos   = p.y();
                int nHeigh = height();
                int nWidth = width();
    
                *result = HTNOWHERE;
    
                if (!isFullScreen() && !isMaximized())
                {
                    if (xPos >= 0 && xPos < MWS_SYS_DRAG_WIDTH){
                        if (yPos >= 0 && yPos < MWS_SYS_DRAG_WIDTH)
                        {
                            *result = HTTOPLEFT;
                        }
                        else if (yPos >= (nHeigh - MWS_SYS_DRAG_WIDTH) && yPos <= nHeigh)
                        {
                            *result = HTBOTTOMLEFT;
                        }
                        else
                        {
                            *result = HTLEFT;
                        }
                    }
    
                    if (xPos >= nWidth - MWS_SYS_DRAG_WIDTH && xPos <= nWidth)
                    {
                        if (yPos >= 0 && yPos <= MWS_SYS_DRAG_WIDTH)
                        {
                            *result = HTTOPRIGHT;
                        }
                        else if (yPos >= (nHeigh - MWS_SYS_DRAG_WIDTH) && yPos <= nHeigh)
                        {
                            *result = HTBOTTOMRIGHT;
                        }
                        else
                        {
                            *result = HTRIGHT;
                        }
                    }
    
                    if (xPos >= MWS_SYS_DRAG_WIDTH && xPos < nWidth - MWS_SYS_DRAG_WIDTH
                        && yPos > 0 && yPos < MWS_SYS_DRAG_WIDTH)
                    {
                        *result = HTTOP;
                    }
    
                    if (xPos > MWS_SYS_DRAG_WIDTH && xPos < nWidth - MWS_SYS_DRAG_WIDTH
                        && yPos >(nHeigh - MWS_SYS_DRAG_WIDTH) && yPos < nHeigh)
                    {
                        *result = HTBOTTOM;
                    }
                }
                if (HTNOWHERE == *result)
                {
                    return false;
                }
                return true;
            }
            break;
    
            case WM_GETMINMAXINFO:
            {
                MINMAXINFO *mmi = (MINMAXINFO*)(msg->lParam);
    
                QRect desktop     = qApp->desktop()->availableGeometry(this);
                QRect desktopRect = qApp->desktop()->screenGeometry(this);
    
                mmi->ptMaxSize.x = desktop.width();
                mmi->ptMaxSize.y = desktop.height();
    
                int desktopLeft = desktop.left() - desktopRect.left();
                int desktopTop  = desktop.top() - desktopRect.top();
    
                mmi->ptMaxPosition.x = desktopLeft;
                mmi->ptMaxPosition.y = desktopTop;
    
                mmi->ptMinTrackSize.x = FRAME_MINI_WIDTH;
                mmi->ptMinTrackSize.y = FRAME_MINI_HEIGHT;
    
                mmi->ptMaxTrackSize.x = desktop.width();
                mmi->ptMaxTrackSize.y = desktop.height();
    
                result = 0;
    
                return true;
            }
            break;
    
            case WM_SIZE:
                switch (msg->wParam)
                {
                case SIZE_MAXIMIZED:
                    break;
                case SIZE_RESTORED:
                    break;
                }
                break;
        }
        return QWidget::nativeEvent(eventType, message, result);
    #else
        return QWidget::nativeEvent(eventType, message, result);    
    #endif
    }
  • 相关阅读:
    bt5设置IP
    flyCoding
    [Cocoa][译]苹果 Cocoa 编码规范中文版
    [BZOJ4569] [Scoi2016]萌萌哒
    BZOJ4899]记忆的轮廓
    [BZOJ1701] [Usaco2007 Jan]Cow School牛学校
    [Poi2011]Lightning Conductor
    [BZOJ4709] [Jsoi2011] 柠檬
    决策单调性优化dp 专题练习
    2369. 区间
  • 原文地址:https://www.cnblogs.com/sz-leez/p/6617437.html
Copyright © 2011-2022 走看看