PRB: ATL Control Appears Incorrect in Access Report

ID: Q242002


The information in this article applies to:
  • The Microsoft Active Template Library (ATL) 3.0
  • Microsoft Access versions 2000, 97


SYMPTOMS

An ATL ActiveX Control does not appear or appears incorrectly when added to a Microsoft Access Report. The same control displays correctly when the Report is in design mode or if the control is added to an Access Form.


CAUSE

Microsoft Access uses different drawing methods for rendering a control on a Report and rendering it on a Form. When placed on a Report, the control is asked for a metafile presentation that Access uses to render the control when previewing or printing the report. The control itself (that is, the control window) is only displayed when in design mode. The problem is that the metafile being rendered by the ATL control is incorrect.

When Access requests that the control draw itself (through IViewObject::Draw), ATL uses the GetDeviceCaps API and the TECHNOLOGY index to determine whether the HDC passed as a parameter to the function is a metafile device context. Under certain circumstances, this call does not succeed in detecting that a metafile is indeed being used, so ATL treats the bounding RECT coordinates as screen coordinates instead of HIMETRIC units.


RESOLUTION

To resolve the problem, change the code that checks the device context so that it also checks if the bounding window RECT passed in from IViewObject::Draw is NULL. If the parameter is NULL, a metafile is not being drawn. See the sample below to demonstrate the workaround.


MORE INFORMATION

Steps to Reproduce Behavior

  1. Start Visual C++ and create a new ATL COM AppWizard project. Name the project AccessTest. Accept the default settings for the wizard.


  2. Using Insert|New ATL Object, add a Full Control object to the project. Name the control AccessTestCtrl, and on the Miscellaneous tab, check the Windowed Only check box. From the Stock Properties tab, select Caption and Enabled. Click OK.


  3. Press the F7 key to build and register the control.


  4. Open Microsoft Access (create a new database if necessary), and add a new Report.


  5. With the report in design mode, select More Controls from the Toolbox toolbar and find the AccessTestCtrl item. Add an instance of the ATL control to the report.


  6. Save the report and close.


  7. Now re-open the Report in preview mode and note that the control does not appear or appears incorrectly.


Building a Workaround

  1. Close down Microsoft Access and go back to your ATL project.


  2. The check for the metafile is in OnDrawAdvanced. To fix the problem, override this function by adding the following to the control class (in AccessTestCtrl.h) just before the OnDraw function:


  3. 
    HRESULT OnDrawAdvanced(ATL_DRAWINFO& di)
    {
       BOOL bDeleteDC = FALSE;
       if (di.hicTargetDev == NULL)
       {
          di.hicTargetDev = AtlCreateTargetDC(di.hdcDraw, di.ptd);
          bDeleteDC = (di.hicTargetDev != di.hdcDraw);
       }
       RECTL rectBoundsDP = *di.prcBounds;
    
    // Check for a metafile...
       BOOL bMetafile = GetDeviceCaps(di.hdcDraw, TECHNOLOGY) == DT_METAFILE;
    
    // Changed "if" clause to check both the metafile flag and
    // bounding window RECT passed to IViewObject::Draw...
       if ((!bMetafile) && (di.prcWBounds == NULL))
       {
          ::LPtoDP(di.hicTargetDev, (LPPOINT)&rectBoundsDP, 2);
          SaveDC(di.hdcDraw);
          SetMapMode(di.hdcDraw, MM_TEXT);
          SetWindowOrgEx(di.hdcDraw, 0, 0, NULL);
          SetViewportOrgEx(di.hdcDraw, 0, 0, NULL);
          di.bOptimize = TRUE; //since you save the DC you can do this
       }
    
       di.prcBounds = &rectBoundsDP;
       GetZoomInfo(di);
    
       HRESULT hRes = OnDraw(di);
       if (bDeleteDC)
          ::DeleteDC(di.hicTargetDev);
       if ((!bMetafile) && (di.prcWBounds == NULL))
          RestoreDC(di.hdcDraw, -1);
       return hRes;
    } 
  4. Recompile the control by pressing the F7 key. Reopen Access, preview the Report made earlier, and note that the control now appears. For example, you should be able to read the caption ("ATL 3.0 : AccessTestCtrl") that you were unable to read before.


  5. NOTE: Access might crop the border of the metafile when displaying it.

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Richard R. Taylor, Microsoft Corporation


REFERENCES

For additional information regarding ATL controls in Access, click the article number below to view the article in the Microsoft Knowledge Base:

Q197490 PRB: ATL Full Control Needs Enabled Stock Property for Access 97

Additional query words:

Keywords : kbAccess kbActiveX kbCtrlCreate kbVC600 kbATL300 kbGrpDSO kbDSupport
Version : WINDOWS:2000,3.0,97
Platform : WINDOWS
Issue type : kbprb


Last Reviewed: October 1, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.