KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q146010: HOWTO: Setting the Picture Property of an ActiveX Control

Article: Q146010
Product(s): Microsoft C Compiler
Version(s): 4.0,5.0,6.0
Operating System(s): 
Keyword(s): kbcode kbActiveX kbBitmap kbCOMt kbCtrl kbMFC kbVC400 kbVC500 kbVC600 kbGrpDSMFCATL
Last Modified: 29-APR-2002

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual C++, version 4.0 
- Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0 
- Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0 
- Microsoft Visual C++, 32-bit Learning Edition, version 6.0 
-------------------------------------------------------------------------------

SUMMARY
=======

To set the Picture property of an ActiveX control, you can use the Component
Gallery to insert an ActiveX control into an AppWizard-generated application
with control container support. If the control has a picture property, the
wrapper classes generated by the Component Gallery will contain the SetPicture()
and GetPicture() methods. The More Information section below contains details
that explain how to use the picture property of an ActiveX control.

MORE INFORMATION
================

Using the Grid Control (Grid32.ocx is shipped with Visual C++ 4.x) as an
example, you can implement the SetPicture() and GetPicture() methods in the
control classes provided by the Component Gallery by using this code:

     void CGridCtrl::SetPicture(LPDISPATCH propVal)
     {
          SetProperty(0x15, VT_DISPATCH, propVal);
     }

     CPicture CGridCtrl::GetPicture()
     {
          LPDISPATCH pDispatch;
          GetProperty(0x15, VT_DISPATCH, (void*)&pDispatch);
          return CPicture(pDispatch);
     }

It is not intuitively clear how to use these methods to get or set the picture
property. The following steps show how to set the picture property of an ActiveX
control successfully in an AppWizard-generated application. The particular
control used in this example is the Microsoft Grid Control, and its picture
property is being set to the toolbar bitmap provided by AppWizard.

1. Generate a new non-dialog based AppWizard application. Be sure to select both
  Container and Control support in AppWizard Step 3.

2. Add the Grid Control from the OLE Controls tab (or Registered ActiveX
  Controls in VC++ 5.0) in the Component Gallery.

3. Add Afxctl.h to the list of pre-compiled header files in Stdafx.h.

4. Add a Grid Control object to the About Dialog box in the dialog editor.

5. Using ClassWizard, add a control member variable called m_grid to the Grid
  Control in the About Dialog box.

6. Override the container's initialization routine to create and initialize a
  CPictureHolder object. Call the SetPicture() method for the control, passing
  in the CPictureHolder's dispatch pointer. SetPicture calls SetProperty() for
  the control. Here's an example:

        BOOL CAboutDlg::OnInitDialog()
        {
            CDialog::OnInitDialog();

            // Create and Initialize the CPictureHolder variable with the
            // toolbar resource
            CPictureHolder pictholder;
            pictholder.CreateFromBitmap(IDR_MAINFRAME);

            //Pass dispatch pointer to CPictureHolder
            m_grid.SetPicture(pictholder.GetPictureDispatch());

            // NOTE: GetPictureDispatch() QI()'s for the IPictureDisp
            // interface, so the picture object will remain alive until
            // the property is reset or the control is destroyed. When
            // pictholder goes out of scope, it calls Release() on the
            // picture object, but the previously mentioned QI() will have
            // bumped the ref count, allowing it to remain alive until the
            // control itself releases the picture object.

          return TRUE;
        }

REFERENCES
==========

For Visual C++ 4.xx: OLE Control Containers: Programming OLE Controls in an OLE
Control Container - Visual C++ Books Online, MFC Encyclopedia.

OLE Controls: Using Pictures in an OLE Control - Visual C++ Books Online, MFC
Encyclopedia.

For Visual C++ 5.0: ActiveX Control Containers: Programming ActiveX Controls in
an ActiveX Control Container - Visual C++ Books Online, Visual C++ Programmer's
Guide.

ActiveX Controls: Using Pictures in an ActiveX Control - Visual C++ Books Online,
Visual C++ Programmer's Guide.

Additional query words: ocx

======================================================================
Keywords          : kbcode kbActiveX kbBitmap kbCOMt kbCtrl kbMFC kbVC400 kbVC500 kbVC600 kbGrpDSMFCATL 
Technology        : kbVCsearch kbVC400 kbAudDeveloper kbVC500 kbVC600 kbVC32bitSearch kbVC500Search
Version           : :4.0,5.0,6.0
Issue type        : kbhowto

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.