我有一个非常简单的winforms项目。它有一个主窗体和一个用户控件。
UserControl由一个面板和一个按钮组成。
我想在UserControl的顶部放置一个图像。任何图像都可以。
问题是图像显示在用户控件的下面,而不是用户控件的顶部(属于用户控件的按钮的顶部)。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DragImageOnUserControl
{
public partial class Form1 : Form
{
private string _directoryContainingThePieces;
private string _imageFileName;
private Image _image;
public Form1()
{
InitializeComponent();
this.ClientSize = new Size(1000, 800);
this.BackColor = Color.Bisque;
_directoryContainingThePieces = @"g:\Programming\Chess\ChessboardWithPieces\Chess Pieces\";
_imageFileName = "White King.PNG";
_image = Image.FromFile(_directoryContainingThePieces + _imageFileName);
UserControl_PanelAndButton panelAndButton = new UserControl_PanelAndButton();
panelAndButton.Location = new Point(50, 50);
panelAndButton.ImageToDisplay = _image; // Set the image after initializing _image.
this.Controls.Add(panelAndButton);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DragImageOnUserControl
{
public partial class UserControl_PanelAndButton : UserControl
{
public UserControl_PanelAndButton()
{
InitializeComponent();
_panel1 = new Panel();
_panel1.Location = new Point(20, 20);
_panel1.Size = new Size(500, 500);
_panel1.BackColor = Color.Cyan;
_panel1.Parent = this; // Set the panel's parent to the user control
_panel1.SendToBack(); // Send the panel to back.
_button1 = new Button();
_button1.Location = new Point(200, 200);
_button1.Size = new Size(75, 75);
_button1.BackColor = Color.Orchid;
_panel1.Controls.Add(_button1); // Add the button to the panel
_button1.SendToBack(); // Send the button to back.
}
private Panel _panel1;
private Button _button1;
private Image _imageToDisplay;
public Image ImageToDisplay
{
get { return _imageToDisplay; }
set
{
_imageToDisplay = value;
Refresh(); // Force a redraw when the image changes.
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw the image.
if (_imageToDisplay != null)
{
int x = Math.Max(0, (Width - _imageToDisplay.Width) / 2); // Center the image horizontally.
int y = Math.Max(0, (Height - _imageToDisplay.Height) / 2); // Center the image vertically.
e.Graphics.DrawImage(_imageToDisplay, new Point(x, y));
}
}
}
}
这是整个代码。
我还有一个非常类似的项目。它有一个主窗体,而不是UserControl,它有一个面板。
我使用相同的图像,图像显示在面板的顶部。我甚至可以在面板的边界内拖动它。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DragImageOnForm
{
public partial class Form1 : Form
{
private Panel _panel1;
private string _directoryContainingThePieces;
private string _imageFileName;
private Image _image;
private Point _imageLocation;
private Point _dragOffset;
private bool _isDragging;
private Rectangle _previousImageRect;
public Form1()
{
InitializeComponent();
this.ClientSize = new Size(1000, 800);
this.BackColor = Color.Bisque;
_panel1 = new Panel();
_panel1.Location = new Point(200, 200);
_panel1.Size = new Size(500, 500);
_panel1.BackColor = Color.Violet;
_panel1.MouseDown += Panel1_MouseDown;
_panel1.MouseMove += Panel1_MouseMove;
_panel1.MouseUp += Panel1_MouseUp;
_panel1.Paint += Panel1_Paint; // Attach the paint event handler.
this.Controls.Add(_panel1);
_directoryContainingThePieces = @"g:\Programming\Chess\ChessboardWithPieces\Chess Pieces\";
_imageFileName = "White King.PNG";
_image = Image.FromFile(_directoryContainingThePieces + _imageFileName);
_imageLocation = new Point(100, 100); // Initial position.
_isDragging = false;
// Initialize previousImageRect with initial image location and size.
_previousImageRect = new Rectangle(_imageLocation, _image. Size);
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
// Draw the image at the current location on the panel.
e.Graphics.DrawImage(_image, _imageLocation);
}
private void Panel1_MouseDown(object sender, MouseEventArgs e)
{
// Check if the mouse click is within the image's bounds.
Rectangle imageRect = new Rectangle(_imageLocation, _image. Size);
if (imageRect.Contains(e.Location))
{
_isDragging = true;
_dragOffset = new Point(e.X - _imageLocation.X, e.Y - _imageLocation.Y);
}
}
private void Panel1_MouseMove(object sender, MouseEventArgs e)
{
if (_isDragging)
{
// Update the image's location based on the mouse position.
_imageLocation = new Point(e.X - _dragOffset.X, e.Y - _dragOffset.Y);
// Calculate the union of the previous and current image rectangles
// and invalidate only that region to minimize redraw.
Rectangle newImageRect = new Rectangle(_imageLocation, _image.Size);
Rectangle updateRect = Rectangle.Union(_previousImageRect, newImageRect);
// Invalidate to update rectangle.
_panel1.Invalidate(updateRect);
// Update previous image rectangle.
_previousImageRect = newImageRect;
}
}
private void Panel1_MouseUp(object sender, MouseEventArgs e)
{
_isDragging = false;
}
}
}
这个项目运作良好。唯一的区别是,在这里它是一个面板的图像是在顶部。
在具有UserControl的项目中,图像应显示在UserControl的顶部而不是其下方。
任何帮助将不胜感激。
我更改了图像示例的位置,使其超出UserControl的边界。
图像确实存在,但它在UserControl之下。只有超出UserControl边界的图像部分可见。
我需要的图像是在用户控件的顶部(在属于UserControl按钮的顶部),而不是在它下面。
2条答案
按热度按时间epggiuax1#
根据谜团的答案,我想出了这个程序。
它包含一个由4个边框面板、一个背景PictureBox和一个按钮组成的用户控件。
图像被放置在属于用户控件的按钮的顶部,并且图像还保留其透明背景。由于图像的大小小于按钮的大小,因此按钮在图像的外轮廓(而不是矩形)周围可见。
plupiseo2#
这是一个Windows窗体应用程序,可以做你想要的。它使用Microsoft Reactive Framework(NuGet System.Reactive.Windows.Forms来获取组件)。
当我运行时,我会显示这个:
然后我可以拖放: