![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
ActiveX MethodsTo add a new method to the Writer2 control, select the Add method button in ClassWizard to open the Add method box. Give this new method the External name Clear and set the Return type to void, as also shown in Figure 13.8. Then click OK to create this new method. void CWriter2Ctrl::Clear() { // TODO: Add your dispatch handler code here }
If another program creates a member object to our Writer2 ActiveX control, that program can use our Clear() method this way: Writer.2Clear(). To actually clear the text in the text box control, we use the SetWindowText() function. void CWriter2Ctrl::Clear() { SetWindowText(); ⇐ }
Now weve added both a property and a method to our ActiveX control. Next, we add a new event, the Change event. ActiveX EventsTo add a new event to the Writer2 control, click the ActiveX Events tab in ClassWizard and select the Add Event button to open the Add Event box, as shown in Figure 13.9. Give this new event the External name Change and click OK. This creates a new event, the Change event. We want to make this event occurcalled firing the eventwhen the user types anything into the Writer2 control, so we connect a handler function to the WM_KEYDOWN message. void CWriter2Ctrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default COleControl::OnKeyDown(nChar, nRepCnt, nFlags); } To fire the Change event, we call the FireChange() function that ClassWizard has added to our program. void CWriter2Ctrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { FireChange(); ⇐ COleControl::OnKeyDown(nChar, nRepCnt, nFlags); } Now that weve created the Writer2 control and given it a Length property, a Clear() method, and a Change event, its time to put this control to work. Creating Writer2AppWe use the Writer2 control in a new program, Writer2App. Create that project, basing its view on the CFormView class.
Next, open the Components and Controls Gallery and add the Writer2 control to the project, accepting the new class Visual C++ proposes for this control: CWriter2. Finally, open the main form window and add a new Writer2 control to the window as well as a text box and a button, giving that button the caption Clear text, as shown in Figure 13.10. Now use ClassWizard to add a new member variable to the Writer2 control. Click the Member Variables tab in ClassWizard and select the IDC_WRITER2-CTRL1 entry, adding a new member variable to the Writer2 control with the name m_writer2. Now we can access our controls methods and properties conveniently. We need to make the text box in Writer2App active first. When the user enters text in the Writer2 control, we can use that controls Change event to display the new texts length in Writer2Apps text box. Click the Message Maps tab in Class-Wizard and find the IDC_WRITER2CTRL1 entry. There is one event associated with that control: Change. Add a new handler function to that event now. void CWriter2AppView::OnChangeWriter2ctrl1() { // TODO: Add your control notification handler code here } We want to display the text length in the Writer2 control, and we do that with the controls Length property. Using ClassWizard again, connect a CString variable to Writer2Apps text box and name that variable m_LengthText. Now when the user types into the Writer2 control, we can get the new length of the text by calling m_writer2s GetLength() function. (Note that we add 1 to the value returned by GetLength(), since that function returns the current length of the text in the control before the current key is added.) void CWriter2AppView::OnChangeWriter2ctrl1() { char text[30]; ⇐ wsprintf(text, Text length = %d, m_writer2.GetLength() + 1); ⇐ . . . Then we display the new text length in Writer2Apps text box. void CWriter2AppView::OnChangeWriter2ctrl1() { char text[30]; wsprintf(text, Text length = %d, m_writer2.GetLength() + 1); m_LengthText = text; ⇐ UpdateData(false); ⇐ } Finally, we enable the Clear text button in Writer2App. Attach a handler function to that button now. void CWriter2AppView::OnButton1() { // TODO: Add your control notification handler code here } Here, all we have to do is to use m_writer2s Clear() method to clear the text in the Writer2 control and update the text in the text box. void CWriter2AppView::OnButton1() { m_writer2.Clear(); ⇐ m_LengthText = Text length = 0; ⇐ UpdateData(false); ⇐ }
That completes Writer2App. Run the program now, as shown in Figure 13.11, and type some text into the Writer2 control. Doing so fires the Change event, and that updates the text in the text box, which indicates the texts length. In addition, clicking the Clear text button invokes the Writer 2 Clear() method and clears the text in the Writer2 control. Through our successful Writer2 control, weve been able to support ActiveX properties, methods, and events. Thats the real core of ActiveX programming; with this support, were able to let our controls interact with the programs theyre embedded in and do what they are designed to do, when theyre supposed to do it.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |