赞
踩
作者: 一去、二三里
个人微信号: iwaleon
微信公众号: 高效程序员
在自定义无边框、标题栏的界面中,需要自己实现最小化、最大化、关闭、窗体背景等功能。最小化、最大化、关闭等按钮设计及功能比较简单,这里就不多做介绍。今天主要介绍一下绘制背景的问题,主要实现自适应屏幕分辨率。
先看一下UI设计的图(大小:1298*786):
如何自适应屏幕分辨率呢?下面是常用的一些方案:
下面,我们分别对它们一一进行分析:
综上所述:很明显,方案3是最好的,那么如何实现呢?请继续往下看!
由于界面存在缩放,所以如果窗体有圆角、或者存在阴影效果,缩放过程中会变形,所以需要进行特殊化处理!
现在,来开始我们的代码之旅吧!
void paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPixmap background(":/background");
int nLeftWidth = 144;
int nBottomHeight = 24;
int nTopHeight = 67;
// 分别计算左、上、右、下的区域
QRect left(0, 100, nLeftWidth, 100);
QRect right(background.width() - nLeftWidth, 100, nLeftWidth, 100);
QRect leftTop(0, 0, nLeftWidth, nTopHeight);
QRect rightTop(background.width() - nLeftWidth, 0, nLeftWidth, nTopHeight);
QRect top(150, 0, 150, nTopHeight);
QRect leftBottom(0, background.height() - nBottomHeight, nLeftWidth, nBottomHeight);
QRect rightBottom(background.width() - nLeftWidth, background.height() - nBottomHeight, nLeftWidth, nBottomHeight);
QRect bottom(150, background.height() - nBottomHeight, 100, nBottomHeight);
QRect center(300, 300, 100, 100);
QRect leftRect(0, nTopHeight, nLeftWidth, this->height() - nTopHeight - nBottomHeight);
QRect rightRect(this->width() - nLeftWidth, nTopHeight, nLeftWidth, this->height() - nTopHeight - nBottomHeight);
QRect leftTopRect(0, 0, nLeftWidth, nTopHeight);
QRect rightTopRect(this->width() - nLeftWidth, 0, nLeftWidth, nTopHeight);
QRect topRect(nLeftWidth, 0, this->width() - nLeftWidth*2, nTopHeight);
QRect leftBottomRect(0, this->height() - nBottomHeight, nLeftWidth, nBottomHeight);
QRect righttBottomRect(this->width() - nLeftWidth, this->height() - nBottomHeight, nLeftWidth, nBottomHeight);
QRect bottomRect(nLeftWidth, this->height() - nBottomHeight, this->width() - nLeftWidth*2, nBottomHeight);
QRect centerRect(nLeftWidth, nTopHeight, this->width() - nLeftWidth*2, this->height() - nTopHeight - nBottomHeight);
// 绘制图片
painter.drawPixmap(topRect, background, top);
painter.drawPixmap(leftRect, background, left);
painter.drawPixmap(rightRect, background, right);
painter.drawPixmap(rightTopRect, background, rightTop);
painter.drawPixmap(leftTopRect, background, leftTop);
painter.drawPixmap(leftBottomRect, background, leftBottom);
painter.drawPixmap(righttBottomRect, background, rightBottom);
painter.drawPixmap(bottomRect, background, bottom);
painter.drawPixmap(centerRect, background, center);
}
关于缩放处理,请参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。