Continue to chat about WPF-digital ink B

Source: Internet
Author: User

 

In the previous section, we created a tablet example in a simple way. Next, let's create a more complex tablet.Program. The sample program has two options: Select "writing" to enter it manually, and select "erase" to remove unnecessary parts, similar to the eraser function. You can also select the stroke color. The first step is to put an inkcanvas control. I believe it is okay. It is very simple.
 
<Grid> <grid. rowdefinitions> <rowdefinition Height = "*"/> <rowdefinition Height = "Auto"/> </grid. rowdefinitions> <inkcanvas X: Name = "myinc" grid. row = "0" margin = "2"/> ......... </GRID>

Define two classes to save the custom class and set of pen tip colors respectively. 

Public class mycolor {Public String colorname {Get; set;} public solidcolorbrush usercolor {Get; Set ;}} public class mycolors: observablecollection <mycolor> {public mycolors () {Add (New mycolor {colorname = "red", usercolor = new solidcolorbrush (colors. red)}); add (New mycolor {colorname = "yellow", usercolor = new solidcolorbrush (colors. yellow)}); add (New mycolor {colorname = "blue", usercolor = new solidcolorbrush (colors. blue )});}}

Switch to the XAML view and embed mycolors as a static resource into the resource list of the form. Define a data template for items displayed in ListBox. 

<Window. resources> <m: mycolors X: Key = "bindcolors"/> <datatemplate X: Key = "itemtmp" datatype = "{X: Type M: mycolor} "> <stackpanel orientation =" horizontal "> <rectangle Height =" 10 "width =" 20 "fill =" {binding usercolor} "/> <textblock text = "{ binding colorname} "/> </stackpanel> </datatemplate> </window. resources>

Declare two radiobutton elements to select the handwritten input state. And a ListBox control. The data source is the mycolors class we just defined. 

 
   
    
    
    
     
      
       
      
     
    
   

To horizontally arrange items in The ListBox, We need to customize the panel of the item. 

 
<Itemspaneltemplate> <stackpanel orientation = "horizontal"/> </itemspaneltemplate>

Write an event handler for two single-choice buttons. 

 
Private void rdwrite_checked (Object sender, routedeventargs e) {This. myinc. editingmode = inkcanvaseditingmode. inkandgesture;} private void rdera_checked (Object sender, routedeventargs e) {This. myinc. erasershape = new ellipsestylusshape (6, 6); this. myinc. editingmode = inkcanvaseditingmode. erasebypoint ;}

 You can modify the editingmode attribute of inkcanvas to control its editing status, such as receiving or erasing ink marks. Finally, write the ListBox event handler to dynamically modify the color of the stroke. 

 
Private void mylistbox_selectionchanged (Object sender, selectionchangedeventargs e) {If (E. addeditems. count = 0) {return;} mycolor MC = E. addeditems [0] As mycolor; If (MC! = NULL) {myinc. defaultdrawingattributes. Color = mc. usercolor. color ;}}

Related Article

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.