MFC drawing cbitmap/hbitmap/cimage/cstatic/cdc/hdc-Five methods of painting to static

Source: Internet
Author: User


Reference: http://bbs.csdn.net/topics/30173861



Reference: http://www.360doc.com/content/13/0507/21/10724725_283723432.shtml



Owed by: Spring Night rain Http://blog.csdn.net/chunyexiyu reprint, please indicate the source



The graphics used are drawn to the device (CDC), and also need to bind the drawing before drawing (CBitmap) First Introduction: HDC/CDC conversion Cbitmap/hbitmap/bitmap Conversion



Hdc:handle to a Device the context (DC). Handle to Device DC



Cdc:defines a class of Device-context objects. Encapsulating device DC



Cbitmap:encapsulates a Windows Graphics Device interface (GDI) bitmap and provides member functions to manipulate the BI  Tmap. Encapsulation bitmap



Hbitmap:handle to a bitmap. The handle of the bitmap



Bitmap:this class inherits from the Image class. Graphics, drawings



On the transition relationship:



CDC->HDC Cbitmap->hbitmap are all the same methods obtained by Getsafexx () handle



HDC HDC = DC. GETSAFEHDC ();



Hbitmap hbitmap= hbitmap (bitmap.   Getsafehandle ()); dc. Detach will also return a hbitmap pointer



HDC->CDC Hbitmap->cbitmap methods are similar, usually using Attach/detach to complete use and shutdown



CDC DC; dc.  Attach (HDC); DC after the use is complete. Detach ().



CBitmap Bitmap; Bitmap. Attach (HBITMAP); Bitmap after use. Detach ();



Cbitmap->bitmap: Through the Getbitmap to obtain the Bitmap, form such as: Bitmap bmp; Bitmap. Getbitmap (BMP);



Hbitmap->bitmap: Through the GetObject to obtain the Bitmap, form such as: Bitmap bmp; GetObject (hbitmap, sizeof (BITMAP), (LPSTR) &bmp);



Bitmap->hbitmap: Obtain hbitmap through Gethbitmap, form: BMP. Gethbitmap (RGB (0,0,0), &hbitmap);



Construction Equipment & selection drawings: SelectObject



When we create a memory DC, if there is no binding drawing, it is not possible to draw information, the method of binding the drawing is as follows: Shape like memdc.selectobject (&membitmap);


	CDC MEMDC;						Defines a display device object
	CBitmap Membitmap;						Defines the bitmap
	Memdc.createcompatibledc (m_static. GetDC ());			Create CDC compatible device
	Membitmap.createcompatiblebitmap (m_static. GetDC (), iwidth,iheight); 	Create a CDC-compatible picture
	<strong>memdc.selectobject (&membitmap);</strong>					//Equipment Select Current drawing-Bitmap


Next, you can use MEMDC to draw, call Memdc.lineto/draw3drect, and so on, after the drawing is finished, you can use the following bitblt to come to the static box



M_static. GetDC ()->bitblt (0,0,200,200,&memdc,50,50,srccopy);



Additional instructions: The BitBlt method can copy information from a piece of a drawing in a device DC and paste it into another device DC



(Note that Crearecompatibledc corresponds to DELETEDC release)



(Note that CreateCompatibleBitmap corresponds to DeleteObject release)






The implementation of the requirements and the implementation method is presented: requirements: A Picture component box (bound CStatic variable) is added to the dialog to display a picture into this



Implementation Method 1: borrow the support of the picture in CImage



A. Modifying the static box to bitmap mode



Setting Properties-Miscellaneous-type:bitmap



