Into the world of Windows programming-----Bitmap and Mapping mode

Source: Internet
Author: User

1-bit Graph usage 1.1-bit diagram introduction

Bitmap-Generates a bitmap file of the response by saving the color of each point on the image.

Raster diagram: Preservation of image lattice

Vector diagram: The save of a drawing command.

Use of 1.2-bit graphs

1. Loading Bitmap Resources

Loadbitap ();

2. Create a DC that prevents bitmaps

CreateCompatibleDC

3. Place the bitmap into the created DC

Slectobject

4. Draw the bitmap to the current DC

BitBlt

5. Remove the bitmap

Slectobject

6. Delete the created bitmap DC

DeleteDC

7. Delete Bitmap Resources

Deleteoject

WinBmp.cpp: Defines the entry point for the application. #include "stdafx.h" #include "WinBmp.h" #define max_loadstring 100//global variable: hinstance hinst;//current instance Tchar sztitle[max_ loadstring];//title bar Text tchar szwindowclass[max_loadstring];//main window class name//forward declaration of functions contained in this code module: Atommyregisterclass (hinstance HINSTANCE); Boolinitinstance (HINSTANCE, int.); LRESULT Callbackwndproc (hwnd, UINT, WPARAM, LPARAM); Int_ptr Callbackabout (hwnd, UINT, WPARAM, LPARAM); INT apientry _                     Twinmain (hinstance hinstance, hinstance hprevinstance, LPTSTR lpCmdLine, int ncmdshow) {unreferenced_parameter (hprevinstance); Unreferenced_parameter (lpCmdLine); TODO: Place the code here. MSG msg; Haccel hacceltable;//Initialization of global string LOADSTRING (HInstance, Ids_app_title, SzTitle, max_loadstring); LoadString (HInstance, Idc_winbmp, Szwindowclass, max_loadstring); MyRegisterClass (HINSTANCE);//Execution of application initialization: if (! InitInstance (HINSTANCE, nCmdShow)) {return FALSE;} hacceltable = Loadaccelerators (hinstance, Makeintresource (idc_winbmp));//main message loop: while (GetMessage (&msg, NULL, 0, 0)) {if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {TranslateMessage (&msg);D ispatchmessage (&msg);}} return (int) Msg.wparam;} Function: MyRegisterClass ()////Purpose: Registers the window class. Note:////This function and its usage are only required if you want//This code is compatible with the WIN32 system that was added to the "registerclassex"//function in Windows 95. It is important to call this function,//so that the application can get the associated//"well-formed" small icon. ATOM MyRegisterClass (hinstance hinstance) {wndclassex wcex;wcex.cbsize = sizeof (wndclassex); wcex.style= CS_HREDRAW | cs_vredraw;wcex.lpfnwndproc= wndproc;wcex.cbclsextra= 0;wcex.cbwndextra= 0;wcex.hinstance= hInstance;wcex.hIcon= LoadIcon (HInstance, Makeintresource (idi_winbmp)); wcex.hcursor= loadcursor (NULL, Idc_arrow); Wcex.hbrbackground= ( Hbrush) (color_window+1); wcex.lpszmenuname= makeintresource (idc_winbmp); wcex.lpszclassname= SzWindowClass; Wcex.hiconsm= LoadIcon (Wcex.hinstance, Makeintresource (Idi_small)); return RegisterClassEx (&wcex);}  Function: InitInstance (hinstance, int)////Purpose: Save the instance handle and create the main window////Note:////      In this function, we save the instance handle in the global variable and//create and display the main program window.   BOOL InitInstance (hinstance hinstance, int ncmdshow) {HWND hwnd; HInst = hinstance; Store instance handles in global variables 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;} void OnPaint (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {paintstruct PS = {'} '}; HDC hdc = BeginPaint (HWND,&PS);//Load bitmap resource Hbitmap hbmp = LoadBitmap (Hinst,makeintresource (IDB_BITMAP1));// Get the bitmap information bitmap bmpinfo = {'} '}; GetObject (Hbmp,sizeof (bmpinfo), &bmpinfo);//A DC that creates a bitmap. A DC that matches the specified HDC (the information contained in the DC is consistent) hdc HBMPDC = CreateCompatibleDC    (hdc);//place bitmap into Hbmpdchbitmap holdbmp = (hbitmap) SelectObject (hbmpdc,hbmp);/* Draw bitmap * BOOL BitBlt (* hdc hdcdest,//draw the destination DC handle * int nxdest,//is drawn for the purpose of the top left x coordinate * int nydest,//to draw the purpose of the upper left y coordinate * int nwidth,//The purpose of drawing the width * int nheight,//The purpose of drawing high * HD C hdcsrc,//NeedThe drawing of the DC handle * int nxsrc,//needs to draw the original x-coordinate * int nysrc,//need to draw the original y-coordinate * DWORD dwrop);//Plot the way */bitblt (hdc,100,100,10 0,100,HBMPDC,0,0,SRCCOPY);/* The stretched function */stretchblt of the bitmap (Hdc,200,200,200,200,hbmpdc,0,0,bmpinfo.bmwidth, bmpinfo.bmheight,srccopy);//Remove Bitmap SelectObject (hbmpdc,holdbmp);//Brush out DCDELETEDC (HBMPDC);//delete bitmap DeleteObject (HBMP); EndPaint (HWND,&PS);} Functions: WndProc (HWND, UINT, WPARAM, LPARAM)////Purpose: Handles the message of the main window. wm_command-processing Application Menu//wm_paint-Draw main window//wm_destroy-send exit message and return////lresult CALLBACK WndProc (HWND hwnd, UINT message , WPARAM WPARAM, LPARAM LPARAM) {int wmid, wmevent; Paintstruct PS; HDC hdc;switch (message) {case Wm_paint:onpaint (hwnd,message,wparam,lparam); Break;case Wm_command:wmid = LOWORD ( WParam); wmevent = HiWord (WParam);//Analysis Menu selection: switch (wmid) {case Idm_about:dialogbox (HInst, Makeintresource (idd_ AboutBox), hwnd, about); Break;case Idm_exit:destroywindow (hwnd); Break;default:return DefWindowProc (hwnd, message, WParam, LParam);} Break;case wm_destroy:postquitmessage (0); BreAk;default:return DefWindowProc (hWnd, message, WParam, LParam);} return 0;} The message handler for the About box. INT_PTR CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM); switch ( Message) {case Wm_initdialog:return (INT_PTR) true;case wm_command:if (LoWord (wParam) = = IDOK | | LoWord (wParam) = = IDCANCEL) {EndDialog (hdlg, LoWord (WParam)); return (INT_PTR) TRUE;} break;} Return (INT_PTR) FALSE;}


