實(shí)體建模:C#畫圖的模式與縮放功能
在實(shí)體建模軟件中,經(jīng)常有設(shè)置并保存各種參考坐標(biāo)系的功能,方便建立模型。C#畫圖中也有這種類似功能。不過沒有建模軟件那么強(qiáng)大。實(shí)體建模軟件中,可以獨(dú)立的設(shè)置并保存各種坐標(biāo)系,并隨時(shí)調(diào)用。而這里只能以嵌套的形式調(diào)用,當(dāng)返回到上一級狀態(tài)時(shí),跳過的狀態(tài)就不再保存了。
C#畫圖普通模式主要命令:
- state = graphics.BeginContainer();
- 建一個(gè)新繪圖狀態(tài)
- e.Graphics.EndContainer(state1);
- 結(jié)束這個(gè)繪圖狀態(tài)
- Rectangle rect = new Rectangle(0,
- 0, 100, 100);//示例圖形
- GraphicsContainer state1 =
- e.Graphics.BeginContainer();
- //建一個(gè)新繪圖坐標(biāo)state1
- e.Graphics.TranslateTransform(100, 100);
- //移動(dòng)坐標(biāo)系到100,100,畫藍(lán)色矩形標(biāo)記
- e.Graphics.DrawRectangle(Pens.Blue, rect);
- GraphicsContainer state2 =
- e.Graphics.BeginContainer();
- //在此基礎(chǔ)上建一個(gè)繪圖坐標(biāo)state2
- e.Graphics.RotateTransform(45);//旋轉(zhuǎn)45度,
- 畫紅色矩形標(biāo)記
- e.Graphics.DrawRectangle(Pens.Red, rect);
- e.Graphics.TranslateTransform(100, 100);
- e.Graphics.DrawRectangle(Pens.Black, rect);
- e.Graphics.EndContainer(state2);//退出坐標(biāo)系2,
- 畫藍(lán)橢圓
- e.Graphics.DrawEllipse(Pens.Blue, rect);
- e.Graphics.EndContainer(state1);//退出state1,
- 畫紅橢圓
- e.Graphics.DrawRectangle(Pens.Red, rect);
建立狀態(tài)1
移動(dòng)到100,100,畫藍(lán)色矩形
建被嵌套的狀態(tài)2
移動(dòng)到200,0,畫紅色矩形
退出狀態(tài)2,畫藍(lán)色橢圓
退出狀態(tài)1,畫紅色矩形
狀態(tài)2是被嵌套的,如果直接退出狀態(tài)1畫紅色矩形,狀態(tài)2不再被保存。
graphics.BeginContainer()和EndGontainer是保存和返回當(dāng)前畫板狀態(tài),當(dāng)然,移動(dòng)只是一種改變畫板狀態(tài)的方式。
C#畫圖縮放功能主要命令:
- GraphicsContainercontainerState=
- e.Graphics.BeginContainer(
- destRect,srcRect,
- GraphicsUnit.Pixel);
- 多加兩個(gè)參數(shù),destRect和scrRect制定縮放大小
- Pixel指定單位
- RectanglesrcRect=newRectangle(
- 0,0,200,200);
- RectangledestRect=newRectangle(
- 200,200,100,100);
- //建一個(gè)比例縮放的畫圖板.
- GraphicsContainercontainerState=
- e.Graphics.BeginContainer(
- destRect,srcRect,
- GraphicsUnit.Pixel);
- //繪圖縮放綠矩形.
- e.Graphics.FillRectangle(
- newSolidBrush(Color.Red),0,0,100,100);
- //退出此繪圖板.
- e.Graphics.EndContainer(containerState);
- //繪原始紅矩形.
- e.Graphics.FillRectangle(
- newSolidBrush(Color.Green),0,0,100,100);
以上就介紹了C#畫圖的模式與縮放功能。
【編輯推薦】