Заливка области - C#
Формулировка задачи:
У меня на форме рисуется прямоугольник мышью. Так вот хотелось бы узнать как сделать, чтобы во время движения мыши я видел пунктирную границу будущего прямоугольника, и как закрасить его потом. Вот код кнопки с рисованием.
private void Form1_MouseUp(object sender, MouseEventArgs e) { Graphics g = this.CreateGraphics(); Pen pen = new Pen(Color.Red, 3); g.DrawRectangle(pen, spotClick.X, spotClick.Y, e.X - spotClick.X, e.Y - spotClick.Y); }
Решение задачи: «Заливка области»
textual
Листинг программы
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 Pictures2 { public partial class Form1 : Form { Boolean bHaveMouse; Point ptOriginal = new Point(); Point ptLast = new Point(); public Form1() { InitializeComponent(); } private void DrawLine() { } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { DrawLine(); } private void Form1_MouseClick(object sender, MouseEventArgs e) { } private void MyMouseDown(object sender, MouseEventArgs e) { bHaveMouse = true; // Store the "starting point" for this rubber-band rectangle. ptOriginal.X = e.X; ptOriginal.Y = e.Y; // Special value lets us know that no previous // rectangle needs to be erased. ptLast.X = -1; ptLast.Y = -1; } private void MyDrawReversibleRectangle(Point p1, Point p2) { Rectangle rc = new Rectangle(); // Convert the points to screen coordinates. p1 = PointToScreen(p1); p2 = PointToScreen(p2); // Normalize the rectangle. if (p1.X < p2.X) { rc.X = p1.X; rc.Width = p2.X - p1.X; } else { rc.X = p2.X; rc.Width = p1.X - p2.X; } if (p1.Y < p2.Y) { rc.Y = p1.Y; rc.Height = p2.Y - p1.Y; } else { rc.Y = p2.Y; rc.Height = p1.Y - p2.Y; } // Draw the reversible frame. ControlPaint.DrawReversibleFrame(rc, Color.Red, FrameStyle.Dashed); } private void MyMouseUp(object sender, MouseEventArgs e) { // Set internal flag to know we no longer "have the mouse". bHaveMouse = false; // If we have drawn previously, draw again in that spot // to remove the lines. if (ptLast.X != -1) { Point ptCurrent = new Point(e.X, e.Y); MyDrawReversibleRectangle(ptOriginal, ptLast); } // Set flags to know that there is no "previous" line to reverse. ptLast.X = -1; ptLast.Y = -1; ptOriginal.X = -1; ptOriginal.Y = -1; Graphics g = this.CreateGraphics(); Pen pen = new Pen(Color.BurlyWood,3); g.DrawRectangle(pen,ptOriginal.X,ptOriginal.Y,ptLast.X-ptOriginal.X,ptLast.Y-ptOriginal.Y); } public void MyMouseMove(Object sender, MouseEventArgs e) { Point ptCurrent = new Point(e.X, e.Y); // If we "have the mouse", then we draw our lines. if (bHaveMouse) { // If we have drawn previously, draw again in // that spot to remove the lines. if (ptLast.X != -1) { MyDrawReversibleRectangle(ptOriginal, ptLast); } // Update last point. ptLast = ptCurrent; // Draw new lines. MyDrawReversibleRectangle(ptOriginal, ptCurrent); } } protected override void OnLoad(System.EventArgs e) { MouseDown += new MouseEventHandler(MyMouseDown); MouseUp += new MouseEventHandler(MyMouseUp); MouseMove += new MouseEventHandler(MyMouseMove); bHaveMouse = false; } private void Form1_MouseUp(object sender, MouseEventArgs e) { } private void Form1_MouseDown(object sender, MouseEventArgs e) { } private void Form1_MouseMove(object sender, MouseEventArgs e) { } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д