#include "widget.h"
#include "ui_widget.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QBuffer>
#include <QTime>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
GetScreen();
LoadConfig();
this->setWindowFlags(Qt::FramelessWindowHint);
this->setWindowOpacity(Opacity);
//band = new QRubberBand(QRubberBand::Rectangle,this);
traylcon = new QSystemTrayIcon(QIcon(tr("1.ico")),this);
menu = new QMenu(this);
ahide = new QAction(tr("隐藏窗口"),this);
ashow = new QAction(tr("显示窗口"),this);
aexit = new QAction(tr("退出"),this);
redraw = new QAction(tr("重绘区域"),this);
menu->addAction(ahide);
menu->addAction(ashow);
menu->addAction(redraw);
menu->addAction(aexit);
connect(ashow,SIGNAL(triggered()),this,SLOT(show()));
connect(ahide,SIGNAL(triggered()),this,SLOT(hide()));
connect(aexit,SIGNAL(triggered(bool)),this,SLOT(close()));
connect(redraw,SIGNAL(triggered(bool)),this,SLOT(Repaint()));
traylcon->setContextMenu(menu);
traylcon->show();
atimer = new QTimer;
connect(atimer,SIGNAL(timeout()),this,SLOT(SendScreen()));
atimer->setSingleShot(true);
udpsend = new QUdpSocket;
// pen.setWidth(3);
// pen.setColor(Qt::red);
// pix=QPixmap(1440,900);
}
Widget::~Widget()
{
delete ui;
}
void Widget::LoadConfig()
{
QSettings setini("config.ini",QSettings::IniFormat);
FPIP=setini.value("SET/FPIP").toString();
Opacity=setini.value("SET/opacity",0.2).toFloat();
Interval=setini.value("SET/interval",100).toInt();
quality=setini.value("SET/quality",-1).toInt();
}
void Widget::GetScreen()
{
QList<QScreen*> scrlst;
scrlst=QGuiApplication::screens();
QRect zhu=scrlst[0]->geometry();
this->setGeometry(zhu);
ui->widget->setGeometry(zhu);
}
void Widget::Delay(int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void Widget::SendScreen()
{
pix=QPixmap::grabWindow(QApplication::desktop()->winId(),pStart.x(),pStart.y(),pEnd.x()-pStart.x(),pEnd.y()-pStart.y());
QByteArray picdata;
QBuffer picbuf(&picdata);
picbuf.open(QIODevice::WriteOnly);
pix.save(&picbuf,"jpg",quality);
while (picdata.size()>65500) {
QByteArray temp;
temp.resize(65500);
temp=picdata.mid(0,65500);
picdata.remove(0,65500);
udpsend->writeDatagram(temp.data(),temp.size(),QHostAddress(FPIP),8083);
Delay(50);
}
udpsend->writeDatagram(picdata.data(),picdata.size(),QHostAddress(FPIP),8083);
atimer->start(Interval);
}
void Widget::Repaint()
{
if(!dia->isHidden())
dia->close();
atimer->stop();
GetScreen();
show();
lock=false;
}
void Widget::HideAndStart()
{
dia->close();
this->hide();
atimer->start(Interval);
}
void Widget::ShowAndStart()
{
dia->close();
atimer->start(Interval);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
if(!lock)
pStart=event->pos();
// isDrawing=true;
}
}
//void Widget::mouseMoveEvent(QMouseEvent *event)
//{
// if(event->buttons()&Qt::LeftButton)
// {
// pEnd=event->pos();
// update();
// }
//}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
if(!lock)
{
pEnd=event->pos();
int w=pEnd.x()-pStart.x();
int h=pEnd.y()-pStart.y();
// band = new QRubberBand(QRubberBand::Rectangle,this);
// band->setGeometry(pStart.x(),pStart.y(),w,h);
// band->show();
this->setGeometry(pStart.x(),pStart.y(),w,h);
ui->widget->setGeometry(this->rect());
lock=true;
dia = new HideOrRe;
connect(dia,SIGNAL(hidewindow()),this,SLOT(HideAndStart()));
connect(dia,SIGNAL(showwindow()),this,SLOT(ShowAndStart()));
connect(dia,SIGNAL(redrwarect()),this,SLOT(Repaint()));
dia->move(pEnd.x(),pStart.y());
dia->show();
// isDrawing=false;
// atimer->start(Interval);
}
}
}
//void Widget::paintEvent(QPaintEvent *event)
//{
// Q_UNUSED(event);
// int x,y,w,h;
// x=pStart.x();
// y=pStart.y();
// w=pEnd.x()-x;
// h=pEnd.y()-y;
// QPainter painter(this);
// if(isDrawing)
// {
// temp=pix;
// QPainter pp(&temp);
// pp.setPen(pen);
// pp.drawRect(x,y,w,h);
// painter.drawPixmap(0,0,temp);
// }
//}
Qt屏幕共享软件,把当前电脑屏幕指定区域分享至其他电脑
需积分: 5 108 浏览量
2020-08-10
10:18:13
上传
评论 1
收藏 7KB RAR 举报
戏言zare
- 粉丝: 1115
- 资源: 30