Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Changing the drawing style of a button from code ( c++ or c# )

 
 
Old 10-20-2006   #1 (permalink)
John Dunn


 
 

Changing the drawing style of a button from code ( c++ or c# )

What's the best way to change the way a button draws itself using only code?
I've tried a few things and none of them do exactly what I'd like.

- Setting the Background property ( either directly or with a Style ).
Doesn't change the background color when the button is shown highlighted (
due to the mouse being over it ).

- Deriving from Button and overriding OnRender. It appears as though you can
only add to the drawing - the original drawing code is still executed - the
documentation says as much as well.

- Using a decorator. I don't really understand decorators but this seemed to
have pretty much the same effect as the previous solution. I could add my own
drawing to the decorator but the original button still drew as well.

So is there a way to accomplish this? What I'm trying to do is to style a
button that looks like the 'red x' close button on a window. I'm sure I could
acccomplish it by deriving a new class from UIElement and completely
re-implementing the button behaviour but it seems like just changing the
drawing of the button is all I really need to do. I'm using C++/CLI but a C#
example is easily translatable as well.

Thanks-

John Dunn

My System SpecsSystem Spec
Old 10-23-2006   #2 (permalink)
Douglas Stockwell


 
 

Re: Changing the drawing style of a button from code ( c++ or c# )

Hi John,

I would suggest doing so the same way you would do it in XAML. By assigning
a new Control Template / Style.

Here's how you might replace the button's template with a Red Rectangle (in
C#).

Button button = ...;

ControlTemplate template = new ControlTemplate(typeof(Button));
FrameworkElementFactory rect = new
FrameworkElementFactory(typeof(Rectangle));
rect.SetValue(Rectangle.FillProperty, Brushes.Red);
template.VisualTree = rect;
button.Template = template;

- Doug

"John Dunn" <jhndnn@community.nospam> wrote in message
newsF705F00-A994-44D6-B985-B2D5E06F020D@microsoft.com...
> What's the best way to change the way a button draws itself using only
> code?
> I've tried a few things and none of them do exactly what I'd like.
>
> - Setting the Background property ( either directly or with a Style ).
> Doesn't change the background color when the button is shown highlighted (
> due to the mouse being over it ).
>
> - Deriving from Button and overriding OnRender. It appears as though you
> can
> only add to the drawing - the original drawing code is still executed -
> the
> documentation says as much as well.
>
> - Using a decorator. I don't really understand decorators but this seemed
> to
> have pretty much the same effect as the previous solution. I could add my
> own
> drawing to the decorator but the original button still drew as well.
>
> So is there a way to accomplish this? What I'm trying to do is to style a
> button that looks like the 'red x' close button on a window. I'm sure I
> could
> acccomplish it by deriving a new class from UIElement and completely
> re-implementing the button behaviour but it seems like just changing the
> drawing of the button is all I really need to do. I'm using C++/CLI but a
> C#
> example is easily translatable as well.
>
> Thanks-
>
> John Dunn


My System SpecsSystem Spec
Old 10-23-2006   #3 (permalink)
John Dunn


 
 

Re: Changing the drawing style of a button from code ( c++ or c# )

Thanks-

That helped quite a bit. Trying to use a lot of the WPF stuff without using
XAML is like stumbing around in the dark.

"Douglas Stockwell" wrote:

> Hi John,
>
> I would suggest doing so the same way you would do it in XAML. By assigning
> a new Control Template / Style.
>
> Here's how you might replace the button's template with a Red Rectangle (in
> C#).
>
> Button button = ...;
>
> ControlTemplate template = new ControlTemplate(typeof(Button));
> FrameworkElementFactory rect = new
> FrameworkElementFactory(typeof(Rectangle));
> rect.SetValue(Rectangle.FillProperty, Brushes.Red);
> template.VisualTree = rect;
> button.Template = template;
>
> - Doug
>
> "John Dunn" <jhndnn@community.nospam> wrote in message
> newsF705F00-A994-44D6-B985-B2D5E06F020D@microsoft.com...
> > What's the best way to change the way a button draws itself using only
> > code?
> > I've tried a few things and none of them do exactly what I'd like.
> >
> > - Setting the Background property ( either directly or with a Style ).
> > Doesn't change the background color when the button is shown highlighted (
> > due to the mouse being over it ).
> >
> > - Deriving from Button and overriding OnRender. It appears as though you
> > can
> > only add to the drawing - the original drawing code is still executed -
> > the
> > documentation says as much as well.
> >
> > - Using a decorator. I don't really understand decorators but this seemed
> > to
> > have pretty much the same effect as the previous solution. I could add my
> > own
> > drawing to the decorator but the original button still drew as well.
> >
> > So is there a way to accomplish this? What I'm trying to do is to style a
> > button that looks like the 'red x' close button on a window. I'm sure I
> > could
> > acccomplish it by deriving a new class from UIElement and completely
> > re-implementing the button behaviour but it seems like just changing the
> > drawing of the button is all I really need to do. I'm using C++/CLI but a
> > C#
> > example is easily translatable as well.
> >
> > Thanks-
> >
> > John Dunn

>
>

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Contacts folder keep changing view style Vista General
DOS-style code on boot? Vista General
Changing System font style. General Discussion
words fuzzy after changing "style" of windows.. Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46