custom midi background

D

DaveL

when painting the background of a mdi app window
how can i resolve the flicker when it paints
from darker to lighter colors

below is the code i wrote


private void ShellWindow_Gradient(object sender, PaintEventArgs e)
{



Graphics oGraphics = (Graphics)e.Graphics;
//enum i Defined
if (this.StyleType == NetToolsUI.WindowStyle.Solid)
{

this.oMdi.BackColor = this.SolidColor; //.RoyalBlue;
//.FromArgb(35,7,143); //.Brushes.AliceBlue;

}
else if (this.StyleType == NetToolsUI.WindowStyle.Gradient)
{
Rectangle rect = oMdi.ClientRectangle;
rect.Inflate(2, 2);// to completely fill the client area

LinearGradientBrush filler = new LinearGradientBrush(
rect,
this.SolidColor,
this.LightColor,
90,true); //this._angle);
oGraphics.FillRectangle(filler, rect);


filler.Dispose();
}
//Draw Some Text On the Window
// Create string to draw.
//e.MeasureString(drawString, this.Font);
// Create font and brush.
System.Drawing.SizeF TxtSize = oGraphics.MeasureString(Header,
this.HeaderFont);
SolidBrush GreyBrush = new SolidBrush(this.ShadowColor);
SolidBrush BlackBrush = new SolidBrush(this.HeaderColor);
// Create point for upper-left corner of drawing.
//x=widht
//y=height
//centers
//float x = (this.ClientSize.Width / 2) - (TxtSize.Width / 2);
//float y = (this.ClientSize.Height / 2) - (TxtSize.Height / 2);
float x = (oMdi.ClientSize.Width / 2) - (TxtSize.Width / 2);
float y = ((float)(oMdi.ClientSize.Height
/this.HeaderLocation)) - (float)(TxtSize.Height/HeaderLocation );
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags =
StringFormatFlags.MeasureTrailingSpaces;
//draw Shadow
oGraphics.DrawString(Header, this.HeaderFont, GreyBrush, x + 3,
y + 3, drawFormat);
// Draw string to screen.

oGraphics.DrawString(Header, this.HeaderFont, BlackBrush, x, y,
drawFormat);
//draw Owner Notice bottom Right


BlackBrush = new SolidBrush(this.FooterColor);
TxtSize = oGraphics.MeasureString(Footer, this.FooterFont);

//text
oGraphics.DrawString(Footer, this.FooterFont, BlackBrush,
oMdi.ClientSize.Width - (TxtSize.Width+10),
oMdi.ClientSize.Height
- (TxtSize.Height+10), drawFormat);


}

Thanks DaveL
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

Well, you are doing quite a bit of work there in the method.
Specifically, I see three brushes (which should have Dispose called on them,
btw) which you are creating each time that method is called. The paint
event can be fired a number of times, so I would recommend that you create
filler, blackbrush, greybrush, etc, etc outside of the paint method, and
then change them when needed (when you change the color in your
preferences). That way, you are not constantly allocating those objects.

You would then dispose those brushes when the appropriate preferences
change, or when the application terminates.

You might also want to cache the StringFormat instance (or better yet,
create it once for the lifetime of your app, since it doesn't seem like it
is dependent on anything in it).
 
D

DaveL

Thanks alot....will clean it up better

DaveL

Nicholas Paldino said:
Dave,

Well, you are doing quite a bit of work there in the method.
Specifically, I see three brushes (which should have Dispose called on
them, btw) which you are creating each time that method is called. The
paint event can be fired a number of times, so I would recommend that you
create filler, blackbrush, greybrush, etc, etc outside of the paint
method, and then change them when needed (when you change the color in
your preferences). That way, you are not constantly allocating those
objects.

You would then dispose those brushes when the appropriate preferences
change, or when the application terminates.

You might also want to cache the StringFormat instance (or better yet,
create it once for the lifetime of your app, since it doesn't seem like it
is dependent on anything in it).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DaveL said:
when painting the background of a mdi app window
how can i resolve the flicker when it paints
from darker to lighter colors

below is the code i wrote


private void ShellWindow_Gradient(object sender, PaintEventArgs e)
{



Graphics oGraphics = (Graphics)e.Graphics;
//enum i Defined
if (this.StyleType == NetToolsUI.WindowStyle.Solid)
{

this.oMdi.BackColor = this.SolidColor; //.RoyalBlue;
//.FromArgb(35,7,143); //.Brushes.AliceBlue;

}
else if (this.StyleType == NetToolsUI.WindowStyle.Gradient)
{
Rectangle rect = oMdi.ClientRectangle;
rect.Inflate(2, 2);// to completely fill the client area

LinearGradientBrush filler = new LinearGradientBrush(
rect,
this.SolidColor,
this.LightColor,
90,true); //this._angle);
oGraphics.FillRectangle(filler, rect);


filler.Dispose();
}
//Draw Some Text On the Window
// Create string to draw.
//e.MeasureString(drawString, this.Font);
// Create font and brush.
System.Drawing.SizeF TxtSize = oGraphics.MeasureString(Header,
this.HeaderFont);
SolidBrush GreyBrush = new SolidBrush(this.ShadowColor);
SolidBrush BlackBrush = new SolidBrush(this.HeaderColor);
// Create point for upper-left corner of drawing.
//x=widht
//y=height
//centers
//float x = (this.ClientSize.Width / 2) - (TxtSize.Width / 2);
//float y = (this.ClientSize.Height / 2) - (TxtSize.Height /
2);
float x = (oMdi.ClientSize.Width / 2) - (TxtSize.Width / 2);
float y = ((float)(oMdi.ClientSize.Height
/this.HeaderLocation)) - (float)(TxtSize.Height/HeaderLocation );
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags =
StringFormatFlags.MeasureTrailingSpaces;
//draw Shadow
oGraphics.DrawString(Header, this.HeaderFont, GreyBrush, x +
3, y + 3, drawFormat);
// Draw string to screen.

oGraphics.DrawString(Header, this.HeaderFont, BlackBrush, x,
y, drawFormat);
//draw Owner Notice bottom Right


BlackBrush = new SolidBrush(this.FooterColor);
TxtSize = oGraphics.MeasureString(Footer, this.FooterFont);

//text
oGraphics.DrawString(Footer, this.FooterFont, BlackBrush,
oMdi.ClientSize.Width - (TxtSize.Width+10),

oMdi.ClientSize.Height - (TxtSize.Height+10), drawFormat);


}

Thanks DaveL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top