Classes can be inserted into any program. It provides a unified interface. The interface function receives three parameters: resource index number, length, and width of the image. Of course, you can also add a pointer parameter of the parent window to determine the Create behavior of the class object, so that you can specify the parent window that the image represents should be attached. To compile this class, you must be familiar with the use of the CRgn class. For example, the CombineRgn and setjavaswrgn member functions, refer to msdn for details. Well, next we will create this class step by step.
First, prepare an image to be displayed on the UI. Define a transparent color and use this color to fill the transparent part, the transparent color should be the least colored color in the image. Note that the transparent and non-transparent junction must be clear and there must be no transitional color. Here I use RGB (255, 0,255 ). As follows:
Use MFC AppWizard to create a dialog box program, add the image in the previous step to the resource tab, and create a new window class CMyWnd derived from generic CWnd. Add three private members to the class:
private:CRect m_rtWnd;CBitmap m_bitmap;BITMAP m_bmp;
Add two message processing functions for this class: WM_CREATE and wm_erasebkgnd:
Int cmywnd: oncreate (maid) {If (cwnd: oncreate (maid) =-1) Return-1; // todo: add your specialized creation code herecolorref CRL; getwindowrect (& m_rtwnd); crgn wndrgn, rgntemp; CDC * PDC = getwindowdc (); CDC dccompatible; dccompatible. createcompatibledc (PDC); cbitmap * poldbitmap = dccompatible. selectObject (& m_bitmap); wndrgn. createrectrgn (0, 0, m_rtwnd.width (), m_rtwnd.height (); For (INT x = 0; x <m_rtwnd.width (); X ++) {for (INT y = 0; Y <m_rtwnd.height (); y ++) {CRL = dccompatible. getpixel (x, y); If (CRL = transcolor) {rgntemp. createrectrgn (X, Y, x + 1, Y + 1); // cut off the transparent area wndrgn. combinergn (& wndrgn, & rgntemp, rgn_xor); rgntemp. deleteobject () ;}}// set the final window range setjavaswrgn (hrgn) wndrgn, true); dccompatible. selectObject (poldbitmap); releasedc (PDC); releasedc (& dccompatible); Return 0;} bool cmywnd: onerasebkgnd (CDC * PDC) {// todo: add your message handler code here and/or call defaultcdc dccompatible; dccompatible. createcompatibledc (PDC); cbitmap * poldbitmap = dccompatible. selectObject (& m_bitmap); PDC-> stretchblt (0, 0, numeric, m_bmp .bmheight, & dccompatible, 0, 0, m_bmp .bmwidth, m_bmp .bmheight, srccopy); dccompatible. selectObject (poldbitmap); releasedc (& dccompatible); Return true ;}
Add a function SetPic for external calls to this class:
void CMyWnd::SetPic(UINT nIDPic, int nLeft, int nTop){if(NULL != m_bitmap.m_hObject){m_bitmap.DeleteObject();}m_bitmap.LoadBitmap(nIDPic);m_bitmap.GetBitmap(&m_bmp);int nRight = m_bmp.bmWidth + nLeft;int nBottom = m_bmp.bmHeight + nTop;Create(NULL, NULL, WS_CHILD, CRect(nLeft, nTop, nRight,nBottom),AfxGetApp()->m_pMainWnd, nIDPic);ShowWindow(SW_SHOWNORMAL);}
Do not forget to add the following before the MyWnd. cpp file:
#define TRANSCOLOR RGB(255,0,255)
This is the defined purple color used for transparent color. In this way, the transparent bitmap class is finished. Now add a CmyWnd m_wnd to the header file of XXXDlg. h In the dialog box to cut the image's bearer window object, and add a code in the BOOL CTransBMPDlg: OnInitDialog () function:
m_wnd.SetPic(IDB_BITMAP1, 10, 10);
This is a big success. It's still simple. ^_^. Programming Environment: Visual C ++ 6.0 & MFC