Qt製作雞你太美鬼畜桌面小工具
阿新 • • 發佈:2022-11-29
作為一名成熟的ikun,守護坤坤是我的使命,同時,作為一名程式設計師,總覺得需要做點什麼來對錶達對坤坤的仰慕之情。
於是,我利用Qt製作了一個守護坤坤的桌面小工具,它可以根據鍵盤上的 'j' 'n' 't' 'm' 'ctrl + c' 'ctrl + t' 'ctrl + r' 'ctrl + l' " ",等按鍵發出雞、你、太、美、唱、跳、rap、籃球、你幹嘛的音效,並伴隨文字顯示,同時支援桌面拖動與系統托盤圖示顯示;
原始碼如下:
程式中使用了 QSystemTrayIcon、QAction、QMenu 對視窗進行托盤圖示設定,QMouseEvent對滑鼠按下、雙擊、託動、鬆開等狀態進行事件處理,QPainter對ikun圖片進行繪製,QSound對音訊進行播放,QKeyEvent對鍵盤按鍵事件進行處理,QTimer對文字氣泡顯示時間定時,QLabel對文字進行展示;
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QSystemTrayIcon> #include <QAction> #include <QMenu> #include <QMouseEvent> #include <QPainter> #include <QSound> #include <QDebug> #include <QKeyEvent> #include <QTimer> #include <QLabel> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); QSystemTrayIcon* mSysTrayIcon; QMenu *mMenu; QAction *mShowMainAction; QAction *mExitAppAction; QAction *mHideAppAction; void createActions(); void createMenu(); void paintEvent(QPaintEvent *); // 滑鼠按下事件 void mousePressEvent(QMouseEvent *event); // 滑鼠移動事件 void mouseMoveEvent(QMouseEvent *event); // 滑鼠釋放事件 void mouseReleaseEvent(QMouseEvent *event); // 滑鼠雙擊事件 void mouseDoubleClickEvent(QMouseEvent *event); //鍵盤按下事件 void keyPressEvent(QKeyEvent *event); ~Widget(); private slots: void on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason); void on_showMainAction(); void on_exitAppAction(); void myTimerOut(); private: bool m_bDrag; bool m_bDrag1; QPoint mouseStartPoint; QPoint windowTopLeftPoint; Ui::Widget *ui; QSound *sound; QPoint p; QTimer *Timer; QLabel *label; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); m_bDrag = false; sound = new QSound(":/Res/雞.wav",this); Timer = new QTimer(this); label = new QLabel(this); //引數一:字型 引數二:大小 QFont font("Microsoft YaHei",15); //設定label背景顏色 , 字型顏色 label->setStyleSheet("background:transparent;color:rgb(0,0,0);"); label->setFont(font); label->setGeometry(70,50,100,110); //去掉視窗邊框 this->setWindowFlags(Qt::FramelessWindowHint | windowFlags()); //把視窗設定透明 this->setAttribute(Qt::WA_TranslucentBackground); //設定視窗置頂 this->setWindowFlags(Qt::WindowStaysOnTopHint | windowFlags()); //限定視窗尺寸 this->setFixedHeight(150); this->setFixedWidth(300); connect(Timer, SIGNAL(timeout()), this, SLOT(myTimerOut())); mSysTrayIcon = new QSystemTrayIcon(this); mSysTrayIcon->setIcon(QIcon(":/Res/CXK_3.ico")); mSysTrayIcon->setToolTip("CXK_3"); connect(mSysTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason))); connect(mSysTrayIcon,&QSystemTrayIcon::messageClicked,[&](){ this->show(); }); //建立托盤操作的選單 createActions(); createMenu(); //在系統托盤顯示此物件 mSysTrayIcon->show(); } void Widget::paintEvent(QPaintEvent *) { QPainter Painter(this); Painter.begin(this); if (m_bDrag) { Painter.drawPixmap(150, 0, QPixmap(":/Res/CXK_2.png")); Painter.drawPixmap(55, 70, QPixmap(":/Res/CXK.png")); } else { label->clear(); Painter.drawPixmap(150, 0, QPixmap(":/Res/CXK_1.png")); } } // 滑鼠按下事件 void Widget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { // label->setText("討厭!"); // m_bDrag = true; m_bDrag1 = true; //獲取資料初始位置 mouseStartPoint = event->globalPos(); //獲取視窗初始位置 windowTopLeftPoint = this->frameGeometry().topLeft(); //觸發一次paintEvent QWidget::update(); } } void Widget::mouseDoubleClickEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { label->setText("你幹嘛!"); sound->play(":/Res/你幹嘛.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } } // 滑鼠移動事件 void Widget::mouseMoveEvent(QMouseEvent *event) { if (m_bDrag1) { //獲得滑鼠移動距離 QPoint distance = event->globalPos() - mouseStartPoint; //改變視窗位置 this->move(windowTopLeftPoint + distance); } } // 滑鼠釋放事件 void Widget::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_bDrag = false; m_bDrag1 = false; QWidget::update(); } } //鍵盤按下事件 void Widget::keyPressEvent(QKeyEvent *event) { qDebug()<<event->text(); if (event->text() == "j" || event->text() == "J") //雞 { label->setText("雞!"); sound->play(":/Res/雞.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } else if (event->text() == "n" || event->text() == "N") //你 { label->setText("你!"); sound->play(":/Res/你.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } else if (event->text() == "t" || event->text() == "T") //太 { label->setText("太!"); sound->play(":/Res/太.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } else if (event->text() == "m" || event->text() == "M") //美 { label->setText("美!"); sound->play(":/Res/美.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } else if (event->text() == "\u0003") //唱 { label->setText("唱!"); sound->play(":/Res/唱.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } else if (event->text() == "\u0014") //跳 { label->setText("跳!"); sound->play(":/Res/跳.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } else if (event->text() == "\u0012") //rap { label->setText("rap!"); sound->play(":/Res/rap.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } else if (event->text() == "\f") //籃球 { label->setText("籃球!"); sound->play(":/Res/籃球.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } else if (event->text() == " ") //你幹嘛 { label->setText("你幹嘛!"); sound->play(":/Res/你幹嘛.wav"); m_bDrag = true; QWidget::update(); Timer->stop(); Timer->start(500); } } void Widget::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason) { switch(reason){ case QSystemTrayIcon::Trigger: mSysTrayIcon->showMessage("練習時長兩年半的個人練習生", "中分揹帶褲,我是ikun我最酷!",QSystemTrayIcon::Information, 1000); break; case QSystemTrayIcon::DoubleClick: this->show(); break; default: break; } } void Widget::createActions() { mShowMainAction = new QAction("展示", this); connect(mShowMainAction,SIGNAL(triggered()),this,SLOT(on_showMainAction())); mHideAppAction = new QAction("隱藏", this); connect(mHideAppAction,&QAction::triggered,[&](){this->hide();}); mExitAppAction = new QAction("退出", this); connect(mExitAppAction,SIGNAL(triggered()), this, SLOT(on_exitAppAction())); } void Widget::createMenu() { mMenu = new QMenu(this); mMenu->addAction(mShowMainAction); mMenu->addAction(mHideAppAction); mMenu->addAction(mExitAppAction); mSysTrayIcon->setContextMenu(mMenu); } void Widget::on_showMainAction() { this->show(); } void Widget::on_exitAppAction() { exit(0); } void Widget::myTimerOut() { m_bDrag = false; QWidget::update(); } Widget::~Widget() { delete ui; }
main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
原始碼下載地址:https://github.com/dsd188/Qt.git