Experiment description:
Design a program to realize the application of bitmap resource.
- Load a bitmap (you can use can bitmap download from the Internet) to the center of the window and the size of the bitmap should be 1/4 of the window. 1/4 of the window.)
- When left-click/right-click the mouse or press the up/down arrow on the keyboard, the bitmap move up/down and when it moves to the upper/lower bound of the window, some warning text appear. (当兔子左When key-clicking/right-clicking or pressing the up/down arrows on the keyboard, the bitmap will move up/down. When the bitmap is moved to the upper/lower limit of the window, some warnings will appear.)
1 The source code is as follows:
#include "resource.h"
#include<windows.h>
#define MAX_LOADSTRING 100
HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
static int cxClient, cyClient, y=40;
HBITMAP hbm;
char str1[] = "Cannot move up anymore!";
char str2[] = "Cannot move down anymore!";
HDC hdcm;
//Functions
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BITMAPDEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
return FALSE;
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BITMAPDEMO);
while (GetMessage(&msg, NULL, 0, 0))
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//register a windows class
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wndclassex;
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = (WNDPROC)WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BITMAPDEMO);
wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclassex.lpszMenuName = (LPCSTR)IDC_BITMAPDEMO;
wndclassex.lpszClassName = szWindowClass;
wndclassex.hIconSm = LoadIcon(wndclassex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wndclassex);
}
//Create a main windows
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance;
hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL
);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_SIZE:
cxClient = LOWORD (lParam);
cyClient = HIWORD (lParam);
InvalidateRect(hWnd, NULL,1);
SendMessage(hWnd, WM_PAINT, wParam, lParam);
break;
case WM_CREATE:
hdc=GetDC(hWnd);
InvalidateRect(hWnd, NULL, 1);
SendMessage(hWnd, WM_SIZE, wParam, lParam);
ReleaseDC(hWnd, hdc);
break;
case WM_LBUTTONDOWN:
if(y < cyClient / 2)
y = y - 10;
InvalidateRect(hWnd, NULL, 1);
SendMessage(hWnd, WM_PAINT, wParam, lParam);
break;
case WM_RBUTTONDOWN:
if(y > 0)
y = y + 10;
InvalidateRect(hWnd, NULL, 1);
SendMessage(hWnd, WM_PAINT, wParam, lParam);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
if(y > 0)
y = y - 10;
InvalidateRect(hWnd, NULL, 1);
SendMessage(hWnd, WM_PAINT, wParam, lParam);
break;
case VK_DOWN:
if(y < cyClient / 2)
y = y + 10;
InvalidateRect(hWnd, NULL, 1);
SendMessage(hWnd, WM_PAINT, wParam, lParam);
break;
}
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
hbm=(HBITMAP)LoadImage(NULL, "forest.bmp", IMAGE_BITMAP, 256, 256, LR_LOADFROMFILE);
hdcm=CreateCompatibleDC(hdc);
if(y <= 0)
TextOut(hdc, 0, 0, str1, sizeof(str1));
else if(y > 0 && y < cyClient / 2)
{
SelectObject(hdcm,hbm);
BitBlt(hdc, 200, y, 320, 320, hdcm, 0, 0, SRCCOPY);
}
else if(y >= cyClient / 2)
TextOut(hdc, 0, 0, str2, sizeof(str2));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
2 Add a resource script according to the source code (the steps for the .rc file are the same as adding a menu), you can create your own icon after the addition is successful.
3 Introduce bitmap (place it directly under the DEBUG folder):
4 The result is shown in the figure:
Click the left/right mouse button or the ↑/↓ key on the keyboard to move the picture. If it exceeds the screen, a warning as shown in the figure appears: