Re: Determining text position in owner drawn context menus

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi again Kellie,

Thanks for trying to help me out here. Actually I am quite aware of
the api functions for drawing icons and text, my problem is more
related to what to put into them.

This is what my WM_DRAWITEM code does at the moment. It is not the
complete code as I have skipped some parts like dealing with
highlighted menus, disabled menus (which have different colors),
separators and so on. I have also added some comments to describe
where the problem is. The newsreader client really messed up the
formatting of this code :-/



STDMETHODIMP
CDCMenuShellExt::OnDrawItem ( DRAWITEMSTRUCT* pdis, LRESULT* pResult )
{
CDCHandle dcBmpSrc;
CDCHandle pdcMenu;
DCMenu* menuPtr;
COLORREF menuColor;
COLORREF textColor;
SIZE textSize;
DWORD off_y,off_x,width,height;


pdcMenu.Attach( pdis->hDC );
if ( (menuPtr = m_DCSubMenu.FindMenu(pdis->itemID)) == NULL )
return S_OK;

/*Calculate text height*/
GetTextDimensions((char*)(menuPtr->GetMenuString()),&textSize);

CRect rectFull(pdis->rcItem);

/*Calculate icon rectangle dimensions*/
height = GetSystemMetrics(SM_CYMENUCHECK); // are these the
correct metrics to use for this kind of icon?
width = GetSystemMetrics(SM_CXMENUCHECK);

/*I have been playing around with various ways to align the
icons and text vertically. I don't remember if the code
below did the trick or if it was just for testing..*/
off_y = rectFull.top+ ((rectFull.bottom - rectFull.top + 1) -
height)/2;
off_x = rectFull.left;

CRect rectIcon( off_x, off_y, off_x+width, off_y+height);

/*Calculate text rectangle dimensions*/
height = textSize.cy;
width = textSize.cx;
off_y = rectFull.top + ((rectFull.bottom - rectFull.top + 1)
- height)/2;

/***********************************************************************************/
/* this is related to one of my problems. If I use the code below,
i.e. start drawing the text using a fixed offset
(DC_ICON_TEXT_DISTANCE is a define) from the icon it will not be
aligned properly on all systems. On my WinXP system for example the
text is aligned properly with the rest of the menus if I start
drawing on offset 17, while on another system I have tested it looks
ok using offset 21 from the right side of the rectangle I get from the
WM_DRAWITEM message.
I figured there must be some way of determining the correct offset
using GetSystemMetrics(), but all my experimentation and searching so
far has unfortunately not helped very much.

Here is an example what it might look like (not sure how this will
look in a newsgroup reader, but I hope for the best).

On system 1:

| WinRAR |
| MyMenu |
| WinZip |

On system 2:

| WinRAR |
| MyMenu |
| WinZip |

On system 3:

| WinRAR |
| MyMenu |
| WinZip |

The point was to give an example of how it looks when the text is not
aligned with other menus...

*/

off_x = rectIcon.right+DC_ICON_TEXT_DISTANCE;
/************************************************************************************************/

CRect rectText( off_x, off_y , off_x+width, off_y+height);

menuColor = GetSysColor( COLOR_MENU );
textColor = GetSysColor( COLOR_MENUTEXT );

/*Redraw the background*/
pdcMenu.FillSolidRect(&rectFull, menuColor);

/*if an icon is used*/
if (menuPtr->GetIconHandle())
{

// Create a new DC and select the original bitmap into
it.
dcBmpSrc.CreateCompatibleDC ( pdcMenu );
dcBmpSrc.SelectBitmap ( menuPtr->GetIconHandle() );

// Blit the bitmap to the menu DC.
pdcMenu.StretchBlt( rectIcon.left, rectIcon.top,
GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK),
dcBmpSrc, 0, 0, rectIcon.Width(),rectIcon.Height(), SRCCOPY
);

dcBmpSrc.DeleteDC();
}

pdcMenu.SetBkColor(menuColor);
pdcMenu.SetTextColor(textColor);

DrawText(
pdcMenu, // handle to DC
menuPtr->GetMenuString(), // text to draw
strlen(menuPtr->GetMenuString()), // text length
&rectText, // formatting
dimensions
DT_SINGLELINE|DT_VCENTER|DT_LEFT //
text-drawing options
);

pdcMenu.Detach();
*pResult = TRUE;

return S_OK;
}



and again... thanks Kellie for helping me out here :-)




On 9 Oct 2005 12:36:25 -0700, "Kellie Fitton" <KELLIEFITTON@xxxxxxxxx>
wrote:

>Hi Kilmar,
>
>Here are some pointers that should help you out:
>
> case WM_DRAWITEM:
>
> // select the appropriate text colors...
>
> GetSysColor()
> SetTextColor()
> SetBkColor()
> CopyRect()
> SHGetFileInfo()
>
>
> // Convert HICON to HBITMAP...
>
> GetSystemMetrics(SM_CXFRAME)
> GetIconInfo()
> GetObject()
> SetRect()
> ExtTextOut()
> CreateCompatibleDC()
> SelectObject()
> DrawIconEx()
>
>
> // draw text one line centered vertically...
>
> lstrlen()
> GetTextExtentPoint32()
> SetRect()
> ExtTextOut()
> SetBkMode() using TRANSPARENT
> OffsetRect()
> SetTextColor()
> lstrlen()
> DrawTextEx()
>
>
> // cleanUp...
>
> DeleteDC()
> DeleteObject()
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsyscolor.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_8bsi.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/pantdraw_433m.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_7zg4.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shgetfileinfo.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemmetrics.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/icons/iconreference/iconfunctions/geticoninfo.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_912s.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_8k6s.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_2ks4.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_499f.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_9v3o.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/icons/iconreference/iconfunctions/drawiconex.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_8smq.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/pantdraw_47hh.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_912s.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/strings/stringreference/stringfunctions/lstrlen.asp
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_4pbs.asp
>
>Hope these information helps,
>
>Kellie.
.



Relevant Pages

  • Re: OT: Win 7... coming unpinned
    ... that the menus look different, but I have no problem with that (for reasons ... Surely that's the same context. ... It adds the phrase "this program" and the icon when the icon you got the ...
    (comp.sys.mac.advocacy)
  • Re: Help to get out of draw mode!?1
    ... no pulldowns, no menus, nothing. ... the code I had attached to that icon. ... The code disables ALL toolbars and menus. ... > I only have the formula bar and the column and row numbers and letters. ...
    (microsoft.public.excel)
  • A quick explination of MMB and how I make my HD menus
    ... I am making some very good menus for playback of my ... NAS archived video. ... (This is where the command line switches go) ... Then for the Interactions Icon ...
    (rec.video.desktop)
  • Need to change icons on 400 + shortcuts
    ... We have menus with 400 + shortcuts on them that are on a network ... Not all folders are recognised as folders. ... my current working directoy only appears to return the parent ... Icon: C:\WINDOWS\explorer.exe,10 ...
    (comp.lang.perl.misc)
  • Re: [Bug Suspicion] So, <any_gdi_object>.Dispose() does ... what exactly?
    ... I did not see this when I wrote an article for drawing icons from ... > I have developed a very small Windows application which creates a new Icon ... > using .NET GDI+ every second ... I also dispose all GDI+ objects (Bitmap, ...
    (microsoft.public.dotnet.framework.drawing)