浙江财经大学
信工学院ACM集训队

[Win32]窗口外的位图

本文由 Ocrosoft 于 2018-06-02 14:00:57 发表

GetDesktopWindow,可以获得桌面句柄

LockWindowUpdate,可以锁定窗口刷新,除了锁定的程序,其他程序不能刷新窗口

运行效果(跟桌面歌词一起使用的时候,桌面歌词仍能刷新,还原的时候也会有问题)

1.1.源码

#include <windows.h>
//#include "resource.h"

#define NUM 300

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	static int iKeep[NUM][4];
	HDC hdcScr, hdcMem;
	int cx, cy;
	HBITMAP hBitmap;
	HWND hwnd;
	int i, j, x1, x2, y1, y2;

	// 锁定屏幕,禁止其他程序刷新屏幕
	// GetDesktopWindow,返回桌面(整个屏幕)的句柄
	if (LockWindowUpdate(hwnd = GetDesktopWindow()))
	{
		// GetDC,获取指定窗口的客户区句柄
		// GetWindowDC,获取指定窗口的窗口句柄
		// GetDCEx,获取指定窗口或者整个屏幕的句柄
		hdcScr = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE);
		hdcMem = CreateCompatibleDC(hdcScr);
		// cx, cy 保存每个小矩形的宽高
		cx = GetSystemMetrics(SM_CXSCREEN) / 10;
		cy = GetSystemMetrics(SM_CYSCREEN) / 10;
		hBitmap = CreateCompatibleBitmap(hdcScr, cx, cy);

		SelectObject(hdcMem, hBitmap);

		srand((int)GetCurrentTime());

		for (i = 0; i < 2; i++)
		{
			for (j = 0; j < NUM; j++)
			{
				// i==0时打乱
				if (i == 0)
				{
					iKeep[j][0] = x1 = cx * (rand() % 10);
					iKeep[j][1] = y1 = cy * (rand() % 10);
					iKeep[j][2] = x2 = cx * (rand() % 10);
					iKeep[j][3] = y2 = cy * (rand() % 10);
				}
				// i==1时恢复
				else
				{
					x1 = iKeep[NUM - 1 - j][0];
					y1 = iKeep[NUM - 1 - j][1];
					x2 = iKeep[NUM - 1 - j][2];
					y2 = iKeep[NUM - 1 - j][3];
				}
				// 将hdcMem作为临时空间进行交换
				BitBlt(hdcMem, 0, 0, cx, cy, hdcScr, x1, y1, SRCCOPY);
				BitBlt(hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY);
				BitBlt(hdcScr, x2, y2, cx, cy, hdcMem, 0, 0, SRCCOPY);

				Sleep(10);
			}
			Sleep(1000);
		}

		DeleteDC(hdcMem);
		ReleaseDC(hwnd, hdcScr);
		DeleteObject(hBitmap);

		// 解锁屏幕
		LockWindowUpdate(NULL);
	}
	return FALSE;
}

欢迎转载,请保留出处与链接。Ocrosoft » [Win32]窗口外的位图

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址