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