这是GDI绘制源码,怎么加上给他加上双缓存防止闪烁
#include <stdio.h>
#include<Windows.h>
#include<stdlib.h>
LRESULT __stdcall callback(HWND hwnd, UINT usermsg, WPARAM wparam, LPARAM lparam)
{
switch (usermsg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(1);
break;
}
return DefWindowProc(hwnd, usermsg, wparam, lparam);
}
HWND createlaiedwindow(const char wndname[255], RECT rect_layedwindow)//创建一个透明的绘制窗口
{
WNDCLASSEX Layedwindow = { 0 };
Layedwindow.cbSize = sizeof WNDCLASSEX;
Layedwindow.lpfnWndProc = callback;
Layedwindow.lpszClassName = "w;";
Layedwindow.style = CS_HREDRAW | CS_VREDRAW;
Layedwindow.hCursor = LoadCursor(NULL, IDC_ARROW);
Layedwindow.hbrBackground = ((HBRUSH)RGB(0, 0, 0));
Layedwindow.hInstance = GetModuleHandle(0);
RegisterClassEx(&Layedwindow);//注册窗口类
//利用窗口类创建一个实体窗口
HWND hwnd_layedwindow = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT, Layedwindow.lpszClassName, wndname,
WS_POPUP, rect_layedwindow.left, rect_layedwindow.top,
rect_layedwindow.right - rect_layedwindow.left, rect_layedwindow.bottom - rect_layedwindow.top,
NULL, NULL, Layedwindow.hInstance, NULL);
SetLayeredWindowAttributes(hwnd_layedwindow, RGB(0, 0, 0), 0, 1);//设置窗口透名
ShowWindow(hwnd_layedwindow, SW_SHOW);
UpdateWindow(hwnd_layedwindow);
return hwnd_layedwindow;
}
void UpdateLayedWindow(HWND hwnd_layedwindow, HWND hwnd_game)//刷新窗口
{
RECT rect_layedwindow{};
GetWindowRect(hwnd_game, &rect_layedwindow);
SetWindowPos(hwnd_layedwindow, HWND_TOPMOST,
rect_layedwindow.left, rect_layedwindow.top,
rect_layedwindow.right - rect_layedwindow.left, rect_layedwindow.bottom - rect_layedwindow.top, SWP_SHOWWINDOW);
MoveWindow(hwnd_layedwindow, rect_layedwindow.left, rect_layedwindow.top,
rect_layedwindow.right - rect_layedwindow.left, rect_layedwindow.bottom - rect_layedwindow.top, TRUE);
}
void clear(HWND hwnd_overlay, RECT rect_window)//删除上一贞数据,防满屏绘制
{
RECT rect = { 0,0,rect_window.right - rect_window.left,rect_window.bottom - rect_window.top };
HWND hwnd = GetForegroundWindow();
HDC hdc = GetDC(hwnd_overlay);
HBRUSH hbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
SelectObject(hdc, hbrush);
FillRect(hdc, &rect, hbrush);
DeleteObject(hbrush);
ReleaseDC(hwnd_overlay, hdc);
}
namespace gdi
{
void drawbox(HDC hdc_layedwindow, int x, int y, int boxwidth, int boxheight)
{
Rectangle(hdc_layedwindow, x, y, x + boxwidth, y + boxheight);
}
void drawline(HDC hdc_layedwindow, int x1, int y1, int x2, int y2)
{
MoveToEx(hdc_layedwindow, x1, y1, NULL);
LineTo(hdc_layedwindow, x2, y2);
}
void drawstring(HDC hdc_layedwindow, int x, int y, COLORREF color, const char* text)
{
SetTextColor(hdc_layedwindow, color);
SetBkMode(hdc_layedwindow, RGB(0, 0, 0));
TextOutA(hdc_layedwindow, x, y, text, strlen(text));
}
}
#include <iostream>
#include "GDI.h"
HWND hwnd = NULL;
HWND hwnd_layedwindow = NULL;
unsigned long processid = NULL;
RECT rect = { 0 };
HDC hdc = NULL;
HPEN hpen = NULL;
HBRUSH hbrush = NULL;
void init(const char wndname[255]);
int main()
{
MessageBoxA(NULL, "按下确定开始绘制", "tips", MB_ICONERROR);
init("新建文本文档.txt - 记事本");
while (true)
{
for (size_t i = 0; i < 700; i=i+12)
{
GetWindowRect(hwnd, &rect);
UpdateLayedWindow(hwnd_layedwindow, hwnd);
clear(hwnd_layedwindow, rect);
hdc = GetDC(hwnd_layedwindow);
hpen = CreatePen(PS_INSIDEFRAME, 1, RGB(255, 0, 0));
hbrush = (HBRUSH)GetStockObject(DEFAULT_GUI_FONT);
SelectObject(hdc, hpen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
SelectObject(hdc, hbrush);//选择对象
gdi::drawstring(hdc, 30+i, 80, RGB(255, 0, 0), "草");
gdi::drawbox(hdc, 30+(i-5), 70, 20, 30);
gdi::drawstring(hdc, 30 + i, 150, RGB(255, 0, 0), "草");
gdi::drawbox(hdc, 30 + (i - 5), 140, 20, 30);
gdi::drawstring(hdc, 30 , 250, RGB(255, 0, 0), "草");
gdi::drawbox(hdc, 25 , 240, 20, 30);
DeleteObject(hbrush);
DeleteObject(hpen);
ReleaseDC(hwnd_layedwindow, hdc);
}
}
}
void init(const char wndname[255])//初始化函数
{
hwnd = FindWindow(NULL, wndname);
if (!hwnd) {
MessageBoxA(NULL, "未找到窗口", "tips", MB_ICONERROR);
PostQuitMessage(0);
}
GetWindowThreadProcessId(hwnd, &processid);
if (processid == NULL) {
MessageBoxA(NULL, "取Pid失败", "tips", MB_ICONERROR);
}
GetWindowRect(hwnd, &rect);
hwnd_layedwindow = createlaiedwindow(" ", rect);
if (!hwnd_layedwindow)
{
MessageBoxA(NULL, "创建窗口失败", "tips", MB_ICONERROR);
}
}