C++恶搞——Windows桌面乱序

整理篇

今日整理了一下自己的电脑,翻出了一些开始学习时觉得有意思的代码。我一直觉得编程始于兴趣,也强于兴趣,兴趣是我们学习最好的老师。虽然这里给出的代码没什么难度,获取可以说很简单,但这还是我好多年前学C++时学习的一个Windows编程的程序,希望能帮助正在初学的小伙伴们。

展示

先看看效果图吧
在这里插入图片描述

正如图片所示,这只是一个让桌面错乱的一个程序。通过windows API很容易的实现。

注意 这里是windows程序,所以需要你是用vs的时候创建windows新项目(空白项就可以了)。

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
	HWND hwnd;
	const int NUM = 500;

	/// 锁定指定窗口,禁止它更新
	/// GetDesktopWindow() 用于返回桌面窗口的句柄
	if (LockWindowUpdate(hwnd = GetDesktopWindow()))
	{
		HDC hdcScr = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE);
		///创建兼容DC
		HDC hdcMem = CreateCompatibleDC(hdcScr);
		
		/// 获取系统当前的分辨率,宽像素 和 高像素
		int cxClient = GetSystemMetrics(SM_CXSCREEN) / 10;
		int cyClient = GetSystemMetrics(SM_CYSCREEN) / 10;

		/// 创建兼容位图
		HBITMAP hBitmap = CreateCompatibleBitmap(hdcScr, cxClient, cyClient);

		SelectObject(hdcMem, hBitmap);

		srand((int)GetCurrentTime());

		int iKeep[NUM][4];
		int	x1, x2, y1, y2;

		for (int i = 0; i < 2; ++i)
		{
			for (int j = 0; j < NUM; ++j)
			{
				if (0 == i)
				{
					iKeep[j][0] = x1 = cxClient * (rand() % 10);
					iKeep[j][1] = y1 = cyClient * (rand() % 10);
					iKeep[j][2] = x2 = cxClient * (rand() % 10);
					iKeep[j][3] = y2 = cyClient * (rand() % 10);
				}
				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];
				}

				/// 对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境
				BitBlt(hdcMem,  0,  0, cxClient, cyClient, hdcScr, x1, y1, SRCCOPY);
				BitBlt(hdcScr, x1, y1, cxClient, cyClient, hdcScr, x2, y2, SRCCOPY);
				BitBlt(hdcScr, x2, y2, cxClient, cyClient, hdcMem,  0,  0, SRCCOPY);

				Sleep(10);
			}
		}

		DeleteDC(hdcMem);
        ReleaseDC(hwnd, hdcScr);
        DeleteObject(hBitmap);
             
        LockWindowUpdate(NULL);
	}

	return FALSE;
}

代码就这么点,运行就行了,现在看来这些东西挺无趣,但想想当初还很兴奋的发给朋友,还特意改了图标名字。
在这里插入图片描述 这东西对于不了解的朋友以为我发了啥,当初也挺有意思的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章