![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Thats it for handling keys. We still need to add the drawing code to the ActiveX control, which we do in the OnDraw() function. Displaying Data in an ActiveX ControlThe default OnDraw() function placed in our program by the ActiveX Control-Wizard simply draws a white rectangle and displays an ellipse in that rectangle. void CWriterCtrl::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); } Here, the rcBounds rectangle holds the dimensions of the controls bounding rectangle. We make use of those dimensions ourselves as we draw a rectangle surrounding the ActiveX control (that is, to make it visible in case it appears in a white window). void CWriterCtrl::OnDraw( CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid) { pdc->Rectangle(rcBounds); ⇐ . . . } Then we simply draw the text in the CString text object, indenting that texts position slightly to avoid obscuring the surrounding rectangle. void CWriterCtrl::OnDraw( CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid) { pdc->Rectangle(rcBounds); pdc->TextOut(2, 2, text); ⇐ } That completes the code for the Writer control. Building the control with Visual C++ automatically registers it with Windows and creates the actual ActiveX control itselfWriter.ocx. (The .ocx extension is left over from the days when such controls were called OLE controls, not ActiveX controls.) Now that we have our new ActiveX control, we can test it with the ActiveX Control Test Container, which is found in the Visual C++ Tools menu. Testing ActiveX ControlsOpen the Test container now and select the Insert New Control item from the Edit menu to open the Insert Control box. Select the entry Writer Control and click OK to insert a new Writer control in the Test container, as shown in Figure 13.2. As also shown in Figure 13.2, you can type into the new ActiveX control, resize it, move it, and so on. Our ActiveX control is a success; now were creating ActiveX controls in Visual C++. We still havent embedded the Writer control into a program yet, however, and ActiveX controls are designed to be used, so lets put Writer to use now in a new program, WriterApp. We embed a Writer control in WriterApp and type into that new control.
Creating WriterAppUsing AppWizard, create a new SDI program named WriterApp. To make it easy to embed the Writer control into WriterApp, base WriterApps View class on CFormView. To add the Writer control to the new WriterApp project, select the Project menus Add to Project item, which opens the Components and Controls Gallery as shown in Figure 13.3. Double-click the Registered ActiveX Controls folder and find the entry for the Writer control in that folder. Double-click that entry; Visual C++ asks you for a name for the new class corresponding to our control. Accept the default, CWriter (you can use this class to make a member object of the new Writer control in WriterApp). Now open the IDD_WRITERAPP_FORM resource from the Dialog folder in ResourceView, as shown in Figure 13.4. This is the main window for WriterApp. As you can see at right in Figure 13.4, our new ActiveX control appears in the control toolbox at the bottom left, with the letters OCX. Use that new tool to draw a Writer control in the WriterApp form now.
Thats all it takes because the Writer control is self-contained. Start the program now and type something into the Writer control, as also shown in Figure 13.5. You can see the typed text appear in the new controlWriter is a success. Were creating our own ActiveX controls.
|
![]() |
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. |