0から始めるVisualStudio2022:ソフト開発塾

20.絵を動かすでは、タイマを使って、絵を動かした
前回の絵を動かすその前にでは、タイマを使つかわず、
WndProc外のメッセージループで、直接文字を表示した。
今回は、文字ではなく本来の絵を表示する。
変更箇所は、TextOutを、BitBltやTransparentBltなどに変える。
#pragma comment(lib,"msimg32.lib")
#include 
#include 
#include 
#include "resource.h"
//
static  const LPCSTR szWindowClass = "sample\0";	// sample 
static  const LPCSTR szTitle = "SAMPLE\0";	// SAMPLE 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//
HINSTANCE g_hInst;
//extern HINSTANCE g_hInst;

//
int WINAPI WinMain(
	_In_ 		HINSTANCE hInstance,
	_In_opt_ 	HINSTANCE hPrevInstance,
	_In_ 		LPSTR     lpCmdLine,
	_In_ 		int       nCmdShow) {
	g_hInst = hInstance;

//	WNDCLASSEX	wcex;
	WNDCLASSEXA	wcex;

	wcex.cbSize = sizeof(WNDCLASSEXA);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = szWindowClass;
	wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);

	if (!RegisterClassExA(&wcex)) {
		return 1;
	}

	HWND hWnd = CreateWindowExA(
		WS_EX_OVERLAPPEDWINDOW,
		szWindowClass,
		szTitle,
		WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,   // ここに追加
		CW_USEDEFAULT, CW_USEDEFAULT,
		500, 300,
		NULL,
		NULL,
		hInstance,
		NULL
	);

	if (!hWnd) {
		return 1;
	}
	ShowWindow(hWnd, nCmdShow);

	// Main message loop:
	MSG msg;
/*	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	*/
	HDC  mm_hdc = GetDC(hWnd);

	HBITMAP mm_hbitmap1 = LoadBitmapA(hInstance, MAKEINTRESOURCEA(IDB_BITMAP1));
	BITMAP mm_bitmap1;
	GetObject(mm_hbitmap1, sizeof(BITMAP), &mm_bitmap1);
	HDC mm_hdc1 = CreateCompatibleDC(NULL);
	SelectObject(mm_hdc1, mm_hbitmap1);
	HBITMAP mm_MemBM = CreateCompatibleBitmap(mm_hdc, mm_bitmap1.bmWidth, mm_bitmap1.bmHeight);
	HDC mm_MemDC = CreateCompatibleDC(mm_hdc);
	SelectObject(mm_MemDC, mm_MemBM);
	int mm_old_tx = -1;
	int rc;

	int  mm_tx = 0, mm_ty = 0;
	BOOL fDone = FALSE;
	int i = 30001;
	while (!fDone) {
		if (i++ > 30000) {
			i = 0;
			TextOutA(mm_hdc, mm_tx, mm_ty, " ", 1);
			if (mm_old_tx >= 0) {
				rc = BitBlt(mm_hdc, mm_old_tx, 100, mm_bitmap1.bmWidth, mm_bitmap1.bmHeight, mm_MemDC, 0, 0, SRCCOPY);
			}
			mm_tx++;
			if (mm_tx > 1024) {
				mm_tx = 0;
				mm_ty += 8;
				if (mm_ty > 32) mm_ty = 0;
			}
			TextOutA(mm_hdc, mm_tx, mm_ty, "x", 1);
			mm_old_tx = mm_tx;
			rc = BitBlt(mm_MemDC, 0, 0, mm_bitmap1.bmWidth, mm_bitmap1.bmHeight, mm_hdc, mm_tx, 100, SRCCOPY);
			rc = TransparentBlt(
				mm_hdc, mm_tx, 100, mm_bitmap1.bmWidth, mm_bitmap1.bmHeight,
				mm_hdc1, 0, 0, mm_bitmap1.bmWidth, mm_bitmap1.bmHeight, RGB(255, 0, 255));

		}
		while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
				if (msg.message == WM_QUIT) {
					fDone = TRUE;
					break;
				}
				if (GetMessage(&msg, NULL, 0, 0)) {
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
				else {
					fDone = TRUE;
					break;
				}
			}
	}
	return (int)msg.wParam;
}
移動速度調整のため、if (i++ > 30000)








Top