Home All Groups Group Topic Archive Search About
Author
5 Jul 2009 9:28 PM
mick
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

Author
8 Jul 2009 11:14 AM
Morten Wennevik [C# MVP]
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]


Show quoteHide quote
"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
>
>
Are all your drivers up to date? click for free checkup

Author
10 Jul 2009 2:54 AM
mick
Show quote Hide quote
> "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

"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]

Realised what the problem is. I had created a class derived from the Label
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
Author
10 Jul 2009 3:29 AM
Peter Duniho
On Thu, 09 Jul 2009 19:54:33 -0700, mick <coughco***@privacy.com> wrote:

> [...]
> 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.

Nope.  That's not how C#, or any of the mainstream OOP languages for that 
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 
> 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.

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
Author
10 Jul 2009 3:55 AM
Peter Duniho
On Thu, 09 Jul 2009 20:29:29 -0700, Peter Duniho 
<no.peted.spam@no.nwlink.spam.com> wrote:

Show quoteHide quote
> [...]
>> 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. [...]

Sorry...I meant to, somewhere in all that, point out that no...you don't 
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

Bookmark and Share