이상적으로는 layered form을 사용하고 싶었지만 표준 양식을 사용하면 여기에 쓰거나 작성하는 것이 훨씬 쉬워졌습니다. StackOverflow.
UX는별로 멀지 않지만 그다지 중요하지 않습니다. 일반적인 생각을 말하면됩니다. 자신 만의 인터페이스를 자유롭게 채택해야합니다.
public sealed partial class GafferTape : Form
{
private Point _startLocation = Point.Empty;
private Point _cursorLocation = Point.Empty;
private bool _drawing;
private Rectangle _regionRectangle;
private readonly List<Rectangle> _rectangles = new List<Rectangle>();
public bool AllowDrawing { get; set; }
public GafferTape()
{
InitializeComponent();
//TODO: Consider letting the designer handle this.
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
DoubleBuffered = true;
ShowInTaskbar = false;
Cursor = Cursors.Cross;
BackColor = Color.White;
Opacity = 0.4;
TransparencyKey = Color.Black;
//TODO: Consider letting the designer handle this.
MouseDown += OnMouseDown;
MouseUp += OnMouseUp;
MouseMove += OnMouseMove;
Paint += OnPaint;
AllowDrawing = true;
}
private void OnMouseDown(object sender, MouseEventArgs mouseEventArgs)
{
//I don't allow the user to draw after the rectangles have been drawn. See: buttonInvert_Clic
if (AllowDrawing)
{
_startLocation = mouseEventArgs.Location;
_drawing = true;
}
}
private void OnMouseUp(object sender, MouseEventArgs mouseEventArgs)
{
_drawing = false;
DialogResult = DialogResult.OK;
_rectangles.Add(_regionRectangle);
}
private void OnMouseMove(object sender, MouseEventArgs mouseEventArgs)
{
if (_drawing == false)
return;
_cursorLocation = mouseEventArgs.Location;
_regionRectangle = new Rectangle(Math.Min(_startLocation.X, _cursorLocation.X),
Math.Min(_startLocation.Y, _cursorLocation.Y),
Math.Abs(_startLocation.X - _cursorLocation.X),
Math.Abs(_startLocation.Y - _cursorLocation.Y));
Invalidate();
}
private void OnPaint(object sender, PaintEventArgs paintEventArgs)
{
foreach (Rectangle rectangle in _rectangles)
paintEventArgs.Graphics.FillRectangle(Brushes.Black, rectangle);
paintEventArgs.Graphics.FillRectangle(Brushes.Black, _regionRectangle);
}
private void buttonInvert_Click(object sender, EventArgs e)
{
Opacity = 100;
TransparencyKey = Color.White;
AllowDrawing = false;
Cursor = Cursors.Default;
}
}
정말 멋지다! 단 한가지는 버튼이 그려지는 곳을 볼 수 없다는 것입니다. – Ben
저는 디자이너를 사용하여 버튼을 배치하고'Anchor' 속성'Top, Left'을 사용했습니다. 미안하다 : C –
Spot on! 이 건배 - 꼭 필요한 것. – Ben