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 }
ps:本博文只用于日常备注及学习交流,请勿用于任何商业用途,涉及网摘等资料如有侵犯原作者权利,请联系确保及时更正!