Microsoft KB Archive/242002

= PRB: ATL Control Appears Incorrect in Access Report =

Article ID: 242002

Article Last Modified on 1/25/2007

-

APPLIES TO


 * Microsoft ActiveX Template Library 3.0
 * Microsoft Access 2002 Standard Edition
 * Microsoft Access 2000 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q242002



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.



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
 Close down Microsoft Access and go back to your ATL project.  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; }                    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.

