#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 设置标题
setWindowTitle(QStringLiteral("Qt之QDesktopWidget的使用说明(屏幕个数、屏幕分辩率、屏幕PDI、设置屏幕分辩率)"));
// 设置界面大小
resize(600, 300);
// 为了测试,先把界面放在左100,顶200位置上。
move(100, 200);
// 桌面操作作
m_pDeskdop = QApplication::desktop();
move((m_pDeskdop->width() - this->width())/2, (m_pDeskdop->height() - this->height())/2);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_4_clicked()
{
ui->textEdit->append(QString::number(m_pDeskdop->screenCount()));
}
void MainWindow::on_pushButton_3_clicked()
{
ui->textEdit->append(QString::number(m_pDeskdop->numScreens()));
}
void MainWindow::on_pushButton_2_clicked()
{
ui->textEdit->append(QString::number(m_pDeskdop->screenNumber()));
}
void MainWindow::on_pushButton_clicked()
{
ui->textEdit->append(QString::number(m_pDeskdop->primaryScreen()));
}
void MainWindow::on_pushButton_5_clicked()
{
ui->textEdit->append(QString::number(m_pDeskdop->screen()->width()));
ui->textEdit->append(QString::number(m_pDeskdop->screen()->height()));
}
int MainWindow::GetTaskbarInfo()
{
QRect deskRect = m_pDeskdop->availableGeometry();
QRect screenRect = m_pDeskdop->screenGeometry();
int nLocation = 1;
if (deskRect.bottom() != screenRect.bottom()) //任务栏在下
{
nLocation = 1;
}
else if (deskRect.top() != screenRect.top()) //任务栏在上
{
nLocation = 2;
}
else if (deskRect.left() != screenRect.left()) //任务栏在左
{
nLocation = 3;
}
else if (deskRect.right() != screenRect.right()) //任务栏在右
{
nLocation = 4;
}
else //任务栏处于自动隐藏状态
{
nLocation = 5;
}
return nLocation;
}
void MainWindow::GetTaskbarInfo(int &width, int &height, int &pos)
{
// 得到被定义的系统数据或者系统配置信息
int wx = GetSystemMetrics(SM_CXSCREEN);// 主显示器屏幕的宽度,以像素为单位。 这是通过调用GetDeviceCaps获得的相同值
int wy = GetSystemMetrics(SM_CYSCREEN);// 主显示器屏幕的高度,以像素为单位。
RECT rtWorkArea;
// 函数查询或设置系统级参数
SystemParametersInfo(SPI_GETWORKAREA, 0, &rtWorkArea, 0);// 工作区是指屏幕上不被系统任务条或应用程序桌面工具遮盖的部分
//1.任务栏停靠在左边情况
if (0 != rtWorkArea.left)
{
width = wx - rtWorkArea.right;
height = wy;
pos = 0;
return;
}
//2.任务栏停靠在上边情况
if(0!=rtWorkArea.top)
{
width = wx;
height = wy - rtWorkArea.bottom;
pos = 1;
return;
}
//3.任务栏停靠在右边情况
if(0==rtWorkArea.left && wx!=rtWorkArea.right)
{
width = wx - rtWorkArea.right;
height = wy;
pos = 2;
return;
}
//4.任务栏停靠在下边情况
if(0==rtWorkArea.top && wy!=rtWorkArea.bottom)
{
width = wx;
height = wy - rtWorkArea.bottom;
pos = 3;
return;
}
//5.任务栏自动隐藏的情况,这样其宽高都是0
if(0==rtWorkArea.left && 0==rtWorkArea.top
&& wx==rtWorkArea.right && wy==rtWorkArea.bottom)
{
width = 0;
height =0;
pos = 4;
return;
}
}
void MainWindow::on_pushButton_7_clicked()
{
ui->textEdit->append(QString::number(GetTaskbarInfo()));
}
void MainWindow::on_pushButton_6_clicked()
{
int w, h, pos;
GetTaskbarInfo(w, h, pos);
ui->textEdit->append(QStringLiteral("任务栏 宽:%1 高:%2 位置:%3").arg(w).arg(h).arg(pos));
}
void MainWindow::on_pushButton_8_clicked()
{
int nScreenWidth = ::GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = ::GetSystemMetrics(SM_CYSCREEN);
HWND hwd = ::GetDesktopWindow();
HDC hdc = ::GetDC(hwd);
int width = GetDeviceCaps(hdc, DESKTOPHORZRES);
int height = GetDeviceCaps(hdc, DESKTOPVERTRES);
double dWidth = (double)width;
double dScreenWidth = (double)nScreenWidth;
double scale = dWidth / dScreenWidth;
ui->textEdit->append(QStringLiteral("nScreenWidth:%1 nScreenHeight:%2 width:%3 height:%4 scale:%5")
.arg(nScreenWidth).arg(nScreenHeight)
.arg(width).arg(height)
.arg(scale));
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt之QDesktopWidget的使用说明(屏幕个数、屏幕分辩率、屏幕PDI、设置屏幕分辩率)
共6个文件
cpp:2个
ui:1个
h:1个
需积分: 1 4 下载量 33 浏览量
2022-09-15
17:58:23
上传
评论
收藏 17KB ZIP 举报
温馨提示
5.保证无毒 1.简单,方便,实用 3.实例可以自行改用 1.如有非法,本人无法律责任! 8.更多作品,查找标签“朱建强”7.下载,请杀毒! 4.如需联系我请看左边数字!1.如不知代表何物,那就放弃计算机吧! 0.还不懂?CSDN老板不让我上传联系方式。
资源详情
资源评论
资源推荐
收起资源包目录
Qt之QDesktopWidget的使用(屏幕个数、屏幕分辩率、屏幕PDI、设置屏幕分辩率).zip (6个子文件)
QDesktopWidget.pro 1KB
mainwindow.h 1KB
mainwindow.cpp 4KB
main.cpp 183B
mainwindow.ui 3KB
Qt之QDesktopWidget的使用(屏幕个数、屏幕分辩率、屏幕PDI、设置屏幕分辩率).png 16KB
共 6 条
- 1
zhujianqiangqq
- 粉丝: 30
- 资源: 388
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0