實現過程:
(1) 新建窗體應用程式
(2) 新增一個MenuScrip控制元件;新增一個ToolScrip控制元件。
在ToolScrip控制元件中對每個單元,要將DisplayStyle屬性改為Text
(3)程式程式碼。
1、新建選單事件主要用白色清除窗體的背景,從而實現“檔案新建”功能
[csharp]
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(backColor);
toolStrip1.Enabled = true;
//建立一個Bitmap
theImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
editFileName = "新建檔案";
//修改視窗標題
this.Text = "MyDraw\t" + editFileName;
ig = Graphics.FromImage(theImage);
ig.Clear(backColor);
}
2、開啟事件用於開啟“開啟檔案”對話方塊,並選擇相應的圖片,將圖片繪製到窗體上.
private void 開啟ToolStripMenuItem_Click(object sender, EventArgs e)
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
this.Text = "MyDraw\t" + openFileDialog1.FileName;
editFileName = openFileDialog1.FileName;
theImage = Image.FromFile(openFileDialog1.FileName);
g.DrawImage(theImage, this.ClientRectangle);
ig.DrawImage(theImage, this.ClientRectangle);
//ToolBar可以使用了
(3) 儲存選單項的Click事件用於將窗體背景儲存為BMP格式的圖片
private void 儲存ToolStripMenuItem_Click(object sender, EventArgs e)
saveFileDialog1.Filter = "影象(*.bmp)|*.bmp";
saveFileDialog1.FileName = editFileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
theImage.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
this.Text = "MyDraw\t" + saveFileDialog1.FileName;
editFileName = saveFileDialog1.FileName;
(4) 在Paint事件中將Image中儲存的影象,繪製出來
private void Form1_Paint(object sender, PaintEventArgs e)
//將Image中儲存的影象,繪製出來
if (theImage != null)
g.Clear(Color.White);
(5)新增Frm_Text.cs文字輸入框。
新增一個Window窗體,取名為Frm_Text,然後對窗體的屬性修改:
把FormBorderStyle屬性改為 None;
把Modifiers的屬性改為 Public
(6) 在窗體的MouseDown事件中,如果當前繪製的是字串,在滑鼠的當前位置顯示文字框;如果繪製的是圖開,設定圖形的起始位置。
[cpp]
private void Frm_Main_MouseDown(object sender, MouseEventArgs e)
if (e.Button == MouseButtons.Left)
//如果選擇文字輸入,則開啟strInput窗體
if (drawTool == drawTools.String)
Frm_Text inputBox = new Frm_Text();
inputBox.StartPosition = FormStartPosition.CenterParent;
if (inputBox.ShowDialog() == DialogResult.OK)
Font theFont = this.Font;
g.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
ig.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
//如果開始繪製,則開始記錄滑鼠位置
else if ((isDrawing = !isDrawing) == true)
startPoint = new Point(e.X, e.Y);
oldPoint = new Point(e.X, e.Y);
實現過程:
(1) 新建窗體應用程式
(2) 新增一個MenuScrip控制元件;新增一個ToolScrip控制元件。
在ToolScrip控制元件中對每個單元,要將DisplayStyle屬性改為Text
(3)程式程式碼。
1、新建選單事件主要用白色清除窗體的背景,從而實現“檔案新建”功能
[csharp]
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(backColor);
toolStrip1.Enabled = true;
//建立一個Bitmap
theImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
editFileName = "新建檔案";
//修改視窗標題
this.Text = "MyDraw\t" + editFileName;
ig = Graphics.FromImage(theImage);
ig.Clear(backColor);
}
2、開啟事件用於開啟“開啟檔案”對話方塊,並選擇相應的圖片,將圖片繪製到窗體上.
[csharp]
private void 開啟ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//修改視窗標題
this.Text = "MyDraw\t" + openFileDialog1.FileName;
editFileName = openFileDialog1.FileName;
theImage = Image.FromFile(openFileDialog1.FileName);
Graphics g = this.CreateGraphics();
g.DrawImage(theImage, this.ClientRectangle);
ig = Graphics.FromImage(theImage);
ig.DrawImage(theImage, this.ClientRectangle);
//ToolBar可以使用了
toolStrip1.Enabled = true;
}
}
(3) 儲存選單項的Click事件用於將窗體背景儲存為BMP格式的圖片
[csharp]
private void 儲存ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "影象(*.bmp)|*.bmp";
saveFileDialog1.FileName = editFileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
theImage.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
this.Text = "MyDraw\t" + saveFileDialog1.FileName;
editFileName = saveFileDialog1.FileName;
}
}
(4) 在Paint事件中將Image中儲存的影象,繪製出來
[csharp]
private void Form1_Paint(object sender, PaintEventArgs e)
{
//將Image中儲存的影象,繪製出來
Graphics g = this.CreateGraphics();
if (theImage != null)
{
g.Clear(Color.White);
g.DrawImage(theImage, this.ClientRectangle);
}
}
(5)新增Frm_Text.cs文字輸入框。
新增一個Window窗體,取名為Frm_Text,然後對窗體的屬性修改:
把FormBorderStyle屬性改為 None;
把Modifiers的屬性改為 Public
(6) 在窗體的MouseDown事件中,如果當前繪製的是字串,在滑鼠的當前位置顯示文字框;如果繪製的是圖開,設定圖形的起始位置。
[cpp]
private void Frm_Main_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//如果選擇文字輸入,則開啟strInput窗體
if (drawTool == drawTools.String)
{
Frm_Text inputBox = new Frm_Text();
inputBox.StartPosition = FormStartPosition.CenterParent;
if (inputBox.ShowDialog() == DialogResult.OK)
{
Graphics g = this.CreateGraphics();
Font theFont = this.Font;
g.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
ig.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
}
}
//如果開始繪製,則開始記錄滑鼠位置
else if ((isDrawing = !isDrawing) == true)
{
startPoint = new Point(e.X, e.Y);
oldPoint = new Point(e.X, e.Y);
}
}
}