How can I draw a square around an object, like thi

2019-09-22 03:20发布

I want to write a GUI editor in C# for AutoIt, but I am not good enough with C#. I want to draw a square (focused) around an object when any object in the GUI is pressed. Like this:

http://i.imgur.com/c9TB8.png

Is there any library to make easier to write this kind of thing?

标签: c# controls
2条回答
The star\"
2楼-- · 2019-09-22 03:39

Square is drawn with one of DrawRectangle functions. Each of them requires a pen. Usually we use ordinary solid pen, but you need a pen with changed DashStyle property. For dotted lines change this property to DashStyle.Dot. You can also experiment with DashPattern property.

To draw little squares around the big square you need one of FillRectangle functions. Each of them requires a brush. You need a white brush, which is conveniently predefined for you to use. After filling a rectangle, you have to draw a rectangle over it with the same dimensions. These two functions together give an impression of empty and lined rectangle.

To make little squares a little rounded, like they are in the image, you have to change a pen parameter used when calling DrawRectangle. Experiment with LineJoin, and other properties of the Pen class.

查看更多
地球回转人心会变
3楼-- · 2019-09-22 04:04

This is very hard for a simple question that you posted. There are a lot of things that you need to take care of.

  1. First I would suggest to make a class that will have a Rectangle property since you can't subclass Rectangle because it is a structure.
  2. You will need to handle the drawing, that is the simplest task as mentioned in the other answers so I'm not going to be specific about that.
  3. Since you have small squares that indicate that the rectangle can be resized, you will have to implement method that checks whether the mouse point is within the big rectangle or some of the small suqares. In this case you should change your cursor to indicate the possibility of resizing.
  4. To handle moving (but not resizing) of the rectangle you will have either to make a new small square with the sign for moving in all directions or you will handle that using cursor when the mouse position is within the Big Rectangle.
  5. The main problem is identifying what to change when resizing, you have two options:(1) to change Location and Size properties or to change X, Y, Width and (2)Height properties of the rectangle. For instance, when you are moving top-right corner you should change both Location and Size in first case or Y and Width is you are using second option.
  6. When you move the mouse while it is clicked, you should watch out in which direction mouse moves. If you split the viewport in quadrants where the center of your rectangle is the center of the Decartes coordinate system, by identifying in which quadrant is the mouse you will know which corner (or edge) of the quadrant you need to move.
  7. Each mouse move will have to call Invalidate() because you can't use Xor like in C++. Therefore, when your Rectangle is displayed, you should be in a special mode where everything that not moves (not changes, everything except Rectangle and selected control) should be drawn to the Bitmap that is used between two redraws and you should only draw what is moving.

As you can see, there is a lot of things that should be taken care of. You should start with this only after you are sure that you have already implemented other parts of your program.

查看更多
登录 后发表回答