(You can also add property modifications to the OnInitDialog code: M_staticpic.modifystyle (ss_enhmetafile, ss_bitmap| Ss_centerimage);



B. Load picture & set to static, in OnInitDialog


<strong>		CImage image;				Use picture class   
		image. Load (m_cstrpicname);			Load path under the picture information to the picture class  
		M_staticpic.setbitmap (image. Detach ());</strong>





Implementation Method 2: Import into the project bitmap file, borrow LoadBitmap



A. The same modification cstatic box is bitmap mode


	M_static. ModifyStyle (ss_enhmetafile,ss_bitmap| Ss_centerimage);


B. Load picture & set to Static


	Hbitmap Hbitmap=::loadbitmap (AfxGetApp ()->m_hinstance,makeintresource (idb_bitmap_test)); Load Picture
	m_static. ModifyStyle (<strong>ss_enhmetafile</strong>,ss_bitmap| Ss_centerimage);
	





Implementation Method 3: Loading pictures with the help of Gdiplus::bitmap



A. The same modification cstatic box is bitmap mode


	M_static. ModifyStyle (ss_enhmetafile,ss_bitmap| Ss_centerimage);


B. Initializing gdi/in constructors closing GDI


DLG the variable 
    	ulong_ptr M_gdiplus;
Initialization of GDI in constructors:
	gdiplus::gdiplusstartupinput input;  
	Gdiplus::gdiplusstartup (&m_gdiplus,&input,null); 
Closing GDI in destructor:
	Gdiplus::gdiplusshutdown (M_gdiplus);  


C. Use GDI-related functions to open files & set to static box


	Gdiplus::bitmap Gdibitmap (cstrpathfile);  	Incoming picture path
	hbitmap hbitmap;
	Gdibitmap.gethbitmap (Gdiplus::color (255, 255, 255), &hbitmap);
	M_static. SetBitmap (HBITMAP);





Implementation method Four: direct drawing in OnPaint



A. Overload the OnPaint function of dialog, add code to the OnPaint function: for example, draw a colored box



Declare a CPAINTDC, bind the static box, and then draw


	CPaintDC dcstatic ((cwnd*) &m_static);
	Dcstatic.fillsolidrect (0, 0, N, RGB (0,255,0));






Implementation method Five: Use CBitmap memory to create a graphic, binding to the static box



A. The same modification cstatic box is bitmap mode


	M_static. ModifyStyle (ss_enhmetafile,ss_bitmap| Ss_centerimage);


B. Create graphics & graphics bindings to static boxes


	CDC MEMDC;						Defines a display device object
	CBitmap Membitmap;						Defines the bitmap
	Memdc.createcompatibledc (m_static. GetDC ());			Create CDC compatible device
	Membitmap.createcompatiblebitmap (m_static. GetDC (), iwidth,iheight); 	Create a CDC-compatible picture
	cbitmap* poldbmp = Memdc.selectobject (&membitmap);					Equipment Select Current drawing-Bitmap
	memdc.draw3drect (0, 0, iwidth, iheight, RGB (0,0,0), RGB (225,225,225));	Drawing information on the drawing
	<strong>m_static. SetBitmap (&membitmap);</strong>
	membitmap.deleteobject ();
	Membitmap.detach ();  Memdc.selectobject (poldbmp); Memdc.deletedc ();


The last step is to bind the static box, or you can copy a piece of information directly from the MEMDC by using the duplicate method:



M_static. GetDC ()->bitblt (0,0,200,200,&memdc,50,50,srccopy);






Implementation Method VI: Use CImage to create a graphic in memory and bind to the static box



A. The same modification cstatic box is bitmap mode


	M_static. ModifyStyle (ss_enhmetafile,ss_bitmap| Ss_centerimage);


B. Creating graphics, using SETPIXELRGB to set colors


	int iwidth =, iheight =;
	CImage image;
	Image. Create (iwidth, iheight,);
	for (int i=0; i<iwidth; i++) for
		(int j=0; j<iheight; j + +)
		{
			image. Setpixelrgb (i,j,0,255,0);
		}
	M_static. SetBitmap (image. Detach ());





Implementation of the method seven: CImage and CBitmap mixed to achieve a similar watermark effect



A. The same modification cstatic box is bitmap mode


	M_static. ModifyStyle (ss_enhmetafile,ss_bitmap| Ss_centerimage);


B. Create CImage, draw some information first


int iwidth =, iheight =;
	CImage image;
	Image. Create (iwidth, iheight,);
	for (int i=0; i<iwidth; i++) for
		(int j=0; j<iheight; j + +)
		{
			image. Setpixelrgb (i,j,255,255,0);
		}


C. Create memory Dc,cimage bind to MEMDC as CBitmap, draw additional information


	CDC MEMDC;									
	Memdc.createcompatibledc (GetDC ());
	Memdc.selectobject (image. Detach ());  or hbitmap Hmap = image. Detach (); Memdc.selectobject (HMAP);
	Memdc.fillsolidrect (0, 0, iwidth, IHEIGHT/2, RGB (0,255,0)); Draw some additional information


D. Binding to static boxes


M_static. SetBitmap (Hbitmap (Memdc.getcurrentbitmap ()->getsafehandle ());   or m_static. SetBitmap (HMAP);


Note: If you use Cbitmap.attach to accept Image.detach, you need to pay attention to CBitmap detach, otherwise the CBitmap lifecycle ends, releasing resources, resulting in pictures not showing





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.