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;
}

代碼就這麼點,運行就行了,現在看來這些東西挺無趣,但想想當初還很興奮的發給朋友,還特意改了圖標名字。
在這裏插入圖片描述 這東西對於不瞭解的朋友以爲我發了啥,當初也挺有意思的。

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