MFC (ActiveX programming, Sun Xin C ++ 18th lecture notes)

Source: Internet
Author: User
Tags textout

1. Call the X Control in VB and add project-> Add
Components. In addition, you can use Object Browser to view controls.

 

Ii. Create an X Control in VC

1. Create an X project named clock. Note that a file can contain multiple controls.

2. Keep the default settings. Pay attention to the three classes it generates and related interfaces.

3. Run it. Select tstcon32.exe as the container.

4. Select Insert control. Now we can see that it draws an elliptic. You can also test it in VB.

5. Delete registration information. Use regsvr32/u + file name. You can also select the anti-register command from the menu.

6. Rewrite the code. Draw an ellipse in cclockctrl: ondraw (). In this case, we get the system time and display it. Therefore, we set a timer in oncreate () to send an invalidate () message at a certain time so that the window is repainted.

7. How can I change the background color and foreground color of the control? Classwizard-> automation-> Add property-> backcolor, you also need to add the corresponding code in ondraw ()

Cbrush brush (translatecolor (getbackcolor ()));

PDC-> fillrect (rcbounds, & brush );

PDC-> setbkmode (transparent );

PDC-> settextcolor (translatecolor (getforecolor ()));

8. added the property page. In

Begin_proppageids (cclockctrl, 2) the number must be changed to the corresponding number.

Proppageid (cclockproppage: guid)

Proppageid (clsid_ccolorproppage)

End_proppageids (cclockctrl) OK ~

9. Add a custom property: classwizard-> automation-> Add property with the variable m_interval. The type is short and the corresponding external variable is interval. Add the onintervalchanged method to cclockctrl. Add the following code:

If (m_interval <0 | m_interval> 6000)

{

M_interval = 1000;

}

Else

{

M_interval = m_interval/1000*1000;

Killtimer (1 );

Settimer (1, m_interval, null );

Boundpropertychanged (0x1 );

}

10. Test: Control-> invoke Methods

11. Add the time interval to the attribute page, and add a text box and edit box to the resource view. Associate member variables with editbox and add the attribute interval.

12. Add method: classwizard-> automation-> Add method-> Hello Add code OK! This method can be called in VB!

Void cclockctrl: Hello ()

{

// Todd add your dispatch handler code here

MessageBox ("Hello world! ");

}

13. Add events: classwizard-> automation-> Add events-> click

14. added custom events: classwizard-> automation-> Add events-> newminute

Send this notification when the next minute arrives, and write the code in ondraw:

Ctime time = ctime: getcurrenttime ();

If (0 = time. getsecond ())

{

Firenewminute ();

}

15. Make the interval attribute persistent. Call the px_short () method in cclockctrl: dopropexchange (), OK!

Px_short (ppx, "interval", m_interval, 1000 );

16. Method for keeping the interval in the property page and property consistent: Call boundpropertychanged (0x1) in onintervalchanged );

17. Methods to keep the control from moving during the design time: In ontimer (), if (ambientusermode () invalidatecontrol (); clever!

 

 

3. Call the X Control in VC

1. Create a clocktest Dialog Box Application

2. Right-click and choose insert X control> clock control.

3. Project-> Add component will generate the cclock class.

4. Add the member variable m_clock of the cclock class to cclocktestdlg, and then you can dynamically create such a thing!

5. Test the click (), newminute (), setbkcolor (), setforecolor () methods and attributes.

6. How to respond to events for dynamically created controls? First, you need to know its ID, and then refer to the non-dynamic control Event code.

 

The specific code is as follows:

1. Create an MFC ActiveX controwizard project and run it directly to create an ActiveX control. Its function is to generate an elliptical

Running, directly registered to the system, so the generated ActiveX control can be found elsewhere

This is tested in VB in vs2008.

Right-click ----> select item ---->

 

In this case, the toolbox has an additional control.

Drag to the window. The effect is as follows:

The project file debug contains:

 

Input

Drag the ocx file above, press OK, and decompile it to remove it from the registry.

 

 

Run mfcxxx and the ActiveX cannot be found.

 

Use register contrl under tools to register ActiveX

Or run regsvr32 and drag the ocx file to register it. Just like above, the/u file is removed.

 

Start to do your own | ActiveX

Void catestctrl: ondraw (CDC * PDC, const crect & rcbounds, const crect & rcinvalid) {// todo: Replace the following code with your own drawing code. // PDC-> fillrect (rcbounds, cbrush: fromhandle (hbrush) getstockobject (white_brush); // PDC-> ellipse (rcbounds ); // The above two statements are the default values, and the elliptic ctime c = ctime: getcurrenttime (); cstring = C. format ("% H: % m: % s"); PDC-> textout (0, 0, cstring );}

For dynamic display of a static time, the settimer function and wm_timer function must be added.

Add a wm_create file in cxxctrl and set the settimer

Settimer (1,1000, null );

Add wm_timer

void CATestCtrl::OnTimer(UINT nIDEvent) {// TODO: Add your message handler code here and/or call default//Invalidate();InvalidateControl();COleControl::OnTimer(nIDEvent);}

At this time, a dynamically displayed clock will be displayed.

 

Using the newly created ActiveX in VB, we found that there are only a few properties.

In this case, we can add our own attributes:

 

At this time, Visual Basic in vs can display the response attributes.

 

At this time, you need to add the corresponding code in ondraw. Otherwise, setting the background color and foreground color does not work.

CBrush cbrush(TranslateColor(GetBackColor()));pdc->FillRect(rcBounds,&cbrush);pdc->SetBkMode(TRANSPARENT);pdc->SetTextColor(TranslateColor(GetForeColor()));

 

Void catestctrl: ondraw (CDC * PDC, const crect & rcbounds, const crect & rcinvalid) {// todo: Replace the following code with your own drawing code. // PDC-> fillrect (rcbounds, cbrush: fromhandle (hbrush) getstockobject (white_brush); // PDC-> ellipse (rcbounds ); // The above two statements are the default ones. Draw the Oval cbrush (translatecolor (getbackcolor (); PDC-> fillrect (rcbounds, & cbrush ); PDC-> setbkmode (transparent); PDC-> settextcolor (translatecolor (getforecolor (); ctime c = ctime: getcurrenttime (); cstring = C. format ("% H: % m: % s"); PDC-> textout (0, 0, cstring );}

Manually add property page

BEGIN_PROPPAGEIDS(CATestCtrl, 2)PROPPAGEID(CATestPropPage::guid)PROPPAGEID(CLSID_CColorPropPage)END_PROPPAGEIDS(CATestCtrl)

 

 

Add custom attributes:

 

void CATestCtrl::OnJianGeChanged() {// TODO: Add notification handler codeif(m_jianGe<0||m_jianGe>6000){m_jianGe=1000;}else{m_jianGe=m_jianGe/1000*1000;KillTimer(1);SetTimer(1,m_jianGe,NULL);}SetModifiedFlag();}

 

Some of the property panels can be viewed in jiange, while others cannot.

Add this to the property page

Add Association

 

The last item is the property name.

 

You can add (standard and custom) events and functions for ActiveX.

 

Add this class in MFC

Related Article

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.