DX notes Five------drawing bitmap of the game screen drawing

Source: Internet
Author: User

This series of articles prepared by zhmxy555, reproduced please indicate the source. http://blog.csdn.net/zhmxy555/article/details/7335103

Total Four Steps

Step One: Load bitmap

Step two: Establish a memory DC compatible with the Windows DC

Step three: Select Bitmap objects

Step Four: Stickers

Detailed steps

Step One: Load bitmap

To load a bitmap from a file, you often use the LoadImage () function.

HANDLE LoadImage (

HINSTANCE hinst,//module handle containing the DLL or EXE file for the target bitmap

LPCTSTR LpszName,

UINT Utype,

int cxdesired,

int cydesired,

UINT Fuload

);

The following is a detailed description of the function parameter.

▲hinstance Source entity: Contains the entity where the bitmap resides, and the bitmap to load is set to "NULL" in the hard disk or resource file.

▲lpctstr Name: The path to the bitmap to be loaded and the file name or resource name

▲uint Bitmap Type: Load the bitmap type with the following three types:

★image_bitmap: The loaded bitmap is a generic diagram file with the extension ". bmp"

★image_cusor: The loaded bitmap is the cursor icon with the extension ". Cur"

★image_icon: The loaded bitmap is an icon with the extension ". ico"

▲int Load Width: The width of the bitmap load, in pixels

▲int Load Height: The height of the bitmap load, in pixels

▲uint Load Mode: Set the location map loading mode, if the bitmap is loaded from the file, then set to "Lr_loadfromfile"

Step Two: Establish a memory DC compatible with the Windows DC

We call the Createcompatible () function to establish the memory DC

HDC CreateCompatibleDC (HDC hdc);//Establish a compatible DC

The only parameter that is output in the function is the DC that is intended to be compatible with the memory DC

As with the window DC, the memory DC must also be released after the operation, freeing the memory DC to call the function DeleteDC ()

DeleteDC (HDC DC name);//Release DC

Step Three: Select Bitmap Objects

The bitmap object is one of GDI's 6 objects, and the memory DC uses the bitmap object in the same way as the brush or brush that was described earlier, all by calling the SelectObject () function.

GDI objects are: brushes, brush, bitmap, font, area and color palette, etc.

Step four: Stickers

Copy the bitmap from the memory DC to the displayed DC, which is the map. The function used by this operation is BitBlt (), which we will use frequently. This function is defined as follows

BOOL BitBlt (

int x,//destination DC x coordinate

int y,//destination DC y-coordinate

int nwidth,//width attached to the destination DC

int nheight,//height attached to the destination DC

cdc* PSRCDC,//source DC

int XSRC,//source DC x coordinate

int YSRC,//source DC y-coordinate

DWORD dwrop//Map mode ();

);

#include <windows.h>//global variable Declarationhinstance HInst; Hbitmap hbmp; HDC MDC;//Declaration of a global functionATOM MyRegisterClass (hinstance hinstance); BOOL InitInstance (HINSTANCE,int); LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);voidMypaint (hdc hdc);//****winmain function, program entry point function **************************************intapientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCm Dline,intncmdshow)    {msg msg;    MyRegisterClass (HINSTANCE); if(!InitInstance (HINSTANCE, nCmdShow)) {        returnFALSE; }    //message Loops     while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg); }        returnMsg.wparam;}//****﹚ Design a window class, similar to the fill-in question, using the window structure body *************************ATOM MyRegisterClass (hinstance hinstance) {wndclassex Wcex; Wcex.cbsize=sizeof(Wndclassex); Wcex.style= Cs_hredraw |Cs_vredraw; Wcex.lpfnwndproc=(WNDPROC) WNDPROC; Wcex.cbclsextra=0; Wcex.cbwndextra=0; Wcex.hinstance=hinstance; Wcex.hicon=NULL; Wcex.hcursor=NULL; Wcex.hcursor=loadcursor (NULL, Idc_arrow); Wcex.hbrbackground= (Hbrush) (color_window+1); Wcex.lpszmenuname=NULL; Wcex.lpszclassname= TEXT ("Canvas"); WCEX.HICONSM=NULL; returnRegisterClassEx (&Wcex);}//* * * Initialize function *************************************//1. Establishing a memory DC compatible with the Windows DC//2. Load the bitmap from the file and coexist to the memory DCBOOL InitInstance (HInstance hinstance,intncmdshow)    {HWND hwnd;    HDC hdc; HInst=hinstance; HWnd= CreateWindow (TEXT ("Canvas"), TEXT ("drawing window"), Ws_overlappedwindow, Cw_usedefault,0, Cw_usedefault,0, NULL, NULL, HINSTANCE, NULL); if(!hWnd) {        returnFALSE; } MoveWindow (HWnd,Ten,Ten, -, -,true);    ShowWindow (HWnd, ncmdshow);    UpdateWindow (HWND); HDC= GetDC (hWnd);//window DCMDC = CreateCompatibleDC (HDC);//window-compatible memory DChbmp= (HBITMAP) LoadImage (Null,text ("bg.bmp"), Image_bitmap, -, -, lr_loadfromfile);//Loads an icon, cursor, animated cursor, or bitmap//loading icons, cursors, animated cursors, or bitmaps; loading bitmapsSelectObject (mdc,hbmp);//use GDI objects to return previously used GDI objects//select Bitmap object;Mypaint (HDC);//StickersReleaseDC (HWND,HDC);//Delete GDI Object Delete successfully returns Boolean "Ture" If Failure returns "FALSE"    returnTRUE;}//* * * Custom drawing function *********************************voidMypaint (hdc hdc) {BITBLT (hdc,0,0, -, -Mdc0,0, srccopy);//using the BitBlt function map}//* * * Message handler function **********************************LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {paintstruct ps;    HDC hdc; Switch(message) { CaseWM_PAINT://window Redraw MessageHDC = BeginPaint (hWnd, &PS);            Mypaint (HDC); EndPaint (HWnd,&PS);  Break;  CaseWm_destroy://window End MessageDeleteDC (MDC);            DeleteObject (hbmp); PostQuitMessage (0);  Break; default://Other Messages            returnDefWindowProc (hWnd, message, WParam, LParam); }   return 0;}

Paste the results

Feel your own Meng da! Thank you for the light ink, hahaha ...

DX notes Five------drawing bitmap for game graphics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.