Skip to content

Instantly share code, notes, and snippets.

@CaptainJH
Last active April 14, 2018 05:20
Show Gist options
  • Save CaptainJH/544d2de41a00d813fa59c6216e02aa99 to your computer and use it in GitHub Desktop.
Save CaptainJH/544d2de41a00d813fa59c6216e02aa99 to your computer and use it in GitHub Desktop.
PyQT5 fonts and HWND sample
import sys
import ctypes
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QFontDatabase, QFont, QFontMetrics, QImage, QPainter, QPen
from PyQt5.QtCore import Qt, QRectF
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
print(int(w.winId()))
testDll = ctypes.CDLL('./testDll.dll')
Test_Add_int = testDll.Test_Add_int
Test_Add_int.argtypes = [ctypes.c_int, ctypes.c_int]
Test_Add_int.restype = ctypes.c_int
ret = Test_Add_int(2, 3)
Test_Add_float = testDll.Test_Add_float
Test_Add_float.argtypes = [ctypes.c_float, ctypes.c_float]
Test_Add_float.restype = ctypes.c_float
ret = Test_Add_float(2.1, 3.3)
HWND_Func = testDll.HWND_Func
HWND_Func.argtypes = [ctypes.c_uint32]
HWND_Func(int(w.winId()))
fonts = QFontDatabase().families()
#for f in fonts:
# print(f)
msg = "abcdefghijklmnopqrstuvwxyz"
font = QFont("Tahoma")
font.setUnderline(False)
font.setPointSize(20)
image = QImage(500, 100, QImage.Format_ARGB32_Premultiplied)
painter = QPainter(image)
painter.setFont(font)
painter.fillRect(image.rect(), Qt.white)
x = image.rect().x()
y = image.rect().y()
painter.drawText(image.rect(), Qt.AlignLeft | Qt.AlignTop, msg)
metric = QFontMetrics(font)
pen = QPen(Qt.red)
painter.setPen(pen)
for c in msg:
r = QRectF(x, y, metric.width(c), metric.height())
painter.drawRect(r)
x += metric.width(c)
image.save("output.png")
w.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment