Skip to content

Commit

Permalink
Merge pull request #13 from pinebit/image_to_skype_win
Browse files Browse the repository at this point in the history
 clipboard image is not pasted in 'Skype' (win)
  • Loading branch information
pinebit authored Nov 18, 2016
2 parents 523f640 + 881f7b6 commit b596498
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dialogs/helpdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ HelpDialog::HelpDialog(const QString& path, const QString& page, QWidget* parent
checkBox->setCheckState(Qt::Checked);

QPushButton* closeButton = new QPushButton(tr("Close"));
connect(closeButton, QPushButton::clicked, this, &HelpDialog::reject);
connect(closeButton, &QPushButton::clicked, this, &HelpDialog::reject);

QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(checkBox);
Expand Down
16 changes: 7 additions & 9 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "selector/cvselector.h"
#include "userselection.h"
#include "params.h"
#include <platform/platform.h>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
Expand Down Expand Up @@ -75,21 +76,18 @@ void MainWindow::slotActionCapture()

void MainWindow::slotActionPaste()
{
QClipboard *clipboard = QApplication::clipboard();
QImage image = clipboard->image();
if (image.isNull()) {
QImage image;
if (!tryPasteFromClipboard(image)) {
_statusbar->showMessage(tr("Clipboard has no image."));
return;
}
else {
updateImage(image);
}
updateImage(image);
_statusbar->showMessage(tr("Pasted from the clipboard."));
}

void MainWindow::slotActionCopy()
{
QClipboard *clipboard = QApplication::clipboard();
clipboard->setImage(_rsview->renderToPixmap().toImage());

copyToClipboard(_rsview->renderToPixmap().toImage());
_statusbar->showMessage(tr("Copied to the clipboard."));
}

Expand Down
13 changes: 13 additions & 0 deletions platform/generic/clipboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "clipboard.h"

#include <QApplication>
#include <QClipboard>

void copyToClipboard(const QImage& image){
QApplication::clipboard()->setImage(image);
}

bool tryPasteFromClipboard(QImage& image) {
image = QApplication::clipboard()->image();
return (!image.isNull()) ? true : false;
}
6 changes: 6 additions & 0 deletions platform/generic/clipboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <QImage>

void copyToClipboard(const QImage& image);
bool tryPasteFromClipboard(QImage& image);
9 changes: 9 additions & 0 deletions platform/generic/generic.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CONFIG += static staticlib c++11

INCLUDEPATH += $$PWD

HEADERS += \
$$PWD/clipboard.h

SOURCES += \
$$PWD/clipboard.cpp
7 changes: 7 additions & 0 deletions platform/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#ifdef Q_OS_WIN
#include <platform/windows/clipboard.h>
#else
#include <platform/generic/clipboard.h>
#endif
9 changes: 9 additions & 0 deletions platform/platform.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
win32 {
include(windows/windows.pri)
}
unix:!macx {
include(generic/generic.pri)
}
macx {
include(generic/generic.pri)
}
22 changes: 22 additions & 0 deletions platform/windows/clipboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "clipboard.h"
#include "image.h"

#include <QApplication>
#include <QClipboard>

void copyToClipboard(const QImage& image){
HBITMAP bitmap = image::convertQImageToHBITMAP(image);
if(bitmap) {
if (::OpenClipboard(NULL)){
::EmptyClipboard();
::SetClipboardData(CF_BITMAP, bitmap);
::CloseClipboard();
}
DeleteObject(bitmap);
}
}

bool tryPasteFromClipboard(QImage& image) {
image = QApplication::clipboard()->image();
return (!image.isNull()) ? true : false;
}
6 changes: 6 additions & 0 deletions platform/windows/clipboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <QImage>

void copyToClipboard(const QImage& image);
bool tryPasteFromClipboard(QImage& image);
56 changes: 56 additions & 0 deletions platform/windows/image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

#include "image.h"

HBITMAP image::convertQImageToHBITMAP(const QImage& image){
if (image.isNull())
return 0;

HDC displayDC = ::GetDC(NULL);
HDC memoryDC = ::CreateCompatibleDC(NULL);
if (!displayDC || !memoryDC){
return 0;
}
// it is a memory memoryDC that must have a handle selected into it
HBITMAP dummyBitmap = ::CreateCompatibleBitmap(displayDC, image.width(), image.height());
::ReleaseDC(NULL, displayDC);
HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, dummyBitmap);

BITMAPINFO info;
::memset(&info, 0, sizeof(info));
info.bmiHeader.biBitCount = image.depth();
info.bmiHeader.biClrImportant = 0;
info.bmiHeader.biClrUsed = 0;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biHeight = -image.height();
info.bmiHeader.biWidth = image.width();
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biSizeImage = image.width() * image.height() * 4;

HBITMAP bitmap = ::CreateDIBitmap(memoryDC, &info.bmiHeader, CBM_INIT, image.constBits(),(LPBITMAPINFO) &info, DIB_RGB_COLORS);
if (bitmap) {
::SelectObject(memoryDC, oldBitmap);
::DeleteObject(dummyBitmap);
::DeleteDC(memoryDC);
}
return bitmap;
}

HBITMAP image::captureQWidget(const QWidget& widget){
HWND hWnd = (HWND)widget.winId();
HDC displayDC = ::GetDC(hWnd);
if (!displayDC){
return 0;
}
RECT rectWindow;
::GetWindowRect(hWnd, &rectWindow);

HDC memoryDC = ::CreateCompatibleDC(displayDC);
HBITMAP bitmap = ::CreateCompatibleBitmap(displayDC, rectWindow.right - rectWindow.left, rectWindow.bottom - rectWindow.top);
if (bitmap) {
::SelectObject(memoryDC, bitmap);
::BitBlt(memoryDC, 0, 0, rectWindow.right - rectWindow.left, rectWindow.bottom - rectWindow.top, displayDC, rectWindow.left, rectWindow.top, SRCCOPY);
}
::ReleaseDC(hWnd, memoryDC);
return bitmap;
}
14 changes: 14 additions & 0 deletions platform/windows/image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <QImage>
#include <QWidget>

#include <windows.h>
#include <wingdi.h>

class image
{
public:
static HBITMAP convertQImageToHBITMAP(const QImage& image);
static HBITMAP captureQWidget(const QWidget& widget);
};
12 changes: 12 additions & 0 deletions platform/windows/windows.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CONFIG += static staticlib c++11
LIBS += -luser32

INCLUDEPATH += $$PWD

HEADERS += \
$$PWD/image.h \
$$PWD/clipboard.h

SOURCES += \
$$PWD/image.cpp \
$$PWD/clipboard.cpp
3 changes: 3 additions & 0 deletions reverscreen.pro
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ OPENCV_PATH = C:/Opencv/mingw_debug
INCLUDEPATH += $$OPENCV_PATH/include
LIBS += -L"$$OPENCV_PATH/x86/mingw/lib"
LIBS += -lopencv_core2413.dll -lopencv_imgproc2413.dll
LIBS += -lgdi32

QMAKE_LFLAGS_RELEASE += -static -static-libgcc

Expand All @@ -85,6 +86,7 @@ QMAKE_INFO_PLIST = Info.plist

QMAKE_CXXFLAGS += -Wno-deprecated-declarations

include(platform/platform.pri)
include(QtAwesome/QtAwesome.pri)

SUBDIRS += QtAwesome/QtAwesome.pro \
Expand All @@ -109,3 +111,4 @@ DISTFILES += \
LICENSE \
README.md \
nsis/reverscreen.nsi

0 comments on commit b596498

Please sign in to comment.