cunningham
asked on
Display of ATL COM Object
I created an ATL COM object that is visible during design time but invisible during runtime. During design time, I print out some text that names the control in the OnDraw function. What I would like to do is to display a square box that looks like a button without the user being able to change the size of the object. Maybe even put a picture representing my control in the button. This is how other objects that are normally not displayed at runtime are shown during design time (e.g. the VB timer control). I would like to do something analogous to the VB timer control.
My Windows drawing skills are very weak. I would appreciate any help I can get in this area.
My Windows drawing skills are very weak. I would appreciate any help I can get in this area.
use CComControl::GetAmbientUse rMode to determine if you are in design mode (FALSE) or run mode (TRUE)
you can then use the above to in you OnDraw handler to either draw a piccie (bitmap) or whatever you display at runtime
eg
HRESULT OnDraw(ATL_DRAWINFO& di)
{
RECT& rc = *(RECT*)di.prcBounds;
Rectangle(di.hdcDraw, rc.left, rc.top, rc.right, rc.bottom);
SetTextAlign(di.hdcDraw, TA_CENTER|TA_BASELINE);
LPCTSTR pszText = _T("ATL 3.0 : MyDraw");
LPCTSTR pszTextDesign = _T("ATL 3.0 : MyDraw Design");
BOOL bUserMode;
GetAmbientUserMode(bUserMo de);
if (bUserMode)
{
TextOut(di.hdcDraw,
(rc.left + rc.right) / 2,
(rc.top + rc.bottom) / 2,
pszText,
lstrlen(pszText));
}
else
{
TextOut(di.hdcDraw,
(rc.left + rc.right) / 2,
(rc.top + rc.bottom) / 2,
pszTextDesign,
lstrlen(pszTextDesign));
}
return S_OK;
}
eg
HRESULT OnDraw(ATL_DRAWINFO& di)
{
RECT& rc = *(RECT*)di.prcBounds;
Rectangle(di.hdcDraw, rc.left, rc.top, rc.right, rc.bottom);
SetTextAlign(di.hdcDraw, TA_CENTER|TA_BASELINE);
LPCTSTR pszText = _T("ATL 3.0 : MyDraw");
LPCTSTR pszTextDesign = _T("ATL 3.0 : MyDraw Design");
BOOL bUserMode;
GetAmbientUserMode(bUserMo
if (bUserMode)
{
TextOut(di.hdcDraw,
(rc.left + rc.right) / 2,
(rc.top + rc.bottom) / 2,
pszText,
lstrlen(pszText));
}
else
{
TextOut(di.hdcDraw,
(rc.left + rc.right) / 2,
(rc.top + rc.bottom) / 2,
pszTextDesign,
lstrlen(pszTextDesign));
}
return S_OK;
}
ASKER
Thanks for your quick response. However, my question is really "How do you draw a button control similar to the VB timer control"? I'm not sure that the VB timer control is based on the button control, but it looks like it. I am able to do the TextOut stuff as shown in your example but would like something a little nicer.
use a bitmap - and bitblt to the screen DC - that is what the IE control does at design time
HBITMAP hBitmap=::LoadBitmap(_Modu le.GetReso urceInstan ce(),MAKEI NTRESOURCE (IDB_MYDRA W));
HDC hDC=::CreateCompatibleDC(d i.hdcDraw) ;
HBITMAP hOldBitmap=(HBITMAP)::Sele ctObject(h DC,hBitmap );
::StretchBlt(di.hdcDraw,0, 0,rc.right ,rc.bottom ,hDC,0,0,1 6,16,SRCCO PY);
::SelectObject(hDC,hOldBit map);
::DeleteDC(hDC);
::DeleteObject(hBitmap);
HBITMAP hBitmap=::LoadBitmap(_Modu
HDC hDC=::CreateCompatibleDC(d
HBITMAP hOldBitmap=(HBITMAP)::Sele
::StretchBlt(di.hdcDraw,0,
::SelectObject(hDC,hOldBit
::DeleteDC(hDC);
::DeleteObject(hBitmap);
ASKER
Thanks for the information. Like I said, my Windows drawing skills are very weak. Do you know how to set the size of the control so that the user can not change it? The timer control in Visual Basic always snaps back to the same size even if the user tries to change it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's exactly what I was looking for. I'll remember to add the AutoSize stock property the next time I build a control when I want to control the size. Thanks for all of your help.
glad to help :)
ASKER
Also, if you take the
[propput, id(DISPID_AUTOSIZE)]
HRESULT AutoSize([in]VARIANT_BOOL vbool);
[propget, id(DISPID_AUTOSIZE)]
HRESULT AutoSize([out,retval]VARIA NT_BOOL* pbool);
lines out of the IDL file, then the AutoSize property is not exported and then there is no way for the user to change the size of the object (just like the timer control).
[propput, id(DISPID_AUTOSIZE)]
HRESULT AutoSize([in]VARIANT_BOOL vbool);
[propget, id(DISPID_AUTOSIZE)]
HRESULT AutoSize([out,retval]VARIA
lines out of the IDL file, then the AutoSize property is not exported and then there is no way for the user to change the size of the object (just like the timer control).