/*15.11
(MDI 画图程序)创建一个MDI 程序,它的每个子窗口都有一个用于画图的面板。在这个MDI程序中添加菜单,使用户能够改变画刷的大小和颜色。当运行程序时,应确保当一个窗口遮盖了另一个时,应清除面板的内容。
*/
using System.Drawing;
using System.Windows.Forms;
namespace UsingMDI
{
public partial class DrawingForm : Form
{
bool shouldPaint = false;
int dotSize = 4;
public Color PaintBrushColor { get; set; }
public DrawingForm()
{
InitializeComponent();
PaintBrushColor = new Color();
}
public int PaintBrushSize
{
get
{
return dotSize;
}
set
{
if ( dotSize != 4 && dotSize != 8 && dotSize != 10 )
dotSize = 4;
else
dotSize = value;
}
}
private void drawingPanel_MouseDown(
object sender, MouseEventArgs e )
{
shouldPaint = true;
}
private void drawingPanel_MouseUp(
object sender, MouseEventArgs e )
{
shouldPaint = false;
}
private void drawingPanel_MouseMove(
object sender, MouseEventArgs e )
{
if ( shouldPaint )
{
Graphics graphics = drawingPanel.CreateGraphics();
graphics.FillEllipse( new SolidBrush( PaintBrushColor ),
e.X, e.Y, dotSize, dotSize );
}
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace UsingMDI
{
public partial class UsingMDIForm : Form
{
private DrawingForm childWindow;
private Color brushColor = Color.Red;
private int brushSize = 4;
public UsingMDIForm()
{
InitializeComponent();
}
private void newMenuItem_Click( object sender, EventArgs e )
{
childWindow = new DrawingForm();
childWindow.PaintBrushColor = brushColor;
childWindow.PaintBrushSize = brushSize;
childWindow.MdiParent = this;
childWindow.Show();
}
private void exitMenuItem_Click( object sender, EventArgs e )
{
Application.Exit();
}
private void colorMenuItems_Click( object sender, EventArgs e )
{
brushColor = Color.FromName( sender.ToString() );
foreach ( var child in this.MdiChildren )
{
if ( child is DrawingForm )
( ( DrawingForm ) child ).PaintBrushColor = brushColor;
}
redMenuItem.Checked = false;
blueMenuItem.Checked = false;
greenMenuItem.Checked = false;
blackMenuItem.Checked = false;
( ( ToolStripMenuItem ) sender).Checked = true;
}
private void sizeMenuItems_Click( object sender, EventArgs e )
{
switch ( sender.ToString() )
{
case "Small":
brushSize = 4;
break;
case "Medium":
brushSize = 8;
break;
case "Large":
brushSize = 12;
break;
}
foreach ( var child in this.MdiChildren )
{
if ( child is DrawingForm )
( ( DrawingForm ) child ).PaintBrushSize = brushSize;
}
smallMenuItem.Checked = false;
mediumMenuItem.Checked = false;
largeMenuItem.Checked = false;
( ( ToolStripMenuItem ) sender ).Checked = true;
}
}
}
原文地址:https://www.cnblogs.com/hsyv123ve/p/8734174.html