Domanda

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?

È stato utile?

Soluzione

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top