2016-12-24 18 views
5

में नियंत्रण और टूलटिप संदेश के दाएं किनारों को संरेखित करने के लिए मैं TextBox से नीचे ToolTip संदेश प्रदर्शित करना चाहता हूं, लेकिन यह भी चाहते हैं कि वे सही गठबंधन हों।सी #

मैं टेक्स्टबॉक्स के दाएं किनारे पर टूलटिप संदेश को स्थान देने में सक्षम था, इसलिए मैंने संदेश की लंबाई से संदेश को स्थानांतरित करने का प्रयास किया।

तो मैंने 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() समारोह में विभिन्न झंडे के साथ करने की कोशिश की, लेकिन यह मदद नहीं की, और के बाद से ToolTip संदेश एक गद्दी है, मैं TextFormatFlags.LeftAndRightPadding के लिए चला गया।

स्पष्ट है, यह मैं प्राप्त करने के लिए चाहते हैं क्या है:

desired output

+0

MyToolTip.Font द्वारा SystemFonts.DefaultFont को प्रतिस्थापित करने का प्रयास करें। – Graffito

+0

@ ग्रेफिटो: ऐसी कोई बात नहीं है। टूलटिप को मालिक-चित्रकारी करते समय कोई फॉन्ट का उपयोग करने का निर्णय ले सकता है। – TaW

+0

क्षमा करें, फ़ॉन्ट केवल ToolTip.Draw ईवेंट (OwnerDraw = true) में उपलब्ध है। – Graffito

उत्तर

4

आप सत्य पर ToolTip की OwnerDraw गुण सेट कर सकते हैं। तो फिर तुम इस तरह से Draw घटना में टूलटिप के स्वरूप को नियंत्रित कर सकते हैं:

[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

ToolTip फ़ॉन्ट SystemFonts.DefaultFont से भी बड़ा है, इसलिए माप सही नहीं है। मुझे नहीं पता कि टूलटिप फ़ॉन्ट के लिए सटीक चर क्या है, लेकिन कई अन्य सिस्टमफोंट सेगो यूआई/आकार 9 में कॉन्फ़िगर किए गए हैं, जो मेरे पीसी में टूलटिप फ़ॉन्ट है। इसके अलावा, आपको पैडिंग के लिए 6 पीएक्स जोड़ना होगा।

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); 
} 
सही नियंत्रण आप टूलटिप खुद Tooltip.OwnerDraw और घटना Tooltip.Draw साथ आकर्षित कर सकता है, फ़ॉन्ट, गद्दी और उपस्थिति को चुनने के लिए

+0

धन्यवाद, फ़ॉन्ट/आकार बदलना भी काम करता है। –