0

I would like to write an application in C++ that can pan image when user hold and move the mouse. I used a Panel and put a pictureBox on it. The property AutoScroll of the Panel is set to true. Now I am trying to change the position of the scroll bar when the mouse move. I tried a few methods but it does not work.

For simplicity, I use +/-100, +/-100 for codes here. I tried

Point p = new Point(100, 100);
panel1->AutoScrollPosition = p;

It gives me the following error:

cannot convert from 'System::Drawing::Point *' to 'System::Drawing::Point'"

I also tried the following.

panel1->AutoScrollPosition.X = 100;
panel1->AutoScrollPosition.Y = 100;

However, the scrollbar does not move and always return 0,0. I have tried using both -ve and +ve values but it's just not working.

How can I solve this problem?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Novfrank
  • 5
  • 5

1 Answers1

2

System::Drawing::Point is a struct, not a class. Structs are value types, and don't need the new operator. I'm not at a compiler, but I believe this is the syntax you want:

Point p(100, 100);
panel1->AutoScrollPosition = p;

(Also, Point being a managed type, gcnew would be much more appropriate. new works, but is very nonstandard, no APIs will accept a parameter of that type.)

The other thing you tried:

panel1->AutoScrollPosition.X = 100;
panel1->AutoScrollPosition.Y = 100;

That doesn't work because Point is a struct. AutoScrollPosition returns a COPY of the struct, and that's what you modified. C# will give a compiler warning when you try this. If you do need to modify one component of a Point, here's what you need to do (this applies to both C++/CLI and C#):

Point p = panel1->AutoScrollPosition;
p.X = 100;
panel1->AutoScrollPosition = p;
David Yaw
  • 27,383
  • 4
  • 60
  • 93
  • Thanks David, I used your suggestion "Point p(100, 100); panel1->AutoScrollPosition = p;" and it works perfectly!! Btw, I would like to know what's the main difference between struct and class?? Why I "new" a Point does not work?? Thanks again. – Novfrank Mar 21 '13 at 13:40
  • Here's a good answer explaining the difference between managed structs and classes: http://stackoverflow.com/a/13275. "new" doesn't work because that's the operator for the unmanaged heap, you want to use "gcnew" on *any* managed type. "gcnew Point" will work, but that's not how managed structs are passed in any standard API, so you'd just have to cast back to the direct type to make use of it anywhere. – David Yaw Mar 21 '13 at 16:17