1.设置没有标题栏
this->setWindowFlags(Qt::FramelessWindowHint);
2.鼠标移动大小
void Dialog::resizeEvent(QResizeEvent *event)
{
ui->Button->setGeometry(0, 0, (82*this->width())/1366, (48*this->height())/728);
ui->Button->move((230*this->width())/1366, (90*this->height())/728);
}
bool Dialog::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(eventType);
MSG* pMsg = reinterpret_cast<MSG*>(message);
if(pMsg->message == WM_NCHITTEST){
int xPos = GET_X_LPARAM(pMsg->lParam) - this->frameGeometry().x();
int yPos = GET_Y_LPARAM(pMsg->lParam) - this->frameGeometry().y();
if(xPos > 0 && xPos < 8 && yPos > 0 && yPos < 6){
*result = HTTOPLEFT;
return true;
}
if(xPos > (this->width() - 8) && xPos < this->width() && yPos > 0 && yPos < 6){
*result = HTTOPRIGHT;
return true;
}
if(xPos > 0 && xPos < 8 && yPos > (this->height() - 8) && yPos < this->height() ){
*result = HTBOTTOMLEFT;
return true;
}
if(xPos > (this->width() - 8) && xPos < this->width() && yPos > this->height() - 8 && yPos < this->height()){
*result = HTBOTTOMRIGHT;
return true;
}
if(xPos > 0 && xPos < 8){
*result = HTLEFT;
return true;
}
if(xPos > (this->width() - 8) && xPos < this->width()&&yPos>20){
*result = HTRIGHT;
return true;
}
if(yPos > 0 && yPos < 6&&xPos<(this->width()-80)){
*result = HTTOP;
return true;
}
if(yPos > (this->height() - 8) && yPos < this->height()){
*result = HTBOTTOM;
return true;
}
}
return false;
}
3.设置背景图片
QPixmap pixmap = QPixmap("./images/bg.png").scaled(this->size());
QPalette palette (this->palette());
palette.setBrush(QPalette::Background, QBrush(pixmap));
this->setPalette(palette);
4.实现ESC键退出
void Dialog2nd::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Escape:
this.close();
break;
default:
break;
}
}
5.退出弹出确定框
void Dialog::closeEvent(QCloseEvent *event)
{
int ret = QMessageBox::information(this,tr("提示"),tr("你确定退出该软件?"),tr("确定"), tr("取消"),0,1);
if (ret == 0)
{
event->accept();
}
else if(ret==1)
{
event->ignore();
}
}