|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Label and TextAlignI have a window with height of 24. I have a label on the said window also
with a height of 24. I have set the Flatstyle to Flat so I can set the background to transparent. Now if I use a smallish font (8 or so) it always displays in the top left of the label irrespective of what the TextAlign is set to. The only way the text responds to TextAlign is if I set the Flatstyle to System (which is no good as I can no longer have a transparent background). Anyone have an idea what gives? mick Hi Mick,
I'm afraid I am unable to reproduce this. Using a WinForm dialog with BorderStyle.None and Height 24 I had no problem adjusting the TextAlign property on a label with FlatStyle.Flat and Background.Transparent. Using an image as the form background the label was clearly transparent and properly aligned. If you still have the problem, can you give us some more details and possibly code that will reproduce the issue? -- Show quoteHide quoteHappy Coding! Morten Wennevik [C# MVP] "mick" wrote: > I have a window with height of 24. I have a label on the said window also > with a height of 24. I have set the Flatstyle to Flat so I can set the > background to transparent. Now if I use a smallish font (8 or so) it always > displays in the top left of the label irrespective of what the TextAlign is > set to. The only way the text responds to TextAlign is if I set the > Flatstyle to System (which is no good as I can no longer have a transparent > background). > > Anyone have an idea what gives? > > mick > >
Show quote
Hide quote
> "mick" wrote: Realised what the problem is. I had created a class derived from the Label > >> I have a window with height of 24. I have a label on the said window also >> with a height of 24. I have set the Flatstyle to Flat so I can set the >> background to transparent. Now if I use a smallish font (8 or so) it >> always >> displays in the top left of the label irrespective of what the TextAlign >> is >> set to. The only way the text responds to TextAlign is if I set the >> Flatstyle to System (which is no good as I can no longer have a >> transparent >> background). >> >> Anyone have an idea what gives? >> >> mick "Morten Wennevik [C# MVP]" <MortenWenne***@hotmail.com> wrote in message news:DC8B8D98-6EB5-40C9-9FCE-64B027E971AF@microsoft.com... > Hi Mick, > > I'm afraid I am unable to reproduce this. Using a WinForm dialog with > BorderStyle.None and Height 24 I had no problem adjusting the TextAlign > property on a label with FlatStyle.Flat and Background.Transparent. Using > an > image as the form background the label was clearly transparent and > properly > aligned. > > If you still have the problem, can you give us some more details and > possibly code that will reproduce the issue? > > -- > Happy Coding! > Morten Wennevik [C# MVP] class and had overriden the OnPaint method as below protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawString(this.Text, this.Font, new SolidBrush(this.ShadowColor),displacement); g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor),new Point(0,0)); } The new class has two new properties - ShadowColor and Displacement. All it does is print the text twice to give a shadow effect. Problem is my new class now ignores the TextAlign. I just assumed that if I didnt handle it it would be handled by the parent class. It seems not. I`m new to this so if anyone could give me a nudge as to what I should be doing it would be appreciated. I imagine it`s something along the lines of reading the TextAlign property and working out the x and y from that? mick On Thu, 09 Jul 2009 19:54:33 -0700, mick <coughco***@privacy.com> wrote:
> [...] Nope. That's not how C#, or any of the mainstream OOP languages for that > The new class has two new properties - ShadowColor and Displacement. All > it does is print the text twice to give a shadow effect. Problem is my > new class now ignores the TextAlign. I just assumed that if I didnt > handle it it would be handled by the parent class. It seems not. matter, work. Your parent class isn't going to do _anything_ for you in an override, unless you specifically call the parent class's implementation. > I`m new to this so if anyone could give me a nudge as to what I should You will probably have better results if you use the TextRenderer class, > be doing it would be appreciated. I imagine it`s something along the > lines of reading the TextAlign property and working out the x and y > from that? with its DrawText() method. It's basically what the Forms controls use to draw text, so it's a lot easier to get identical results to the built-in controls if you use that. You'll have to check each applicable property in the class that affects drawing and translate that to the appropriate flags for the DrawText() method. Of course, since you _aren't_ calling the parent implementation, it begs the question as to why you are bothering to subclass Label at all. You're not _enhancing_ the Label class, you're simply replacing its functionality with something else. You might as well just make your own Control-derived class instead. This has the added benefit that then you can include only properties that will actually affect the drawing of the text. Subclassing Label, you're going to have all these properties that Label respects, but which won't have any effect in your sub-classed control until you explicitly handle them. Either you're going to go through each and every one and duplicate the functionality in Label, or you're not. If you do, then you've basically just re-written Label, and it might as well be its own class. If you don't, then you've broken Label by implying to a user some property they might set would work, even though it won't. In that class also you would be better off with a completely independent custom class, so that there are only properties in the class that actually affect the way the control behaves. Pete On Thu, 09 Jul 2009 20:29:29 -0700, Peter Duniho
<no.peted.spam@no.nwlink.spam.com> wrote: Show quoteHide quote > [...] Sorry...I meant to, somewhere in all that, point out that no...you don't >> I`m new to this so if anyone could give me a nudge as to what I should >> be doing it would be appreciated. I imagine it`s something along the >> lines of reading the TextAlign property and working out the x and y >> from that? > > You will probably have better results if you use the TextRenderer class, > with its DrawText() method. It's basically what the Forms controls use > to draw text, so it's a lot easier to get identical results to the > built-in controls if you use that. > > You'll have to check each applicable property in the class that affects > drawing and translate that to the appropriate flags for the DrawText() > method. [...] need to "work out the x and y". Whether you are using Graphics.DrawString() or TextRenderer.DrawText(), you can pass appropriate flags to the method, along with a rectangle within which the text should be drawn, and the method itself will handle issues like alignment for you. Pete
Other interesting topics
LINQ: Count records
C# program creating folder on server/access denied A very small complete program that doesn't work as expected GZipStream catch-22 a simple client example Problem setting system time... Accessing List Members AttachConsole with Windows Service? get current platform (x86, x64) Gallery |
|||||||||||||||||||||||