2016-04-02 17 views
7

システムトレイに温度を表示するときにCoreTempと同じように、.icoファイルを表示するのではなく、システムトレイに2〜3の更新可能な文字を表示しようとしています:アイコンの代わりにシステムトレーにテキストを書き込む

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が取得する何のような貧弱な結果は何も発生しない:私は次のコードと一緒に私のWinFormsアプリケーションにNotifyIconを使用しています

enter image description here

enter image description here

あなたはソリューションは、フォントサイズを大きくすることだと思うだろうが、サイズ8を超えるものは、画像内に収まりません。ビットマップを16x16から32x32に増やすことは何もしません。

次に、「55」ではなく「8.55」を表示したいという問題があります。アイコンの周りに十分なスペースがありますが、使用できないように見えます。

enter image description here

これを行うには良い方法はありますか?なぜウィンドウズは次のことをすることができますか?できません。

enter image description here

更新:良い解決策のための@NineBerryため

感謝。追加するには、Tahomaが使用するのに最適なフォントであることがわかります。

+1

ここで使用するのに最適なフォントです。 –

答えて

9

これは私に2桁の文字列のかなり格好良い表示を与える:

enter image description here

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); 
} 

私は何が変わった:

  1. は、より大きなフォントサイズを使用しますが、移動xとyはさらに左と上にオフセットしています(-4、-2)。

  2. アンチエイリアスを無効にするには、GraphicsオブジェクトでTextRenderingHintを設定します。

2桁以上の文字を描画することは不可能です。アイコンは正方形の形式です。 2文字以上のテキストは、テキストの高さを大幅に減らすことを意味します。

キーボードレイアウト(ENG)を選択したサンプルは、トレイ領域の通知アイコンではなく、独自のシェルツールバーです。


私は8を表示するのに最適です。55:非常に狭いフォントである

  1. 使用トレビュシェットMS:以下の変更を

    enter image description here

    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); 
    } 
    

  2. ドットの代わりに一重引用符を使用してください。
  3. フォントサイズ10を使用し、オフセットを適切に調整します。
私は、他のアプリケーションはただ単に私が「のTahoma」を見つけると言うことは、また、おかげで非常に多くの
+0

大きな反響をオンザフライでそれらを生成しようとすることを期待する – MSOACC

+1

を内蔵アイコンのセットを使用するのではなく – MSOACC

関連する問題