C 中GDI 程式設計10個基本技巧二

2021-07-02 13:26:58 字數 2645 閱讀 7196

5、漸變色填充

需要使用兩個刷子:

線性梯度刷子(lineargradientbrush)

路徑梯度刷子(pathguadientbrush)

private void button4_click(object sender,system.eventargs e) ;

//定義顏色漸變比率

float r = ;

colorblend blend = new colorblend();

blend.colors = colors;

blend.positions = r;

brush.interpolationcolors = blend;

//在橢圓外填充乙個矩形

g.fillrectangle(brush, 0, 0, 210, 110);

//用新增了橢圓的路徑定義第二個路徑梯度刷子

graphicspath gp2 = new graphicspath();

gp2.addellipse(300, 0, 200, 100);

pathgradientbrush brush2 = new pathgradientbrush(gp2);

//設定中心點位置和顏色

brush2.centerpoint = new pointf(450, 50);

brush2.centercolor = color.fromargb(0, 255, 0);

//設定邊界顏色

color color2 = ;

brush2.surroundcolors = color2;

//用第二個梯度刷填充橢圓

g.fillellipse(brush2, 300, 0, 200, 100);

}6、gdi+的座標系統

通用座標系——使用者自定義座標系。

頁面座標系——虛擬座標系。

裝置座標系——螢幕座標系。

當頁面座標系和裝置座標系的單位都是象素時,它們相同。

private void button10_click(object sender, system.eventargse)

private void draw(graphics g)

private void button5_click(object sender, system.eventargs e)

private void button6_click(object sender, system.eventargs e)

private void button7_click(object sender, system.eventargs e)

private void button8_click(object sender, system.eventargs e)

private void button9_click(object sender, system.eventargs e)

7、全域性座標——變換對於繪圖表面上的每個圖元都會產生影響。通常用於設定通用座標系。

下程式將原定點移動到控制項中心,並且y軸正向朝上。

//先畫乙個圓

graphics g = e.graphics;

g.fillrectangle(brushes.white, this.clientrectangle);

g.drawellipse(pens.black, -100, -100, 200, 200);

//使y軸正向朝上,必須做相對於x軸映象

//變換矩陣為[1,0,0,-1,0,0]

matrix mat = new matrix(1, 0, 0, -1, 0, 0);

g.transform = mat;

rectangle rect = this.clientrectangle;

int w = rect.width;

int h = rect.height;

g.translatetransform(w/2, -h/2);

//以原點為中心,做乙個半徑為100的圓

g.drawellipse(pens.red, -100, -100, 200, 200);

g.translatetransform(100, 100);

g.drawellipse(pens.green, -100, -100, 200, 200);

g.scaletransform(2, 2);

g.drawellipse(pens.blue, -100, -100, 200, 200);

8、區域性座標系——只對某些圖形進行變換,而其它圖形元素不變。

protected override void onpaint(painteventargs e)

9、alpha混合

color.fromargb()的a就是alpha。alpha的取值範圍從0到255。0表示完全透明,255完全不透明。

當前色=前景色×alpha/255+背景色×(255-alpha)/255

protected override void onpaint(painteventargs e)

10、反走樣

protected override void onpaint(painteventargs e)

private void draw(graphics g)

完了。暫時先總結那麼多。以後發現必要的可以再補充。

10個程式設計技巧

很長一段時間以來,我都在關注如何提高 質量,也為此做過一些嘗試,我想這個話題可能大家會比較感興趣,在這裡分享一下我關於如何提高 質量的一些體會。你知道怎麼寫高質量 嗎?不要一上來就開始寫 想清楚再動手,下面分享10個寫 的小技巧教你寫出高質量 1 重構思維模式 不要一上來就開始寫 要掌握盡量多的重構...

10個Python程式設計小技巧

2a,b b,a 實現了對兩個數的交換 a,b 2,1 name jack country china age 18 1.傳統的字串拼接 很繁雜 print hi,i m name i m from country and i m str age years old.2.百分號語法 print hi...

C 的4個基本技巧

1.如果可能盡量使用介面來程式設計 net框架包括類和介面,在編寫程式的時候,你可能知道正在用.net的哪個類。然而,在這種情況下如果你用.net支援的介面而不是它的類來程式設計時,會變得更加穩定 可用性會更高。請分析下面的 private void loadlist object items,li...