How to make the + operator work while adding two Points to each other?
It's not going to happen the way you expect. The only overload that the Point
structure provides for the +
(addition) operator is one that translates the coordinates of the Point
by a Size
.
There's no way to add two Point
structures together, and I'm not even sure what that would mean.
Don't waste too much time figuring it out, either, considering that you cannot write extension methods that overload operators.
Fortunately, in a compiled language, there's no penalty for splitting up code into multiple lines. So you can re-write your code as follows:
Point newLocation = new Point(e.Location.X + this.i_rendered.Location.X,
e.Location.Y + this.i_rendered.Location.Y);
this.cm1.Show(newLocation);
Alternatively, you could use the Offset
method, but I'm not convinced that enhances readability.
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?