QT调用GDI绘图

时间:2024-04-06 10:03:20

#pragma once

#include <QtWidgets/QWidget>
#include "ui_TestGdi.h"

class TestGdi : public QWidget
{
    Q_OBJECT

public:
    TestGdi(QWidget *parent = Q_NULLPTR);

    QPaintEngine * paintEngine() const;

    void paintEvent(QPaintEvent *event);

private:
    Ui::TestGdiClass ui;
};
 

 

#include "TestGdi.h"
#include <QtWinExtras/QtWin>
TestGdi::TestGdi(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    setAttribute(Qt::WA_PaintOnScreen, true);

    
    int a = 1;
    

}

void TestGdi::paintEvent(QPaintEvent *event)
{
    HDC hdc = GetDC((HWND)this->winId());

    HPEN pen = ::CreatePen(0, 4, RGB(0, 0, 0));
    HPEN oldPen = (HPEN)::SelectObject(hdc, pen);
    ::MoveToEx(hdc, 10, 10, NULL);
    ::LineTo(hdc, 200, 200);
    ::SelectObject(hdc, oldPen);
    DeleteObject(pen);

}

QPaintEngine * TestGdi::paintEngine() const
{
    return nullptr;
}

//要重写Qwidget里面的paintEngine()事件,如果不重写是绘制不出来 的

//效果如下:

QT调用GDI绘图