Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2005

Tip: Looking for answers? Try searching our database.

DrawString calls inconsistant in Paint Handler!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kumarforg - 15 Oct 2005 11:35 GMT
Hi,

    I am using Graphics.DrawString in the paint handler of a label.
The text comes up and behaves as expected on all events such as resize of
the lable control.

The problem is when i move a messagebox/ another application( say notepad )
across the lable.

In this case a clean repaint of the text fails to appear. The text gets
drawn erratically or altogether disappears.

What could i be missing out on in this code.

I tried inheriting from System.Windows.Forms.Label and overriding the
onpaint method with same results. I had also tried

having the usercontrol as UserControlStyles.UserPaint.

I was trying to avoid having to create the Graphics object for the lable in
the main form as this is not advisable; but i

tried that also with even worse results.

Any help to get me have the paint handler working properly would be greatly
appreciated.

Regards,

//code snippet
        private void lblMessage_Paint(object sender, PaintEventArgs e)
        {
            if( lblMessage.Text != "" )
                lblMessage.Text = "";
            Graphics g = e.Graphics;// Graphics.FromHwnd( this.Handle );

            //g.SmoothingMode = SmoothingMode.AntiAlias;

               
            StringFormat sf = new StringFormat();
            sf.Trimming = StringTrimming.EllipsisCharacter;
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.FormatFlags = StringFormatFlags.LineLimit |
                StringFormatFlags.FitBlackBox |
                StringFormatFlags.NoClip
                ;

            g.DrawString (
                strText,
                this.Font,
                Brushes.Black,
                new RectangleF(
                g.VisibleClipBounds.Location,
                g.VisibleClipBounds.Size),
                sf );

        }
Bob Powell [MVP] - 15 Oct 2005 16:13 GMT
Why do you get the graphics, which may be a completely different graphics
object, when the correct Graphics object to draw on is handed to you in the
event arguments?

In fact, you're getting the paint notification from the label, handling that
and using the Graphics of the parent object. I'm not surprised it's feeling
a bit off-colour.

Just use the Graphics you're handed in the PaintEventArgs.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Hi,
>
[quoted text clipped - 56 lines]
>
> }
Andrew Smith (Infragistics) - 15 Oct 2005 20:41 GMT
You're using the VisibleClipBounds as the location where the text should be
drawn but that will be the area within the graphics object that can be
rendered to. So if only part of the window is invalidated, that value is
going to be just the area that needs to be rendered. You should render based
on the client area where you want to draw in the control and not based on
the VisibleClipBounds. For a test, try passing in your control's
clientrectangle if you want to fill the area of the control.

> Hi,
>
[quoted text clipped - 56 lines]
>
> }
Kumarforg - 17 Oct 2005 11:05 GMT
Thanks for the help Bob and Andrew,

    It's an honour to get an answer from you guys;
             
               Andrew's answer took me further;
    But what fixed my problem was having the size of the rectangle being passed
to drawstring as that of the lable itself;

    I was passing the Graphics.VisibleClipBounds and then Graphics.ClipBounds
as suggested by Andrew.

    The fix was suggested to me by my friend Kiran Gurujane.

g.DrawString (strLblMessageText,
                    this.Font,
                    Brushes.Black,
        new RectangleF(
            new PointF( 1,1 ),
            new SizeF( lblMessage.Size.Width,lblMessage.Size.Height ) ),
            sf );

regards

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.