Solved

Display of ATL COM Object

Posted on 2001-09-05
9
484 Views
Last Modified: 2013-11-25
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.
0
Comment
Question by:cunningham
  • 5
  • 4
9 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6456847
use CComControl::GetAmbientUserMode to determine if you are in design mode (FALSE) or run mode (TRUE)
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6456864
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(bUserMode);

          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;
     }
0
 

Author Comment

by:cunningham
ID: 6456910
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.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6457061
use a bitmap - and bitblt to the screen DC - that is what the IE control does at design time

               HBITMAP hBitmap=::LoadBitmap(_Module.GetResourceInstance(),MAKEINTRESOURCE(IDB_MYDRAW));
               HDC hDC=::CreateCompatibleDC(di.hdcDraw);
               HBITMAP hOldBitmap=(HBITMAP)::SelectObject(hDC,hBitmap);
               ::StretchBlt(di.hdcDraw,0,0,rc.right,rc.bottom,hDC,0,0,16,16,SRCCOPY);
               ::SelectObject(hDC,hOldBitmap);
               ::DeleteDC(hDC);
               ::DeleteObject(hBitmap);
0
 

Author Comment

by:cunningham
ID: 6457442
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.
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 250 total points
ID: 6457665
you will have to support the property DISPID_AUTOSIZE

here goes

add this to your .idl

          [propput, id(DISPID_AUTOSIZE)]
          HRESULT AutoSize([in]VARIANT_BOOL vbool);
          [propget, id(DISPID_AUTOSIZE)]
          HRESULT AutoSize([out,retval]VARIANT_BOOL* pbool);

in you .h

change the IDisplatchImpl line to be something like this (change the classes,intefaces, libraries etc)

     public CStockPropImpl<CMyDraw, IMyDraw, &IID_IMyDraw, &LIBID_ATLDRAWLib>,

add this to your prop map

     PROP_ENTRY("AutoSize", DISPID_AUTOSIZE, CLSID_NULL)


and now for the fun stuff - contorl are in HIMETRICS but we mortals like pixels so...

     CMyDraw()
     {
          m_bAutoSize=true; // this says we will control the size ourselves
          SIZE sz;
          sz.cx=32; // we want 32x32
          sz.cy=32;
          AtlPixelToHiMetric(&sz,&m_sizeExtent); // convert pixels to himetrics
     }

I hope this all makes sense
0
 

Author Comment

by:cunningham
ID: 6458294
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.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6458309
glad to help :)
0
 

Author Comment

by:cunningham
ID: 6458325
Also, if you take the

         [propput, id(DISPID_AUTOSIZE)]
         HRESULT AutoSize([in]VARIANT_BOOL vbool);
         [propget, id(DISPID_AUTOSIZE)]
         HRESULT AutoSize([out,retval]VARIANT_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).
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
wait notify demo infinite loop 3 124
sumHeights  challenge 17 75
sumHeights2  challenge 7 117
How do I get Window Title of all opened process? 4 14
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question