본문 바로가기
소프트웨어/파이썬

[파이썬] pywin32를 이용해 윈도우 화면에 그리기

by 만들오 2022. 6. 2.
728x90

윈도우 화면에 강조를 위한 그리기 방법입니다.

 

requirements
pip install pywin32

 

draw.py
import win32gui, win32api

class draw:
    def __init__(self):
        hwnd = win32gui.GetDesktopWindow()
        self.hdc = win32gui.GetDC(hwnd)
    def rect(self, x, y, w, h, color=False):
        # color = (255,0,0) int type
        color = win32api.RGB(0,255,0) if not color else win32api.RGB(color[0], color[1], color[2])
        for i in range(x, x + w):
            win32gui.SetPixel(self.hdc, i, y, color)
            win32gui.SetPixel(self.hdc, i, y + h, color)
        for j in range(y, y + h):
            win32gui.SetPixel(self.hdc, x, j, color)
            win32gui.SetPixel(self.hdc, x + w, j, color)

app = draw()
app.rect(20, 10, 55, 55)
app.rect(20, 135, 55, 55)

# 빨간색으로 하려면 아래처럼
# app.rect(20, 10, 55, 55, (255,0,0))

 

실행 결과

지정한 위치에 초록색 테두리를 그리도록 했습니다.

화면상에서 이미지를 찾는 경우, 위 함수를 이용해 표시해줄 용도로 만든 코드입니다.

[끝].

댓글