Objective
This article takes the process of the poker control as an example, introduces the detailed process of the MFC ActiveX control, and hopes to help the friend who writes the learning control.
First, the production of Poker control
1, a new "MFC ActiveX ControlWizard" project. The project name is named cards, and then the default value of the wizard is used to generate the project.
2, for the project to add 54 cards bitmap and poker background bitmap resources. Note the bitmap has a idb_cards bitmap for the control display when the icon, you can modify their own favorite pattern.
3, fill in the property value for the control, open View->classwizard. Click the Automation tab. Select Ccardsctrl from the class name Drop-down box and click the Add Property button to eject the Properties Add dialog box. Enter Value,type in external name to the short type. At the same time, choose Get/set methods in the implementation, and there are two other items, regardless of whether it is set up by its own function on the line. Now we've added a property to the control that handles the number of cards.
4, according to the third step of the method, we then set a number of properties, respectively, is backbmp, the type is short, used to deal with the background pattern is what; background, type bool, used to handle the display card front or reverse.
5, the property is added, the following set the initial values for these properties: Open the CardsCtl.cpp file, in the constructor Ccardsctrl::ccardsctrl () Add code: value=1;backbmp=1;
6, in order to enable the control in the program normal display, we also need to modify the Ccardsctrl::ondraw. The revised contents are as follows:
void CCardsCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
CBitmap *pOldBitmap;
CBitmap bitmap;
CRect rect;
GetClientRect(rect);
CDC ppdc;
CClientDC dc(this);
ppdc.CreateCompatibleDC(&dc);
if(background)//背景图案
{
if(backbmp==1)bitmap.LoadBitmap(255);
if(backbmp==2)bitmap.LoadBitmap(256);
}
else//前景图案
bitmap.LoadBitmap(IDB_BITMAP1+value-1);
pOldBitmap=ppdc.SelectObject(&bitmap);
dc.BitBlt(0,0,rect.Width(),rect.Height(),&ppdc,0,0,SRCCOPY);
ppdc.SelectObject(pOldBitmap);
}
In the code above, note that the bitmap. LoadBitmap (idb_bitmap1+value-1); statement, make sure that you insert bitmap resources in the resource continuously. You can open the Resource.h file to see if your bitmap resources are contiguous.