How to draw on a Window in WPF (best practice)?

前端 未结 5 520
无人及你
无人及你 2021-02-01 04:01

I am trying to write a small interactive game-like application, where I need to have a Draw method that\'s gonna draw on screen, but can\'t figure out how to struct

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 04:22

    Typically, you "draw" in WPF in a completely different manner.

    In Windows Forms/GDI, the graphics API is an immediate mode graphics API. Each time the window is refreshed/invalidated, you explicitly draw the contents using Graphics.

    In WPF, however, things work differently. You rarely ever directly draw - instead, it's a retained mode graphics API. You tell WPF where you want the objects, and it takes care of the drawing for you.

    The best way to think of it is, in Windows Forms, you'd say "Draw a line from X1 to Y1. Then draw a line from X2 to Y2. Then ...". And you repeat this every time you need to "redraw" since the screen is invalidated.

    In WPF, instead, you say "I want a line from X1 to Y1. I want a line from X2 to Y2." WPF then decides when and how to draw it for you.

    This is done by placing the shapes on a Canvas, and then letting WPF do all of the hard work.

提交回复
热议问题