Examples of using visualbrush in WPF

Source: Internet
Author: User

This article implements a small program named "You come and go", which manages the cash owned by two shoes, Zhang San and Li Si. At the beginning, each of the two shoes has 100 US dollars in cash, with the transfer of cash from one person to another, the amount of cash owned by the two is changing, and the procedure can track the change and correctly display the amount of cash owned by each person. You can transfer up to three banknotes each time. The amount of banknotes can be 5, 10, or 20.

After the program runs, the effect 1 is shown. We click the "$5", "$10", and "$20" button on the right of "Zhang San", and then click the "play" button, after the cash flow is completed from "Michael" to "Mr. Li", the cash owned by "Michael" is changed to 135 US dollars, while "Mr. Li" only has 65 US dollars, the result 2 after running is shown in.

 

Figure 1 initial status chart

 

Figure 2 Distribution chart after cash flow

When you click "$5", "$10", or "$20, the "$5", "$10", and "$20" buttons will be copied to the "waiting for payment" area between "Michael" and "Lee ", to complete this operation, you need to use visualbrush.

 

Figure 3 cash flow into the area to be paid

Click "cancel" to clear the buttons in the "to be paid" area.

Visualbrush is a derived class of the WPF brush class, and the brush class is an abstract class. In WPF, the class derived from the brush class includes: solidcolorbrush (solid paint brush) and lineargradientbrush (linear gradient Paint Brush) radialgradientbrush (radial gradient Paint Brush), imagebrush, drawingbrush, etc. Visualbrush uses visual objects to draw areas. visual objects include buttons and pages. In this example, use visualbrush to draw the rectangle fill. The final effect is to copy the button into the "to be paid" area ". The code for this operation is as follows.

private void AddChildren(Button btn){     VisualBrush vBrush = new VisualBrush(btn);     Rectangle rect = new Rectangle();     rect.Width = btn.ActualWidth;     rect.Height = btn.ActualHeight;     rect.Fill = vBrush;     rect.Opacity = 0.8;     this.StackPanel_Pay.Children.Add(rect);}
In the code, use a button to initialize visualbrush. Then, draw a rectangle Based on the width and height of the button, and set the fill attribute of the rectangle to a visualbrush object, add the newly drawn rectangle to the "to be paid" area ". Okay, that's it.

The complete code of this example is attached below.

Mainwindow. XAML File Code

<Window X: class = "wpfvisualbrushexp. mainwindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: X = "http://schemas.microsoft.com/winfx/2006/xaml" Title = "height height" Height = "350" width = "525" windowstyle = "toolwindow"> <grid. rowdefinitions> <rowdefinition Height = "*"> </rowdefinition> <rowdefinition height = "0.2 *"> </rowdefinition> <rowdefinition Height = "*"> </rowdefinition> <rowdefinition Height = "0.2 *"> </rowdefinition> <rowdefinition Height = "*"> </rowdefinition> <rowdefinition Height = "*"> </rowdefinition> <rowdefinition Height = "*"> </rowdefinition> </grid. rowdefinitions> <grid. columndefinitions> <columndefinition width = "*"> </columndefinition> <comment width = "0.05 *"> </columndefinition> <columndefinition width = "*"> </columndefinition> </grid. columndefinitions> <textblock text = "zhangsan owns" grid. row = "1" grid. column = "0" horizontalalignment = "right" margin = ","/> <textblock X: Name = "textblock_zhangsanamount" grid. row = "1" grid. column = "1" horizontalalignment = "center" margin = ","/> <textblock text = "" grid. row = "1" grid. column = "2" horizontalalignment = "Left" margin = ","/> <button X: Name = "button_tolisifive" content = "$5" grid. row = "0" grid. column = "4" Click = "button_tolisifive_click"/> <button X: Name = "button_tolisiten" content = "$10" grid. row = "1" grid. column = "4" Click = "button_tolisiten_click"/> <button X: Name = "button_tolisitwenty" content = "$20" grid. row = "2" grid. column = "4" Click = "button_tolisitwenty_click"/> <textblock grid. row = "3" grid. column = "0" grid. columnspan = "5" background = "cyan"/> <stackpanel X: Name = "stackpanel_pay" background = "white" grid. row = "4" grid. column = "0" grid. columnspan = "3" orientation = "horizontal"/> <stackpanel grid. row = "4" grid. column = "4"> <button X: Name = "button_pay" content = "pay" Click = "button_pay_click"/> <button X: name = "button_cancel" content = "cancel" Click = "button_cancel_click"/> </stackpanel> <textblock grid. row = "5" grid. column = "0" grid. columnspan = "5" background = "cyan"/> <textblock text = "" grid. row = "7" grid. column = "0" horizontalalignment = "right" margin = ","/> <textblock X: Name = "textblock_lisiamount" grid. row = "7" grid. column = "1" horizontalalignment = "center" margin = ","/> <textblock text = "" grid. row = "7" grid. column = "2" horizontalalignment = "Left" margin = ","/> <button X: Name = "button_tozhangsanfive" content = "$5" grid. row = "6" grid. column = "4" Click = "button_tozhangsanfive_click"/> <button X: Name = "button_tozhangsanten" content = "$10" grid. row = "7" grid. column = "4" Click = "button_tozhangsanten_click"/> <button X: Name = "button_tozhangsantwenty" content = "$20" grid. row = "8" grid. column = "4" Click = "button_tozhangsantwenty_click"/> <textblock grid. row = "0" grid. column = "3" grid. rowspan = "9" background = "cyan"/> </GRID> </WINDOW>

