粘贴板工具:PPT或Excel复制粘贴成图片问题的解决方案

粘贴板工具:PPT或Excel复制粘贴成图片问题的终极解决方案

一、小工具介绍

       针对PPT或Excel复制粘贴成图片问题,Clipboard是一个将图像粘贴板内容转化为文字粘贴板内容的小工具。下载链接:https://github.com/MarkCens/Clipboard2text/blob/master/demo/Clipboard2text.rar。代码:https://github.com/MarkCens/Clipboard2text。
在这里插入图片描述

使用指南
  1. 点击并按住第一个按钮,你将能够实现小窗体的拖动,注意是使用鼠标左键点击拖拽。
  2. 点击第二个按钮(一个笑脸),你将能够实现傻瓜式的复制粘贴成文字的操作。
  3. 点击第三个按钮(一个顶字),你将能够取消窗体最前。如果再次点击该按钮,那么将能再次设置窗体最前。
  4. 点击第四个按钮(一个交叉),你将能够退出这个小窗体。
  5. 下载以后,解压,运行exe文件。如果运行过程有任何问题,请留言。

二、开发背景

       微软PPT、Excel等软件复制出来的粘贴板内容优先是图像,其次才是文字。因此,在微信等可以粘贴图像的输入框中常常是优先粘贴图像内容而非文字内容,这给微信等聊天用户带来了一定的不便。为了说明此场景,我将该问题进行更详细的描述。

环境

       一个PPT文件或者Excel文件,电脑安装微软office办公软件(包括PPT、Excel等),电脑已安装微信PC版。

示范

       以PPT文件为例,我打开了一个PPT文件,如下。
在这里插入图片描述
       但是我的英文水平不是特别好(个人水平有限),想要将这页PPT内容复制到微信中翻译(个人偏好,因为微信聊天能保存翻译过的多条记录能给我带来很多方便,不喜勿喷)。
在这里插入图片描述
       然后我一波操作猛如虎,便是拿去ctrl+c、ctrl+v键复制我需要翻译的文字到微信聊天框中,想着马上就能看到翻译结果了!…结果,竟然…得到了一张图片!我人都!!!∑(゚Д゚ノ)ノ(小声叨唠:图片我怎么翻译嘛!)
在这里插入图片描述
       可是图片,图片有啥子嘛。于是我拿起手机点开图片又是一波翻译操作。

       但是…还有很多张图片文件需要我如此一波又一波的操作,这让我操碎了心呐!

       心碎的我,不知道该质疑微软好还是微信好…

       这时,我突然脑瓜闪过一个想法,既然是想要得到文字,在txt文件或者只能输入文字的输入框粘贴一下再复制一下不就好了?好像是哦。于是我动用起来了txt文件,打开又是一波ctrl+c ctrl+v的操作,这确实让我体验到了复制文字粘贴文字的舒服的感觉,翻译也很快啊,因为是文字所以一下子就出来结果了。但是久了,懒惰的我发现,这是一份令人感到劳累的苦差事,我ctrl+c ctrl+v的两根手指都酸了,十分苦恼TAT。

       苦恼了几秒钟以后,我灵光一动,仿佛得到了Bjarne Stroustrup博士的指点。于是脑海中又飘过一个想法,既然我学习过C++和Qt,为什么不能写一个小工具能够进行傻瓜式的点击操作复制粘贴呢?能啊,于是我事不宜迟,立刻动手,写了一个Qt小窗口,实现了我的这个想法。

