Hello Ian,

Tuesday, May 21, 2002, 12:07:46 PM, you wrote:

thanks a lot for this explanation.
well, regarding your question of why used SystemBrushes on same places
and not in other...oh, well, it's that this is the first time I'm
trying to build an app using .net and c#, and I'm a still a little
lost here :)


IG> As someone has already pointed out this looks like it's probably a driver
IG> problem - works fine when I try your code.

IG> However there are some problems in your code.  They're unrelated, but you
IG> should know about them:

>>     gr.FillRectangle( new SolidBrush( SystemColors.ControlLight ), rect );

IG> It would be easier just to use SystemBrushes.ControlLight here instead of
IG> creating your own brush.  It would also be more efficient, as the framework
IG> will cache the Brush that it creates, which will save you from creating a
IG> new one every time.

IG> Also, since you are creating your own brush here you should be calling
IG> Dispose on it when you are finished.  As it stands, this code is relying on
IG> the GC to free the Brush, which means you will consume far more GDI+
IG> resources than necessary.  But in this case, going with the SystemBrushes
IG> class is the preferable solution (they don't need to be Disposed. in fact
IG> they mustn't be diposed.)

IG> However, there's another potential problem.  What behaviour do you want when
IG> the user changes the menu color in the system properties?  Don't you want
IG> your highlight to change colour accordingly?  Otherwise things are going to
IG> look a bit strange.  So what you probably wanted was this:

IG>   Color menuLight = ControlPaint.Light(SystemColors.Menu);
IG>   using (Brush b = new SolidBrush(menuLightBrush))
IG>   {
IG>      gr.FillRectangle( menuLightBrush, rect );
IG>   }

IG> Here we go back to creating our own SolidBrush - this is because there is no
IG> SystemColors.MenuLight, and therefore there is no corresponding
IG> SystemBrushes.MenuLight, so we have to create our own.  Note the use of the
IG> using construct to make sure the brush gets freed.


>>     gr.FillRectangle( new SolidBrush( Color.FromArgb(173, 173, 209) ),
IG> rect );

IG> This hard-coded color probably isn't what you want either.  I'm assuming
IG> you're looking for Office XP/VS.NET-style menu drawing here.  Those menus
IG> are all sensitive to the user's setting for selected items.  By default it
IG> is blue, but if you change your selected item colour to, say, green, then
IG> the Office/VS menu item selection is also in green.  The following code
IG> seems to come up with the same selection colour that Office XP and VS.NET
IG> use:

IG>     Color sel = SystemColors.Highlight;
IG>     Color mix = SystemColors.Window;
IG>     Color selectLight = Color.FromArgb((sel.R + mix.R*2)/3,
IG>         (sel.G + mix.G*2)/3, (sel.B + mix.B*2)/3);
IG>     using (Brush b = new SolidBrush(selectLight))
IG>     {
IG>         gr.FillRectangle( b, rect );
IG>     }

IG> So we've created a colour which is 1/3 menu highlight, and 2/3 Window (i.e.
IG> white on most systems).  This looks right to me, although I've no idea if
IG> it's officially The Right Colour.



>>     gr.DrawRectangle( new Pen( Color.DarkBlue ), rect.X,
>>     rect.Y, rect.Width - 1 , rect.Height - 1 );

IG> This hardcoding is also bad, as is the failure to dispose.  You want to use
IG> SystemPens.Highlight, which will give you the correct colour, and will not
IG> need to be disposed.


>>       gr.FillRectangle( new SolidBrush( SystemColors.ControlLight ),
IG> rect );
>>                         rect.X     += bitmap_size + 5;
>>                         rect.Width -= bitmap_size + 5;
>>       gr.FillRectangle( new SolidBrush( SystemColors.ControlLightLight ),
IG> rect );

IG> This is arguably bad practice - why are you filling the whole area with one
IG> colour when you then paint most of that straight over with another colour?
IG> Just paint the areas you need to paint.

IG> Also, I'm not sure these are the right colours to use.  As it happens,
IG> Office XP and VS.NET appear to be using SystemColors.Window for the pale
IG> part.  I'm not completely sure what it's using for the darker part - it
IG> seems to be the same colour as the face of the toolbars.  This is not a
IG> system color, but it is apparently influenced by SystemColors.Window - it's
IG> a very washed out version of it.  (Try changing your Window colour on your
IG> system to Fuschia and you'll see what I mean.)  So I guess they're maybe
IG> applying a colour transformation to derive that colour.  But I think it
IG> looks kind of odd.

IG> ControlLight actually looks like a good approximation in the default colour
IG> scheme though.  Using Window would be closer to office XP/VS.NET behaviour
IG> for the second one.

IG> Again in both cases you should be using SystemBrushes for two reasons (1) so
IG> that the fact that you've not called Dispose on these brushes is not a
IG> problem, and (2) because it'll be more efficient.

>>         gr.FillRectangle( SystemBrushes.Control, rect );

IG> So how come you used SystemBrushes here?  It's the right thing to do, I just
IG> assumed you didn't know about it, since you hadn't used it anywhere else...
IG> :-)


IG> --
IG> Ian Griffiths
IG> DevelopMentor

IG> You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
IG> subscribe to other DevelopMentor lists at http://discuss.develop.com.



--
Regards,
Luis Abreu
http://www.luisabreu.go.cc/
mailto:[EMAIL PROTECTED]
---------------------------------------------
"Those who give up never win,
and those who win never give up!", Luis Abreu
---------------------------------------------

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to