MDICSharp案例总结:
1、 MDI窗体IsMdiContainer属性设置为true
2、 MainMenu、toolBarButton、ImageList工具控件等设计
顶级菜单MdiList属性设置为True,则显示所有子窗口及选中当前子窗口
ImageList需准备好图片
toolBar需先选择要用到的ImageList,然后设置Buttons属性,打开Buttons属性对话框,给每个toolBarButton选择ImageIndex
toolBar_Click单击函数:
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (toolBar1.Buttons.IndexOf(e.Button))
{
case 0 :
mnuFileNew_Click(null, null);
break;
case 1 :
mnuFileExit_Click(null,null);
break;
}
}
3、 文件:打开文件、保存文件,在这个地方是对应的两个操作
public void miOpen_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog.FileName;
currentFilename = filename;
Stream openStream = openFileDialog.OpenFile();
if (openStream != null)
{
BinaryFormatter formatter = new BinaryFormatter();
shapesArr = (ArrayList) formatter.Deserialize(openStream);
openStream.Close();
// allow shapes to be displayed
bDrawAllowed = true;
// reset shape type
currShape = ShapeType.cursor;
this.Refresh();
} // if().
} // if().
} // miOpen_Click().
public void miSave_Click(object sender, System.EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
String filename;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
filename = saveFileDialog.FileName;
}
if (filename == null) return;
else
{
Stream saveStream ;
saveStream = File.OpenWrite(filename);
if (saveStream != null)
{
BinaryFormatter formatter = new BinaryFormatter();
// serialize shapes
formatter.Serialize(saveStream, shapesArr);
saveStream.Close();
} // if().
} // else.
}
4、 对话框:MessageBox对话框、Color对话框
a.
MessageBox.Show(this, "by Shruti (C) 2003","About ...", MessageBoxButtons.OK, MessageBoxIcon.Information);
b.
MessageBox.Show(this,"No changes!", "Error: Empty Canvas",
MessageBoxButtons.OK, MessageBoxIcon.Error);
c.
MessageBox.Show(string question,"Save File?", MessageBoxButtons.YesNo) == DialogResult.Yes
ColorDialog:
Color color;
ColorDialog dlg = new ColorDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
color = dlg.Color;
m_pen = new Pen(color);
m_brush = new SolidBrush(color);
}
5、 DrawLine、DrawTriangle、DrawRectangle、DrawEllipse等画图形函数
***:
Rectangle drawArea;
Size rectSize = new Size((EndPoint.X - StartPoint.X), (EndPoint.Y - StartPoint.Y));
drawArea = new Rectangle(StartPoint, rectSize);
public override void DrawShape(Graphics dc, Pen drawPen, Brush brush)
{
//***
//Brush b = new SolidBrush(Color.Red);
dc.DrawLine(drawPen, StartPoint, EndPoint);
}
public override void DrawShape(Graphics dc, Pen drawPen, Brush brush)
{
***
Brush b = new SolidBrush(Color.Red);
dc.DrawRectangle(drawPen, drawArea);
dc.FillRectangle(brush, drawArea);
}
public override void DrawShape(Graphics dc, Pen drawPen, Brush brush)
{
***
dc.DrawEllipse(drawPen, drawArea);
dc.FillEllipse(brush, drawArea);
}
public override void DrawShape(Graphics dc, Pen drawPen, Brush brush)
{
***
Brush b = new SolidBrush(Color.Red);
Point[] pt;
pt = new Point[4];
pt[0].X = pt[3].X = EndPoint.X;
pt[0].Y = pt[3].Y = EndPoint.Y;
pt[1].X = StartPoint.X + rectSize.Width/2;
pt[1].Y = StartPoint.Y;
pt[2].X = StartPoint.X;
pt[2].Y = EndPoint.Y;
dc.DrawPolygon(drawPen, pt);
dc.FillPolygon(brush, pt);
}
6、 C#类库
using system;
using system.Drawing; —— 画图形
using system.IO; —— 流、文件
using system.Collections; —— 集合、Arraylist
using system.Windows.Forms;
using system.Runtime.Serialization.Formatters.Binary; —— BinaryFormatter