三、实现过程

       如果你只是使用这个工具,不需要浏览一下内容。下面是我实现这个工具的过程和内容。按理来说,此工具不仅仅使用与微信的聊天框,QQ聊天框、TIM聊天框以及其他可以粘贴图像的输入框都适用。

       在我的想法中,这个小窗口能够拖拽,能够点一下就复制成文字,能够始终在所有窗口最前(同时也能取消窗口最前),能够退出。所以,我分别设计了四个按钮,用来实现这四个功能。

       这第一步,我在Qt creator中设计了一个窗口界面,这个窗口界面是长宽固定,并且是一个水平布局,这水平布局里面放四个按钮,这四个按钮的长宽也是固定的。为了美观,我在界面的设计加入了QSS样式以及qrc资源文件。窗体的界面代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>120</width>
    <height>35</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>120</width>
    <height>35</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>120</width>
    <height>35</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>-</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <property name="sizePolicy">
    <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <property name="minimumSize">
    <size>
     <width>120</width>
     <height>35</height>
    </size>
   </property>
   <property name="maximumSize">
    <size>
     <width>120</width>
     <height>35</height>
    </size>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout_2">
    <property name="spacing">
     <number>0</number>
    </property>
    <property name="leftMargin">
     <number>0</number>
    </property>
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="rightMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <property name="spacing">
       <number>0</number>
      </property>
      <item>
       <widget class="QPushButton" name="main_move">
        <property name="minimumSize">
         <size>
          <width>15</width>
          <height>35</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>15</width>
          <height>35</height>
         </size>
        </property>
        <property name="styleSheet">
         <string notr="true">QPushButton{background-color:#4aabfe;color:#ffffff;font-weight:bold;border-image:url();border:0;}
QPushButton:hover{background-color:#5bbcff;}
QPushButton:pressed{background-color:#5bbcff;}</string>
        </property>
        <property name="text">
         <string/>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="main_convert">
        <property name="minimumSize">
         <size>
          <width>35</width>
          <height>35</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>35</width>
          <height>35</height>
         </size>
        </property>
        <property name="styleSheet">
         <string notr="true">QPushButton{background-color:#4aabfe;color:#ffffff;font-weight:bold;border-image:url();border-image:url(:/img/icon.png);border:0;}
QPushButton:hover{background-color:#5bbcff;}
QPushButton:pressed{background-color:#5bbcff;}</string>
        </property>
        <property name="text">
         <string/>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="main_top">
        <property name="minimumSize">
         <size>
          <width>35</width>
          <height>35</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>35</width>
          <height>35</height>
         </size>
        </property>
        <property name="font">
         <font>
          <family>Microsoft YaHei UI</family>
          <pointsize>12</pointsize>
          <weight>75</weight>
          <bold>true</bold>
         </font>
        </property>
        <property name="styleSheet">
         <string notr="true">QPushButton{background-color:#4aabfe;color:#ffffff;font-weight:bold;border-image:url();border:0;}
QPushButton:hover{background-color:#5bbcff;}
QPushButton:pressed{background-color:#5bbcff;}</string>
        </property>
        <property name="text">
         <string></string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="main_close">
        <property name="minimumSize">
         <size>
          <width>35</width>
          <height>35</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>35</width>
          <height>35</height>
         </size>
        </property>
        <property name="font">
         <font>
          <family>Microsoft YaHei UI</family>
          <pointsize>12</pointsize>
          <weight>75</weight>
          <bold>true</bold>
         </font>
        </property>
        <property name="styleSheet">
         <string notr="true">QPushButton{background-color:#4aabfe;color:#ffffff;font-weight:bold;border-image:url(:/img/close.png);border:5px 5px 5px 5px;}
QPushButton:hover{background-color:#ff5747;}
QPushButton:pressed{background-color:#5bbcff;}</string>
        </property>
        <property name="text">
         <string/>
        </property>
        <property name="iconSize">
         <size>
          <width>16</width>
          <height>16</height>
         </size>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

       按钮需要实现它的点击事件或者拖拽事件,因此我在窗口的头文件定义了相关的函数。由于Qt中是利用信号和槽实现事件监听的,因此如果你想理解下面的代码,你需要实现了解Qt的信号和槽机制。窗体的头文件如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QIcon>
#include <QPoint>
#include <QMouseEvent>
#include <QApplication>
#include <QClipboard>
#include <QString>
#include <QLineEdit>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void mousePressEvent(QMouseEvent *);    // 实现窗体移动的事件(鼠标点击事件)
    void mouseMoveEvent(QMouseEvent *);     // 实现窗体移动的事件(鼠标移动事件)

public slots:
    void click_bt_top();    // 槽:窗体最前点击函数
    void click_bt_close();  // 槽:窗体关闭点击函数
    void paste();           // 槽:复制点击函数(点击实现傻瓜式图片粘贴板内容2文字粘贴板内容)

private:
    Ui::MainWindow *ui;
    bool m_is_top = false;  // 变量:用于实现窗体最前/取消窗体最前
    QPoint m_pre_pos;       // 变量:用于实现窗体拖拽移动
};
#endif // MAINWINDOW_H

窗体的cpp文件如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){
    ui->setupUi(this);
    // 隐藏窗体标题栏
    this->setWindowFlags(Qt::FramelessWindowHint);
    // 设置窗体icon,这个qtc资源图片路径
    this->setWindowIcon(QIcon(":/img/icon.png"));
    // 设置窗体最前
    click_bt_top();
    // 连接信号和槽(设置程序需要的各种事件监听)
    connect(ui->main_convert, &QPushButton::clicked, this, &MainWindow::paste);
    connect(ui->main_top, &QPushButton::clicked, this, &MainWindow::click_bt_top);
    connect(ui->main_close, &QPushButton::clicked, this, &MainWindow::click_bt_close);
}

MainWindow::~MainWindow(){
    delete ui;
}

void MainWindow::click_bt_top(){
    // 如果不是窗体最前
    if (this->m_is_top == false){
        // 设置为窗体最前
        this->m_is_top = true;
        // 更新按钮的样式
        ui->main_top->setStyleSheet(""
                                    "QPushButton{background-color:#5bbcff;color:#ffffff;font-weight:bold;border-image:url();border:0;}"
                                    "QPushButton:hover{background-color:#4aabfe;}"
                                    "QPushButton:pressed{background-color:#4aabfe;}");
        // 设置窗体Flags,前者是隐藏窗口标题栏,后者是保持窗体最前
        this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
        this->showNormal();
    }else{  // 如果已经是窗体最前
        this->m_is_top = false;
        // 取消设置为窗体最前
        ui->main_top->setStyleSheet(""
                                    "QPushButton{background-color:#4aabfe;color:#ffffff;font-weight:bold;border-image:url();border:0;}"
                                    "QPushButton:hover{background-color:#5bbcff;}"
                                    "QPushButton:pressed{background-color:#5bbcff;}");
        // 设置窗体Flags,意思是保持当前窗口Flags但除了保持窗体最前
        this->setWindowFlags(this->windowFlags() & ~Qt::WindowStaysOnTopHint);
        this->showNormal();
    }
}

void MainWindow::click_bt_close(){
    // 关闭窗口,退出程序
    this->close();
    exit(0);
}

void MainWindow::paste(){
    // 获取系统剪贴板指针
    QClipboard *clipboard = QApplication::clipboard();
    // 获取剪贴板文本信息
    QString content = clipboard->text();
    // 设置剪贴板内容
    clipboard->setText(content);
}

void MainWindow::mouseMoveEvent(QMouseEvent *lpEvent){
    // 鼠标左键实现拖拽
    if (lpEvent->buttons() & Qt::LeftButton){
        // 获取位置
        QPoint pos = lpEvent->globalPos();
        // 移动窗体
        move(pos - m_pre_pos);
    }
}

void MainWindow::mousePressEvent(QMouseEvent *lpEvent){
    // 鼠标左键实现拖拽
    if (lpEvent->button() == Qt::LeftButton){
        // 获取位置
        m_pre_pos = lpEvent->globalPos() - frameGeometry().topLeft();
    }
}

       点击Qt creator绿色的运行按钮,便完成了我的小工具开发。

四、结束语

       如果你不想写一遍代码,没关系,请到我的GitHub项目中下载这个软件,解压点击就能运行使用这个方便的小工具。

       如果你有更好的建议和解决方案,请在博客中留言,让更多的朋友享受到复制粘贴的快乐。
       如果你读完以后觉得快乐,请为我点赞,我将感激不尽!

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞机火车巴雷特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值