RSS


[ Pobierz całość w formacie PDF ]
.m_bEnabled = true;m_bstrCaption = T2BSTR("Button");m_ModalResult = -1;m_OnOff = FALSE;m_StdButtonType = 0;// Set the initial control size.m_sizeExtent.cx = 2100;m_sizeExtent.cy = 580;// Register the updateSetDirty(true);FireViewChange();// Set the initialization variable so that we don't// initialize the control twice.m_bInitialized = true;// Return a non-error result.return S_OK;}Let s talk about the InitNew() method first.This is the method that the programmingenvironment calls when creating a new control.The first bit of code checks to ensure theFont property is defined.If not, the method assigns the current system font to the control.Initializing the other properties is as easy as assigning them a value.You ll want to be surethat all of the defaults you assign work together.All ATL controls start out as a moderatelylarge square, which isn t a very good shape for a pushbutton.The next bit of code assigns amore believable size to the control that roughly equates to the size of a standard Visual C++pushbutton.Once all of the initialization is complete, you must use the SetDirty() method toensure the control values get changed and the FireViewChange() method to ensure thechanges also get reflected in the control s appearance.Finally, the InitNew() method tellsthe control that it s initialized by setting m_bInitialized to true, then returns a non-error resultto the control.Another potential problem is the appearance of the control itself.You may not see thecontrol you d expected to see when you place it in the IDE.Part of the problem is that ATL makes no assumptions about how you want to create the control it creates the most basicform available without considering any special features of a subclassed control.With that inmind, you need to change the control s OnCreate() method as shown in Listing 6-14 to takebetter advantage of the inherent features that control has to offer.Listing 6-14LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM/*lParam*/, BOOL&/*bHandled*/){// Determine how big to make the control.// Tell Visual C++ that we'll be converting BSTR values.USES_CONVERSION;// Determine how big to make the control.RECT rc;GetWindowRect(&rc);rc.right -= rc.left;rc.bottom -= rc.top;rc.top = rc.left = 0;// Make sure you modify the default Create() method syntax// to create the kind of button that you want.m_ctlButton.Create(m_hWnd, rc,OLE2T(m_bstrCaption),BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP);// Return a non-error result.return 0;}For the most part you ll find that ATL creates a simple box for you to look at, and it s up toyou to do something with it.In this case, we add a caption to the pushbutton and give itsome of the standard pushbutton flags.The flags you add will depend on the type of controlyou re creating.In some cases, the default ATL settings will work just fine; but in most cases,you ll need to make some minor changes.Obviously, the control has to support the flags youchoose.Visual C++ users will see a problem almost instantly after they compile the control code andtry to use it within a Visual C++ application.What you ll get for a default presentation in theIDE is a blank white box.Fortunately, the control appears as normal when you compile andrun the application.The problem is that Visual C++ doesn t InPlaceActivate the control.As aresult, the OnPaint() method never gets called to paint the control on screen, and the ATLimplementation for OnDraw() doesn t do anything.Listing 6-15 shows one way to get aroundthis problem by implementing the OnDraw() method yourself. Listing 6-15HRESULT CPButton2Ctrl::OnDraw(ATL_DRAWINFO &di){// Control's bounding rectangle.RECT& rc = *(RECT*)di.prcBounds;// If the control isn't InPlaceActive, then we// need to draw it.Otherwise, the control gets// drawn as part of a call to OnPaint().if (!m_bInPlaceActive){// Tell Visual C++ that we'll be converting BSTR values.USES_CONVERSION;// Make the control display a simple rectangle with text.Rectangle(di.hdcDraw,rc.left,rc.top,rc.right,rc.bottom);DrawText(di.hdcDraw,OLE2T(m_bstrCaption),-1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);}return S_OK;}I chose to create a very simple presentation, in this case, because the drawing code isn tvery important.All the user really needs is a box to show the extent of the control and thecaption text.Creating an image that looks like a button would be nice, but not required.There s one absolute necessity.Notice that the code checks the InPlaceActivate status ofthe control before it draws anything.You must perform this step or the user will see a flawedbutton appearance when the application is running.NoteAt this point, you d add a property page to the control similar to the one forthe MFC version of the control.In addition, you d want the ModalResult andStdButtonType properties to use drop-down list boxes similar to those foundfor the MFC version of the control.You ll find both of these additions on thesource code CD.Testing the Control After you create a new ActiveX control, you have to test it to make sure it works asanticipated.I m going to use Visual C++ throughout the following sections.You could useany programming environment that supports ActiveX controls.For example, you might wantto test the OnOff control with Visual Basic to see how it works with another language.Theimportant consideration isn t the language you use for testing but that you test the controlfully with some programming language that includes full debugging support.You want tomake sure that the control you ve created actually works as anticipated in the safest possibleenvironment designed to do so.Since many Internet tools are in the testing stage, you mayfind that a control that seems like it should work doesn t.Being able to eliminate the controlitself by testing it in a known environment is one step toward troubleshooting the problem.Performing the ActiveX control test doesn t have to be a long, drawn-out affair [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • nvs.xlx.pl