How to make the + operator work while adding two Points to each other?

后端 未结 3 730
终归单人心
终归单人心 2020-12-07 02:06

Is there any way to get the + operator to work for the Point object?

Take, for example, this tiny snippet:

this.cm1.Show((MouseEventArg         


        
3条回答
  •  庸人自扰
    2020-12-07 02:58

    I read the documentation for System.Drawing.Point (linked in Cody Gray's answer), and it has an instance method Offset. That method mutates the current Point (the designers chose to make Point a mutable struct!).

    So here's an example:

    var p1 = new Point(10, 20);
    var p2 = new Point(6, 7);
    p1.Offset(p2); // will change p1 into the sum!
    

    In the same doc I also see an explicit conversion from Point to Size. Therefore, try this:

    var p1 = new Point(10, 20);
    var p2 = new Point(6, 7);
    Point pTotal = p1 + (Size)p2; // your solution?
    

提交回复
热议问题