Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

PRB: ATL Control Appears Incorrect in Access Report


View products that this article applies to.

This article was previously published under Q242002

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


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:
    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;
    }
    					
  3. 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.NOTE: Access might crop the border of the metafile when displaying it.

↑ Back to the top


References

For additional information regarding ATL controls in Access, click the article number below to view the article in the Microsoft Knowledge Base:
197490� PRB: ATL Full Control Needs Enabled Stock Property for Access 97

↑ Back to the top


Keywords: KB242002, kbprb, kbctrlcreate

↑ Back to the top

Article Info
Article ID : 242002
Revision : 9
Created on : 1/25/2007
Published on : 1/25/2007
Exists online : False
Views : 386