2010-12-16 18 views
7

में घुमाए गए पाठ संरेखित करने के लिए मुझे लेबल में टेक्स्ट घुमाने में सक्षम होना चाहिए और इसे बाएं, दाएं या केंद्र में संरेखित करना होगा। अब तक मैं व्युत्पन्न लेबल की OnPaint विधि में इस कोड के साथ रोटेशन ऐसा करने में सक्षम हूँ:सी #

float width = graphics.MeasureString(Text, this.Font).Width; 
float height = graphics.MeasureString(Text, this.Font).Height; 

double angle = (_rotationAngle/180) * Math.PI; 
graphics.TranslateTransform(
    (ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle)))/2, 
    (ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle)))/2); 
graphics.RotateTransform(270f); 
graphics.DrawString(Text, this.Font, textBrush, new PointF(0,0), stringFormat); 
graphics.ResetTransform(); 

और यह ठीक काम करता है। मैं 270 डिग्री घूर्णन पाठ देख सकते हैं।

लेकिन जब मैं स्ट्रिंगफॉर्मैट में संरेखण सेट करने का प्रयास करता हूं तो यह पागल हो जाता है, और मैं यह नहीं समझ सकता कि क्या हो रहा है।

मेरे पास 270 डिग्री से घूर्णन पाठ कैसे हो सकता है और इसे संरेखित कर सकता है?

+0

क्या संरेखण आप सेट कर रहे हैं? – Aliostad

+0

पहले के करीब, लेकिन फिर जब आप ग्राफिक्स को बदलते हैं तो मैं सुदूर और केंद्र –

+0

में बदलना चाहता हूं, तो संपूर्ण "विश्व" बदल जाता है, इसलिए निकट वास्तव में निकट नहीं है। क्या आप इसे अपनी स्थिति में सेट नहीं कर सकते हैं? – Aliostad

उत्तर

23

यदि कोई सुझाव ढूंढ रहा था, तो यहां 0, 9 0, 180, 270, और 360 डिग्री रोटेशन का समाधान है, जहां स्ट्रिंग एलीगमेंट काम करता है।

एक चीज मूल को स्थानांतरित करने के लिए सही बिंदु चुन रही थी, और दूसरा रोटेशन के अनुसार प्रदर्शन आयताकार को संशोधित करना था।

StringFormat format = new StringFormat(); 
format.Alignment = StringAlignment.Center; 

SizeF txt = e.Graphics.MeasureString(Text, this.Font); 
SizeF sz = e.Graphics.VisibleClipBounds.Size; 

//90 degrees 
e.Graphics.TranslateTransform(sz.Width, 0); 
e.Graphics.RotateTransform(90); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format); 
e.Graphics.ResetTransform(); 

//180 degrees 
e.Graphics.TranslateTransform(sz.Width, sz.Height); 
e.Graphics.RotateTransform(180); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format); 
e.Graphics.ResetTransform(); 

//270 degrees 
e.Graphics.TranslateTransform(0, sz.Height); 
e.Graphics.RotateTransform(270); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format); 
e.Graphics.ResetTransform(); 

//0 = 360 degrees 
e.Graphics.TranslateTransform(0, 0); 
e.Graphics.RotateTransform(0); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format); 
e.Graphics.ResetTransform(); 

यदि आप इस कोड को लेबल के ऑनपेंट ईवेंट में डालते हैं, तो यह आपके घुमावदार फॉर्म का शीर्षक चार बार प्रदर्शित करेगा। एड्रियन Serafin के जवाब की

0

एक्सटेंशन यदि आप पर एक गैर-0 एक्स आकर्षित करने के लिए की जरूरत है, Y:

//90 degrees 
e.Graphics.TranslateTransform(sz.Width, 0); 
e.Graphics.RotateTransform(90); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, 
    new RectangleF(sz.ToPointF().Y, sz.ToPointF().X, sz.Height, sz.Width), format); 
e.Graphics.ResetTransform(); 
//180 degrees 
e.Graphics.TranslateTransform(sz.Width, sz.Height); 
e.Graphics.RotateTransform(180 this.Font, Brushes.Black, 
    new RectangleF(-sz.ToPointF().X, -sz.ToPointF().Y, sz.Width, sz.Height), format); 
e.Graphics.ResetTransform(); 
//270 degrees 
e.Graphics.TranslateTransform(0, sz.Height); 
e.Graphics.RotateTransform(270); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, 
    new RectangleF(-sz.ToPointF().Y, sz.ToPointF().X, sz.Height, sz.Width), format); 
//0 = 360 degrees 
e.Graphics.TranslateTransform(0, 0); 
e.Graphics.RotateTransform(0); 
e.Graphics.DrawString(Text, this.Font, Brushes.Black, 
    new RectangleF(sz.ToPointF().X, sz.ToPointF().Y, sz.Width, sz.Height), format); 
e.Graphics.ResetTransform();