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
    }
  • 相关阅读:
    day_2:re
    day_1:Requests
    CTF-PHP漏洞总结(持续更新)
    BugkuCTF-Web- flag在index里 80
    中缀表达式转换为后缀表达式(C语言实现)
    逆波兰计算器1.0 (c语言 栈实现)支持小数计算
    二进制数转化为十进制数(栈的学习练习)
    数据结构:统计学生信息(c++动态链表完成)
    数据结构:删除数组中的元素(c++)链表形式
    约瑟夫环 (c++循环链表方法书写)
  • 原文地址:https://www.cnblogs.com/sz-leez/p/6617437.html
Copyright © 2011-2022 走看看