1. 程式人生 > >C#大學課程(第五版)課後習題15.11MDI 畫圖程序

C#大學課程(第五版)課後習題15.11MDI 畫圖程序

use eve RM gpo log checked parent () 運行程序

/*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;
}
}
}

C#大學課程(第五版)課後習題15.11MDI 畫圖程序