实验描述:
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。)
- 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. (当鼠标左键单击/右键单击或按键盘上的上/下箭头时,位图会上/下移动,当位图移动到窗口的上/下限时,会出现一些警告。)
1 源代码如下:
#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 根据源代码添加资源脚本(.rc文件 步骤与添加菜单相同),添加成功后可以创建自己的图标。
3 引入位图(直接放在DEBUG文件夹下):
4 结果如图:
点击鼠标左/右或键盘↑/↓键移动图片,若超出屏幕,出现如图警告: