Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 242002 - Last Review: January 25, 2007 - Revision: 5.2
PRB: ATL Control Appears Incorrect in Access Report
This article was previously published under Q242002
On This Page
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
- Start Visual C++ and create a new ATL COM AppWizard project. Name the project AccessTest. Accept the default settings for the wizard.
- 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.
- Press the F7 key to build and register the control.
- Open Microsoft Access (create a new database if necessary), and add a new Report.
- 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.
- Save the report and close.
- 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.
REFERENCES
For additional information regarding ATL controls in Access, click the article number below
to view the article in the Microsoft Knowledge Base:
197490Â
(http://kbalertz.com/Feedback.aspx?kbNumber=197490/EN-US/
)
PRB: ATL Full Control Needs Enabled Stock Property for Access 97
APPLIES TO
- Microsoft ActiveX Template Library 3.0
- Microsoft Access 2002 Standard Edition
- Microsoft Access 2000 Standard Edition
- Microsoft Access 97 Standard Edition
| kbctrlcreate kbprb KB242002 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please