如何通过Click Event更改pictureBox对象的参考移动定位点?
Great question! I’ve dealt with this exact need when building UIs that require precise, intuitive positioning—totally get why the default top-left anchor doesn’t cut it for your scenario.
The short answer: WinForms doesn’t have a built-in setting to change a control’s anchor point away from the top-left corner, but we can easily simulate this with offset calculations. Here’s how to make your "Yellow Circle" point the reference for pictureBoxRed when clicking the "Black Star" position:
Step 1: Define Your Custom Anchor Offset
First, calculate how far your reference point (Yellow Circle) is from the PictureBox’s top-left corner. This is your offset value. For example:
- If the Yellow Circle is the center of
pictureBoxRed, the offset is half the control’s width and height. - If it’s a fixed point (e.g., 25px from the left, 30px from the top), use those exact values.
Store this offset as a field in your form class so it’s accessible in event handlers:
// Field to hold the offset from PictureBox top-left to your custom anchor point private Point _customAnchorOffset;
Step 2: Initialize the Offset
Set the offset when your form loads (or when the PictureBox is initialized). If your PictureBox size might change (e.g., dynamic image loading), update the offset in the SizeChanged event too:
private void Form1_Load(object sender, EventArgs e) { // Example: Set anchor to the center (your Yellow Circle) UpdateCustomAnchorOffset(); } private void pictureBoxRed_SizeChanged(object sender, EventArgs e) { // Re-calculate offset if the PictureBox size changes UpdateCustomAnchorOffset(); } private void UpdateCustomAnchorOffset() { // Center anchor: (half width, half height) _customAnchorOffset = new Point( pictureBoxRed.Width / 2, pictureBoxRed.Height / 2 ); // For a fixed point instead: // _customAnchorOffset = new Point(25, 30); // 25px left, 30px top }
Step 3: Adjust Position on Click
In your click event handler (e.g., on the form or parent panel), calculate the new Location for pictureBoxRed by subtracting the offset from the click position. This ensures your custom anchor point lands exactly where the user clicked (Black Star):
private void Form1_Click(object sender, EventArgs e) { // Get the exact click position relative to the form Point clickPosition = e.Location; // Calculate the new top-left position for the PictureBox: // Click position minus the anchor offset = where the top-left needs to be Point newLocation = new Point( clickPosition.X - _customAnchorOffset.X, clickPosition.Y - _customAnchorOffset.Y ); // Apply the new position pictureBoxRed.Location = newLocation; }
Quick Note on Coordinates
If your click event is triggered from a child control (like a Panel), convert the click coordinates to be relative to the form first:
// If clicking inside a Panel named panel1 Point screenClick = panel1.PointToScreen(e.Location); Point formRelativeClick = this.PointToClient(screenClick);
This approach works for any custom anchor point—you just adjust the _customAnchorOffset to match your Yellow Circle’s position relative to pictureBoxRed’s top-left corner.
内容的提问来源于stack exchange,提问作者drakislow

