Windows Develop Bookmark and Share   
 index > Windows Forms General > Point on PictureBox on Panel
 

Point on PictureBox on Panel

Hi,

I got a problem and would like to seek help.

I got a picturebox control placed in a panel control.
I need to add control on top of the picture. And need to have zoom in/zoom out function and print the controls together with the picture in the picture box.

1) if I add the control to the picturebox, I can use the zoom in/zoom out function and print the picture with the controls on top. However, I cannot get the correct left and top position of the control. Always have deviation from my mouse click point.

2) If I add the control to the panel, i can get the correct left and top, however, I cannot print the picture together with the added control. Only the picture is printed.

How can I get the correct left and top of the controls if I add the control on the picture on a panel? Or, could any one tell me how to print the control together with the picture if I add the control to the panel?

Thanks a lot..

Vicki
Vicki Chan  Thursday, May 21, 2009 2:16 AM

Based on my understanding, the main part of your problem is how to find the positions of a control in picture box. BinaryCoder has already shown the key problem. The code snippet below explains the idea:

Panel panel;

PictureBox picBox;

Button bt;

private void Form1_Load(object sender, EventArgs e)

{

panel = new Panel();

panel.Location = new Point(20, 20);

panel.Size = new Size(200, 200);

panel.BorderStyle = BorderStyle.FixedSingle;

this.Controls.Add(panel);

picBox = new PictureBox();

picBox.Location = new Point(5, 5);

picBox.Size = new Size(100, 100);

picBox.BorderStyle = BorderStyle.Fixed3D;

this.panel.Controls.Add(picBox);

bt = new Button();

bt.Name = "bt";

bt.Location = new Point(2, 2);

bt.Text = bt.Name;

bt.Size = new Size(30, 30);

this.picBox.Controls.Add(bt);

bt.Click += new EventHandler(bt_Click);

}

private void bt_Click(object sender, EventArgs e)

{

Point posInScreen = bt.PointToScreen(Point.Empty);

string posInfo = "";

posInfo += "In PictureBox:" +

GetLocation(this.picBox.PointToClient(posInScreen))

+ "\r\n";

posInfo += "In Panel:" +

GetLocation(this.panel.PointToClient(posInScreen))

+ "\r\n";

posInfo += "In form:" +

GetLocation(this.PointToClient(posInScreen))

+ "\r\n";

MessageBox.Show(posInfo);

}

private string GetLocation(Point p)

{

return p.X + ":" + p.Y;

}

Let me know if this helps.

Best regards,

Aland Li


Please mark the replies as answers if they help and unmark if they don't.
Aland Li  Friday, May 22, 2009 8:25 AM
You need to use PointToClient and/or PointToScreen.

For example, you might be looking for:

Button b = your_button;
Point pt = this.PointToClient(b.PointToScreen(Point.Empty));
BinaryCoder  Thursday, May 21, 2009 2:38 AM

Could you please illustrate more?

What will be the relationship for thetheleft and top ofButton b, PictureBox pic, Panel pnl and this?

Vicki Chan  Thursday, May 21, 2009 3:56 AM

Based on my understanding, the main part of your problem is how to find the positions of a control in picture box. BinaryCoder has already shown the key problem. The code snippet below explains the idea:

Panel panel;

PictureBox picBox;

Button bt;

private void Form1_Load(object sender, EventArgs e)

{

panel = new Panel();

panel.Location = new Point(20, 20);

panel.Size = new Size(200, 200);

panel.BorderStyle = BorderStyle.FixedSingle;

this.Controls.Add(panel);

picBox = new PictureBox();

picBox.Location = new Point(5, 5);

picBox.Size = new Size(100, 100);

picBox.BorderStyle = BorderStyle.Fixed3D;

this.panel.Controls.Add(picBox);

bt = new Button();

bt.Name = "bt";

bt.Location = new Point(2, 2);

bt.Text = bt.Name;

bt.Size = new Size(30, 30);

this.picBox.Controls.Add(bt);

bt.Click += new EventHandler(bt_Click);

}

private void bt_Click(object sender, EventArgs e)

{

Point posInScreen = bt.PointToScreen(Point.Empty);

string posInfo = "";

posInfo += "In PictureBox:" +

GetLocation(this.picBox.PointToClient(posInScreen))

+ "\r\n";

posInfo += "In Panel:" +

GetLocation(this.panel.PointToClient(posInScreen))

+ "\r\n";

posInfo += "In form:" +

GetLocation(this.PointToClient(posInScreen))

+ "\r\n";

MessageBox.Show(posInfo);

}

private string GetLocation(Point p)

{

return p.X + ":" + p.Y;

}

Let me know if this helps.

Best regards,

Aland Li


Please mark the replies as answers if they help and unmark if they don't.
Aland Li  Friday, May 22, 2009 8:25 AM

You can use google to search for other answers

Custom Search

More Threads

• what is wrong in this code openning url?
• VS.net 2005 & SQL Reporting Services ???
• Error:Type not marked as serializable
• Detect and not allow another application to steal focus.
• Zero Touch Deploy with XP Theme manifest file
• prevent focus steal
• toolstripbutton border.
• Serial Port Communication
• Can Windows Client delete my A record on DNS Server when is Log Off?
• VPN Connction