Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文本而不是图标写入系统托盘_C#_.net_System Tray_Notifyicon - Fatal编程技术网

C# 将文本而不是图标写入系统托盘

C# 将文本而不是图标写入系统托盘,c#,.net,system-tray,notifyicon,C#,.net,System Tray,Notifyicon,我尝试在系统托盘中显示2-3个可更新字符,而不是显示.ico文件-类似于CoreTemp在系统中显示温度时所做的尝试: 我正在WinForms应用程序中使用Notify图标以及以下代码: Font fontToUse = new Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Pixel); Brush brushToUse = new SolidBrush(Color.White); Bitmap bitmapT

我尝试在系统托盘中显示2-3个可更新字符,而不是显示.ico文件-类似于CoreTemp在系统中显示温度时所做的尝试:

我正在WinForms应用程序中使用Notify图标以及以下代码:

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得到的结果完全不同:

您可能会认为解决方案是增加字体大小,但任何超过8的大小都不适合图像。将位图从16x16增加到32x32也不起任何作用-它的大小会减小

还有一个问题是,我想显示“8.55”而不仅仅是“55”-图标周围有足够的空间,但它似乎无法使用

有更好的方法吗?为什么windows可以执行以下操作,但我不能

更新:


感谢@NineBerry提供了一个很好的解决方案。另外,我发现
Tahoma
是最适合使用的字体。

这给了我一个非常好看的两位数字符串显示:

我改变的是:

  • 使用较大的字体大小,但将x和y偏移量进一步向左和向上移动(-4,-2)

  • 在图形对象上设置TextRenderingHint以禁用抗锯齿

  • 似乎不可能画出超过2个数字或字符。图标采用方形格式。任何超过两个字符的文本都意味着文本的高度会大大降低

    选择键盘布局(ENG)的示例实际上不是托盘区域中的通知图标,而是它自己的shell工具栏


    我所能达到的最佳效果是显示8.55:

    作出以下更改:

  • 使用Trebuchet MS,这是一种非常窄的字体
  • 使用单引号代替圆点,因为它在侧面的空间较小
  • 使用字体大小10并适当调整偏移量
    我希望其他应用程序只使用一组内置图标,而不是尝试在FlyAlway上生成它们,只是说我发现“Tahoma”是这里使用的最佳字体。你需要一行:DestroyIcon(hIcon),以防止应用程序在大约50分钟后由于内存泄漏而退出。
    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);
    }
    
    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);
    }