Resizing main menu for high DPI/font size

徘徊边缘 提交于 2019-12-13 00:34:48

问题


I have an issue with font height in standard main menu/popup menu when it contains images. Looks like this.

When there are no images, there are no problems as displayed above. Main menu uses TImageList with image width/height set to 16.

So I want to preserve image size at 16x16 and center it, to get something like this:

How can I read the font height of the main menu and adjust images in TImageList accordingly? One idea I have is to copy images from one TImageList to another with larger image width/height but I still need to determine proper size from the font size. How do I do that?

UPDATE

I solved this by examining SystemParametersInfo - SPI_GETNONCLIENTMETRICS value and using the iMenuHeight value for TImageList Width/Height. As images are deleted after changing Width/Height, I copied another to another TImageList. Works exactly as it should. Thank you everyone for your most helpful answers.

UPDATE 2

After examining the problem futher the solution which I marked as correct down there is giving better result so I switched to that one instead. Tested on Win7 and XP, appears to be working properly.


回答1:


You can get the height of Screen.MenuFont by selecting it to a temporary DC:

function GetMenuFontHeight: Integer;
var
  DC: HDC;
  SaveObj: HGDIOBJ;
  Size: TSize;
begin
  DC := GetDC(HWND_DESKTOP);
  try
    SaveObj := SelectObject(DC, Screen.MenuFont.Handle);
    GetTextExtentPoint32(DC, '|', 1, Size); // the character doesn't really matter
    Result := Size.cy;
    SelectObject(DC, SaveObj);
  finally
    ReleaseDC(HWND_DESKTOP, DC);
  end;
end;



回答2:


Well, Canvas.GetTextHeight('gh') usually helps to get height of text. But in case of different DPI, you can simply scale by Screen.PixelsPerInch / 96.0.




回答3:


The text height is probably not what you need to use. I suggest that you use icons whose square dimension is equal to the prevailing small icon size. That's the system metric whose ID is SM_CXSMICON. Retrieve the value by calling GetSystemMetrics passing that ID.




回答4:


You can use Power Menu Component with many advanced features Download from here : http://elvand.com/downloads/DELPHI/PowerMenu.zip

Delphi7-XE2 size=193 KB




回答5:


 #include <windows.h>

 int GetMainMenuHeight(void)
 {
   NONCLIENTMETRICS Rec;

   Rec.cbSize = sizeof(Rec);
   if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, Rec.cbSize, &Rec.cbSize, 0))
    return Rec.iMenuHeight;
   else return -1;
 }


来源:https://stackoverflow.com/questions/20825325/resizing-main-menu-for-high-dpi-font-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!