Mainwindow. XAML. CS File Code

//************************************** ********************* // Visualbrush sample code /// Author: may 3, May /// Date: /08/26 // http://blog.csdn.net/yl2isoft ////****************************** * ***************************** using system; using system. collections. generic; using system. LINQ; using system. windows; using system. windows. controls; using system. windows. media; using system. windows. shapes; Nam Espace wpfvisualbrushexp {// <summary> // mainwindow. interaction logic of XAML // </Summary> Public partial class mainwindow: window {bool iszhangsanpaying = false; bool islisipaying = false; list <int> zhangsanpayamountlist = new list <int> (); List <int> lisipayamountlist = new list <int> (); Public mainwindow () {initializecomponent (); this. textblock_zhangsanamount.text = "100"; this. textblock_lisiamount.text = "100" ;}# region Zhang San private void encode (Object sender, routedeventargs e) {If (returnbymaxcount () | returnbylisiispaying () {return;} addchildren (this. button_tolisifive); zhangsanpayamountlist. add (5); iszhangsanpaying = true;} private void button_tolisiten_click (Object sender, routedeventargs e) {If (returnbymaxcount () | response () {return;} addchildren (thi S. button_tolisiten); zhangsanpayamountlist. add (10); iszhangsanpaying = true;} private void evaluate (Object sender, routedeventargs e) {If (returnbymaxcount () | returnbylisiispaying () {return;} addchildren (this. button_tolisitwenty); zhangsanpayamountlist. add (20); iszhangsanpaying = true;} # endregion // Zhang San # region Li Si private void button_tozhangsanfive_click (Object sender, rout Edeventargs e) {If (returnbymaxcount () | returnbyzhangsanispaying () {return;} addchildren (this. button_tozhangsanfive); lisipayamountlist. add (5); islisipaying = true;} private void button_tozhangsanten_click (Object sender, routedeventargs e) {If (returnbymaxcount () | consume () {return;} addchildren (this. button_tozhangsanten); lisipayamountlist. add (10); islisipaying = tru E;} private void button_tozhangsantwenty_click (Object sender, routedeventargs e) {If (returnbymaxcount () | returnbyzhangsanispaying () {return;} addchildren (this. button_tozhangsantwenty); lisipayamountlist. add (20); islisipaying = true;} # endregion // Li Si private bool returnbymaxcount () {If (this. stackpanel_pay.children.count> = 3) {MessageBox. show ("You can only pay 3 USD at a time. "); Return true;} return false;} private bool returnbyzhangsanispaying () {If (iszhangsanpaying = true) {MessageBox. show ("Michael is paying... "); Return true;} return false;} private bool returnbylisiispaying () {If (islisipaying = true) {MessageBox. show ("Li Si is paying... "); Return true;} return false;} private void addchildren (Button BTN) {visualbrush vbrush = new visualbrush (BTN); rectangle rect = new rectangle (); rect. width = BTN. actualwidth; rect. height = BTN. actualheight; rect. fill = vbrush; rect. opacity = 0.8; this. stackpanel_pay.children.add (rect);} private void button_pay_click (Object sender, routedeventargs e) {If (iszhangsanpaying) {int zhangsanamount = convert. toint32 (this. textblock_zhangsanamount.text)-zhangsanpayamountlist. sum (IT => it); int lisiamount = convert. toint32 (this. textblock_lisiamount.text) + zhangsanpayamountlist. sum (IT => it); this. textblock_zhangsanamount.text = zhangsanamount. tostring (); this. textblock_lisiamount.text = lisiamount. tostring (); zhangsanpayamountlist. clear (); iszhangsanpaying = false;} else {int lisiamount = convert. toint32 (this. textblock_lisiamount.text)-lisipayamountlist. sum (IT => it); int zhangsanamount = convert. toint32 (this. textblock_zhangsanamount.text) + lisipayamountlist. sum (IT => it); this. textblock_lisiamount.text = lisiamount. tostring (); this. textblock_zhangsanamount.text = zhangsanamount. tostring (); lisipayamountlist. clear (); islisipaying = false;} This. stackpanel_pay.children.clear ();} private void button_cancel_click (Object sender, routedeventargs e) {This. stackpanel_pay.children.clear (); zhangsanpayamountlist. clear (); lisipayamountlist. clear (); iszhangsanpaying = false; islisipaying = false ;}}}

 


 

 

 


Examples of using visualbrush in WPF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.