2 coordinate system and Mapping mode 2.1 coordinate system 2.1.1 Device coordinate system

device coordinate system in pixels, x positive direction from left to right, y positive direction from top down

Screen coordinate system: the upper-left corner of the screen is the Origin point.

Window coordinate system: the upper-left corner of the outermost border of the window is the origin point.

Customer area Coordinate system: The origin is the upper-left corner of the client area within the window.

2.1.2 Logical coordinate system

The coordinate system is defined in logical units, and the coordinates used in the drawing process are basically drawn in a logical coordinate system. By default, the device coordinate system remains the same, but can be modified.

2.2 Mapping mode

Mapping relationships between logical coordinates and device coordinates

Types of 2.2.1 Mapping patterns

Mm_text = = The default mapping method, the logical coordinate system and the device coordinate system are consistent.

mm_lometric–0.1mm X direction Right, y direction left

mm_himetric–0.01mm x direction to the right, y to the left.

mm_loeglish–0.1in, x direction to the right, y to the left.

mm_hienglish–0.01in x direction to the right, y to the left.

Mm_twips–1/1440/in x direction to the right, y to the left.

mm_isoropic– can specify the positive direction of the logical unit XY

X-Axis units = y-Axis units

mm_anisotropic– can specify logical units and XY positive direction x-axis units and y-axis units can be unequal

Use of 2.2.2 Mapping mode

1. Set a new mapping mode

Setmapmode (); Returns the original old mapping method

2. Drawing

3, restore the new mapping mode

Setmapmode ();


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.