2016-12-24 1 views
5

ToolTipメッセージをTextBoxの下に表示したいだけでなく、右揃えにしたいと考えています。Cのコントロールとツールヒントメッセージの右端を揃える方法

テキストボックスの右端にツールヒントメッセージを配置できたので、メッセージ長だけメッセージを移動しようとしました。

TextRenderer.MeasureText()を使用して文字列の長さを取得しようとしましたが、次のように位置が少しずれています。

current result

private void button1_Click(object sender, EventArgs e) 
{ 
    ToolTip myToolTip = new ToolTip(); 

    string test = "This is a test string."; 
    int textWidth = TextRenderer.MeasureText(test, SystemFonts.DefaultFont, textBox1.Size, TextFormatFlags.LeftAndRightPadding).Width; 
    int toolTipTextPosition_X = textBox1.Size.Width - textWidth; 

    myToolTip.Show(test, textBox1, toolTipTextPosition_X, textBox1.Size.Height); 
} 

私はのmeasureText()機能が異なるフラグをしようとしたが、それは助ける、およびツールヒントメッセージので、パディングを持っていなかった、私はTextFormatFlags.LeftAndRightPaddingのために行ってきました。

明確にするために、これは私が達成したいものです。

desired output

+0

SystemFonts.DefaultFontをmyToolTip.Fontで置き換えようとします。 – Graffito

+0

@Graffito:そのようなものはありません。 ToolTipのオーナー描画時に使用するFontを決めることができます。 – TaW

+0

申し訳ありませんが、フォントはToolTip.Drawイベント(OwnerDraw = true)でのみ使用できます。 – Graffito

答えて

4

あなたがtrueにToolTipOwnerDrawプロパティを設定することができます。測定値が不正確であるので

[System.Runtime.InteropServices.DllImport("User32.dll")] 
static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw); 
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e) 
{ 
    e.DrawBackground(); 
    e.DrawBorder(); 
    e.DrawText(); 
    var t = (ToolTip)sender; 
    var h = t.GetType().GetProperty("Handle", 
     System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); 
    var handle = (IntPtr)h.GetValue(t); 
    var c = e.AssociatedControl; 
    var location = c.Parent.PointToScreen(new Point(c.Right - e.Bounds.Width, c.Bottom)); 
    MoveWindow(handle, location.X, location.Y, e.Bounds.Width, e.Bounds.Height, false); 
} 

enter image description here

1

ツールヒントフォントがSystemFonts.DefaultFontよりも大きい:次に、あなたは、このようDrawイベントでツールチップの外観を制御することができます。私はToolTipフォントの正確な変数が分かりませんが、他のSystemFontsの多くは、私のPCのTooltipフォントであるSegoe UI/size 9に設定されています。さらに、パディングに6pxを追加する必要があります。あなたは、フォント、パディングと外観を選択、Tooltip.OwnerDrawやイベントTooltip.Drawでツールチップを自分で描くことができ、完全な制御のために

private void button1_Click(object sender, EventArgs e) 
{ 
    ToolTip myToolTip = new ToolTip(); 

    string test = "This is a test string."; 
    int textWidth = TextRenderer.MeasureText(test, SystemFonts.CaptionFont, textBox1.Size, TextFormatFlags.LeftAndRightPadding).Width; 
    textWidth += 6; 
    int toolTipTextPosition_X = textBox1.Size.Width - textWidth; 

    myToolTip.Show(test, textBox1, toolTipTextPosition_X, textBox1.Size.Height); 
} 

+0

ありがとう、フォント/サイズを変更してもらえました。 –

関連する問題