我试图在系统任务栏中显示2-3个可更新字符,而不是显示.ico文件-类似于CoreTemp在系统中显示温度时所做的尝试:
I am trying to display 2-3 updatable characters in the system tray rather than display an .ico file - similar to what CoreTemp does when they display the temperature in the system try:
我正在WinForms应用程序中使用NotifyIcon以及以下代码:
I am using a NotifyIcon in my WinForms application along with the following code:
Font fontToUse = new Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Pixel); Brush brushToUse = new SolidBrush(Color.White); Bitmap bitmapText = new Bitmap(16, 16); Graphics g = Drawing.Graphics.FromImage(bitmapText); IntPtr hIcon; public void CreateTextIcon(string str) { g.Clear(Color.Transparent); g.DrawString(str, fontToUse, brushToUse, -2, 5); hIcon = (bitmapText.GetHicon); NotifyIcon1.Icon = Drawing.Icon.FromHandle(hIcon); DestroyIcon(hIcon.ToInt32); }可悲的是,这产生的效果很差,与CoreTemp所获得的完全一样:
Sadly this produces a poor result nothing like what CoreTemp gets:
您会认为解决方案是增加字体大小,但是任何大于8号的内容都不适合图像.将位图从16x16增加到32x32都无济于事-它会缩小尺寸.
You'd think the solution would be to increase the font size, but anything over size 8 doesn't fit inside the image. Increasing the bitmap from 16x16 to 32x32 does nothing either - it gets resized down.
然后就是我想要显示"8.55"而不是"55"的问题-图标周围有足够的空间,但是它似乎无法使用.
Then there's the problem of me wanting to display "8.55" instead of just "55" - there's enough space around the icon but it appears unusable.
是否有更好的方法可以做到这一点?Windows为什么可以执行以下操作,但我不能执行以下操作?
Is there a better way to do this? Why can windows do the following but I cannot?
更新:
感谢@NineBerry提供了一个好的解决方案.作为补充,我发现 Tahoma 是最好的字体.
Thanks for @NineBerry for a good solution. To add, I find Tahoma to be the best font to use.
推荐答案这使我可以很好地显示两位数的字符串:
This gives me a quite good looking display of a two digit string:
private void button1_Click(object sender, EventArgs e) { CreateTextIcon("89"); } public void CreateTextIcon(string str) { Font fontToUse = new Font("Microsoft Sans Serif", 16, FontStyle.Regular, GraphicsUnit.Pixel); Brush brushToUse = new SolidBrush(Color.White); Bitmap bitmapText = new Bitmap(16, 16); Graphics g = System.Drawing.Graphics.FromImage(bitmapText); IntPtr hIcon; g.Clear(Color.Transparent); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; g.DrawString(str, fontToUse, brushToUse, -4, -2); hIcon = (bitmapText.GetHicon()); notifyIcon1.Icon = System.Drawing.Icon.FromHandle(hIcon); //DestroyIcon(hIcon.ToInt32); }
我更改了:
使用较大的字体,但将x和y偏移量进一步移至左侧和顶部(-4,-2).
Use a larger font size, but move the x and y offset further to the left and top (-4, -2).
在Graphics对象上设置TextRenderingHint以禁用抗锯齿.
Set TextRenderingHint on the Graphics object to disable anti-aliasing.
绘制超过2位数字或字符似乎是不可能的.图标具有正方形格式.长度超过两个字符的任何文本都将意味着大大降低文本的高度.
It seems impossible to draw more than 2 digits or characters. The icons have a square format. Any text longer than two characters would mean reducing the height of the text a lot.
选择键盘布局(ENG)的示例实际上不是托盘区域中的通知图标,而是它自己的外壳工具栏.
The sample where you select the keyboard layout (ENG) is actually not a notification icon in the tray area but its very own shell toolbar.
显示8.55是我能达到的最好成绩:
The best I could achieve to display 8.55:
private void button1_Click(object sender, EventArgs e) { CreateTextIcon("8'55"); } public void CreateTextIcon(string str) { Font fontToUse = new Font("Trebuchet MS", 10, FontStyle.Regular, GraphicsUnit.Pixel); Brush brushToUse = new SolidBrush(Color.White); Bitmap bitmapText = new Bitmap(16, 16); Graphics g = System.Drawing.Graphics.FromImage(bitmapText); IntPtr hIcon; g.Clear(Color.Transparent); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; g.DrawString(str, fontToUse, brushToUse, -2, 0); hIcon = (bitmapText.GetHicon()); notifyIcon1.Icon = System.Drawing.Icon.FromHandle(hIcon); //DestroyIcon(hIcon.ToInt32); }
进行以下更改:
更多推荐
将文字而不是图标写入系统任务栏
发布评论