Application experiment of resource in program design based on SDK
First, Experimental Purpose
1. Master the application of various resources and program design method of resource application.
Ii. contents of the experiment and Steps
Experimental tasks
1. Familiar with the creation process of menu resources;
2. Familiar with the creation of bitmap resources;
3. Familiar with the application of dialog box resources;
4. Be familiar with icon resources.
Experimental content
Loads an arbitrary bitmap in the center of a window, the bitmap size is One-fourth of the window area, and when you click the left mouse button or the up arrow on the keyboard, the bitmap moves up, and when you move to the top edge of the window, the window displays " no more moving up. " typeface, when you click the right mouse button or the down arrow on the keyboard, the bitmap moves down, and when you reach the bottom border of the window, the screen displays the word " no More moves down " .
Program code:
#include <windows.h>
Long WINAPI WndProc (HWND hwnd,uint iMessage,
UINT Wparam,long LParam); The message-handling function declaration.
BOOL Initwindowsclass (hinstance hinstance); Initializing a window class declaration
BOOL initwindows (hinstance hinstance, int ncmdshow); Initializing a window declaration
HWND Hwndmain; Defining window Handles
HDC hdc; Defining a device environment handle
HDC Hdcmem; Defining memory Handles
Hbitmap hBm; Defining a bitmap handle
BITMAP BM; Defining Bitmap Structure variables
int iy=80; Initial y-coordinate of the upper-left corner of the bitmap
int iwindowwidth,iwindowheight; Width and height of the window
Char cupwarn[]= "can no longer move upward"; Up warning string
Char cdownwarn[]= "can no longer move Down"; Down warning string
int WINAPI WinMain (hinstance hinstance,hinstance hprevinstance,
LPSTR lpcmdline,int nCmdShow)
{
MSG Message; Defining message Variables
Hbm=loadbitmap (hinstance, "Forest"); Loading bitmaps
GetObject (Hbm,sizeof (BITMAP), (LPVOID) &BM); Get bitmap Information
Iwindowwidth=2*bm.bmwidth; Get the width of the window
Iwindowheight=2*bm.bmheight; Get the height of the window
if (! Initwindowsclass (HINSTANCE))//Initialize window class
return FALSE;
if (! Initwindows (Hinstance,ncmdshow))//Initialization window
return FALSE;
while (GetMessage (&message,0,0,0))
{
TranslateMessage (&message); Message loops
DispatchMessage (&message);
}
return message.wparam;
}
Long WINAPI WndProc (HWND hwnd,uint imessage,uint wparam,long lParam)
{
Paintstruct PS; Defining structure variables that contain drawing information
Switch (iMessage)
{
Case WM_LBUTTONDOWN://Click left mouse button
iy=iy-10; Bitmap Height-10
InvalidateRect (hwnd,null,1); Refresh User Area
Break
Case Wm_rbuttondown://Click right mouse button
iy=iy+10; Bitmap height +10
InvalidateRect (hwnd,null,1); Refresh User Area
Break
Case WM_KEYDOWN:
Switch (WParam)
{
Case VK_UP://Press UP ARROW, Position-10
iy=iy-10;
Break
Case Vk_down://When the arrow is pressed, position +10
iy=iy+10;
Break
}
InvalidateRect (hwnd,null,1); Refresh User Area
Break
Case WM_CREATE://Initialize window message
HDC=GETDC (HWND); Get the device environment pointer
Hdcmem=createcompatibledc (HDC); Get Memory pointers
ReleaseDC (HWND,HDC); Remove device Environment pointer
Case WM_PAINT://Processing drawing messages
Hdc=beginpaint (HWND,&PS); Get the device environment pointer
if (IY>0&&IY<IWINDOWHEIGHT/2)//When the bitmap is complete in the window
{
SelectObject (HDCMEM,HBM); Select in memory handle
BitBlt (hdc,60,iy,bm.bmwidth,bm.bmheight,hdcmem,0,0,srccopy);//Output bitmap
}
else if (iy<=0)//when the upper bounds of the bitmap are outside the window
{
TextOut (hdc,0,0,cupwarn,sizeof (Cupwarn)); Output warning
}
else//When the lower border of the bitmap exceeds the window
{
TextOut (hdc,0,0,cdownwarn,sizeof (Cdownwarn)); Output warning
}
EndPaint (HWND,&PS); Output environment handle
Break
Case Wm_destroy://Close window message
DeleteObject (HBM); To delete a memory handle
PostQuitMessage (0);
return 0;
Default
Return (DefWindowProc (Hwnd,imessage,wparam,lparam));
}
return 0;
}
BOOL initwindows (hinstance hinstance, int ncmdshow)
{
HWND hwnd;
Hwnd=createwindow ("bitmapinstance",//window class name
"Bitmap Display Instance Program",//title bar name
Ws_overlappedwindow,//Window style
Cw_usedefault,//window upper-left corner coordinates
0,
Iwindowwidth,//With custom height and width
Iwindowheight,
NULL,//No parent window
NULL,//No Menu
HINSTANCE,//Current application instance handle
NULL); Pointer to a parameter passed to the window
if (!hwnd)//Create failed
return FALSE;
Hwndmain=hwnd; Pay a value to the global window handle
ShowWindow (hwnd,ncmdshow); Display window
UpdateWindow (HWND); Refresh User Area
return TRUE;
}
BOOL Initwindowsclass (hinstance hinstance)
{
Wndclass Wndclass;
wndclass.cbclsextra=0; Windowless class Extension
wndclass.cbwndextra=0; No window instance extension
Wndclass.hbrbackground= (Hbrush) (Getstockobject (White_brush)); Background is white
Wndclass.hcursor=loadcursor (Null,idc_arrow); The cursor is the arrow
Wndclass.hicon=loadicon (null,idi_application); Using the default icon
Wndclass.hinstance=hinstance; Current instance
Wndclass.lpfnwndproc=wndproc; Message-handling functions
Wndclass.lpszclassname= "Bitmapinstance"; Window class name
Wndclass.lpszmenuname= "Menu"; Define menu for
wndclass.style=0; Default window Style
Return registerclass (&wndclass); return window Registration value
The results of the program run :
Third, the experimental results
Through this experiment, I have mastered the application of various resources and application of resources program design method, familiar with the menu resources, Bitmap Resources, dialog resources, and icon resource creation and application process.
Chapter seventh application of resources in Windows programming P157 7-8