Posts Tagged ‘canvas’

How to draw something on canvas in C#

1. Create a canvas frame
In design screen of WPF, you create a canvas frame like the image below:
Don’d forget to name canvas frame to Name=”canvas”
Now, we practice to draw a line and a ellipse on the above canvas
2. Draw a line
The ways to draw shapes on canvas are similar to each other.

  • First, you have to define an instance of that shape.
  • Second, you initiate some obligation attributes for that object, for detail they are coordination, width, height, and color.
  • Final, you import that shape into canvas children list.

Let’s begin with a Line
Read more…

Categories: C# Tags: , , , ,

How to create a Timer in C#

In this post, I introduce how to create a timer for repeated execution with an validate on the screen. The case is when you create an function to draw and redraw something on canvas, and you want after an interval period of time, the function is executed again. My solution is using DispatcherTimer belongs to using System.Windows.Threading class.

//dont forgetto use: using System.Windows.Threading;

private DispatcherTimer timer;
private long runTume = 0;
//then you have 
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(tick);
timer.Tick += new EventHandler(timer_Tick);
void timer_Tick(object sender, EventArgs e)
    //throw new NotImplementedException();
    runTime += tick / 10000000;

in this case, draw(runTime); function will draw on canvas screen.
Notice that in the timer.Interval = new TimeSpan(tick); the tick value has value of 100 nanosecond, so you should concern to adjust to fix your interval time.

%d bloggers like this: