简体   繁体   English

如何确定根据Icon的大小使用哪种字体?

[英]How to figure out which font size to use depending on size of Icon?

I'm trying to create a taskbar Icon in C# that would display a number representing the current processor time of an application. 我正在尝试在C#中创建一个任务栏图标,该图标将显示代表应用程序当前处理器时间的数字。 I can figure out the proper size of the icon by simply referencing the default taskbar icons dimesions, however, I run into a problem. 我可以通过简单地引用默认任务栏图标尺寸来找出图标的正确大小,但是,我遇到了问题。 Different Icon sizes work well with different font sizes of text. 不同的图标大小可以与文本的不同字体大小一起很好地工作。 I need to figure out how to select a font size that would be as legible and clear as possible given the size of the Icon to be created. 我需要弄清楚如何根据给定要创建的图标的大小来选择尽可能清晰易读的字体大小。

In this case, font size 18 works well as the bitmap resolution of the Icon I'm creating is 32x32 (as my monitor has a 1680 x 1050 resolution). 在这种情况下,字体大小为18的效果很好,因为我正在创建的Icon的位图分辨率为32x32(因为我的显示器的分辨率为1680 x 1050)。 However, suppose the Icon to be made has a resolution of 16x16 (or something smaller than 32 x 32) - then a font size of 18 would be too large for the icon itself. 但是,假设要制作的图标的分辨率为16x16(或小于32 x 32的分辨率)-那么对于图标本身而言,字体大小为18太大了。 Is there a method call I can make, or something along those lines that would tell windows to figure out the best font-size to use depending on resolution of the icon it is to be displayed on? 有没有可以调用的方法,或者可以让窗口根据要显示的图标的分辨率确定要使用的最佳字体大小的方法? I'm sorry if this is a confusing post, I would be happy to clarify anything. 抱歉,如果这是一个令人困惑的帖子,我很乐意澄清。

 using (var image = new Bitmap(Icon.Width, Icon.Height)) 
        using (var g = Graphics.FromImage(image))
        {
            g.DrawString(cpuReading.ToString(), new Font("Times New Roman", 18), new SolidBrush(Color.LightGreen), new PointF());
            this.tskBarIcon.Icon = Icon.FromHandle(image.GetHicon());
        }

There is no direct method to do what you want. 没有直接的方法可以做您想要的。

But what you can do is use Graphics.MeasureString to measure the size of a rendered string. 但是您可以做的是使用Graphics.MeasureString来测量渲染字符串的大小。 Start measuring with a large font size, and then try progressively smaller sizes until you get to one that will fit your icon dimensions. 以大字体开始测量,然后尝试逐渐减小字体大小,直到达到适合图标尺寸的大小为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找出窗口标题的字体大小? - How to find out the font size of window caption? 如何使用组合框更改richTextBox字体大小 - How to use combobox to change the richTextBox font size 如何使用Silverlight 2找出已加载图像的大小? - How do I figure out the size of a loaded image using Silverlight 2? 如何使用 Xamarin 在 xaml 中创建自定义字体大小,以便您可以根据设备更改字体大小? - How to create a custom font size in xaml using Xamarin, so that you can change font sizes depending on device? 无法在十进制参数中找出“大小无效” - Could not figure out "size is invalid" on decimal argument 如何在DateTimePicker中设置字体大小 - How to set font size in DateTimePicker 如何将FreshTabbedNavigationContainer选项卡式页面更改为顶部并更改Xamarin形式的ios的图标和字体大小 - How to change FreshTabbedNavigationContainer tabbed page to top and change icon and font size for ios in xamarin forms 如何在XAML中增加字体大小? - How to increase font size in XAML? 如何提取内容的字体大小 - How to extract font size of content 使用nhibernate时,如何在映射代码中找出最佳批量大小? - When using nhibernate, how do I figure out optimal batch size in the mapping code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM