C 自定義控制項三Vista按鈕

2021-09-08 07:58:15 字數 2610 閱讀 9430

c#自定義控制項三vista按鈕

效果圖:

vista和win7作業系統的按鈕非常華麗,用c#gdi+去實現也挺方便的,這個是參考網上原始碼修改過來的,個人覺得非常有學習價值。

第一步:新增使用者控制項,命名為

myvistabutton

第三步:重寫paint事件,為了增強效果,還可以重寫滑鼠移入移出等事件。

貼出核心**:

#region方法

private

graphicspath roundrect(rectanglef r, float r1, float r2, float r3, float r4)

private

stringformat stringformatalignment(contentalignment textalign)

switch (textalign)

return sf;

}///

///繪製外部邊框

//////

private

void drawouterstroke(graphics g)

rectangle r = this.clientrectangle;

r.width -= 1; r.height -= 1;

using (graphicspath rr = roundrect(r, cornerradius, cornerradius, cornerradius, cornerradius)) }}

//////繪製內部邊框

//////

private

void drawinnerstroke(graphics g)

rectangle r = this.clientrectangle;

r.x++; r.y++;

r.width -= 3; r.height -= 3;

using (graphicspath rr = roundrect(r, cornerradius, cornerradius, cornerradius, cornerradius)) }}

//////繪製背景

//////

private

void drawbackground(graphics g)

int alpha = (mbuttonstate == state.pressed) ? 204 : 127;

rectangle r = this.clientrectangle;

r.width--; r.height--;

using (graphicspath rr = roundrect(r, cornerradius, cornerradius, cornerradius, cornerradius))

setclip(g);

if (this.backimage != null)

g.resetclip();

using (solidbrush sb = new

solidbrush(color.fromargb(alpha, this.buttoncolor))) }}

//////繪製高亮顏色

//////

private

void drawhighlight(graphics g)

int alpha = (mbuttonstate == state.pressed) ? 60 : 150;

rectangle rect = new

rectangle(0, 0, this.width, this.height / 2);

using (graphicspath r = roundrect(rect, cornerradius, cornerradius, 0, 0)) }}

//////繪製滑鼠移入顏色

//////

private

void drawglow(graphics g)

setclip(g);

using (graphicspath glow = new

graphicspath())

;g.fillpath(gl, glow);}}

g.resetclip();

}///

///繪製文字

//////

private

void drawtext(graphics g)

//////繪製

//////

private

void drawimage(graphics g)

rectangle r = new

rectangle(8, 8, this.imagesize.width, this.imagesize.height);

switch (this.imagealign)

g.drawimage(this.image, r);

}private

void setclip(graphics g)

}#endregion

重寫paint事件:

private

void myvistabutton_paint(object sender, painteventargs e)

Qt 控制項 自定義按鈕

mybutton.h ifndef mybutton h define mybutton h include include include include class mybutton public qwidget endif mybutton h mybutton.cpp include myb...

C 自定義控制項

做radiobutton button textbox的自定義控制項 先在工程分別新增3個類 然後分別在類裡面寫 diybutton.cs diytextbox.cs diyradiobutton.cs 寫好自定義控制項後就開始更改一些地方 窗體下有個窗體名.designer.cs開啟這個 檔案 更...

c 自定義控制項

第一步 新建乙個控制項庫專案 mycontrol 第二步 從工具箱裡面拖動1個picturebox 1個button 6個lable控制項到使用者介面上,布局如下 如上圖,設定picturebox的name為picbox,背景為白色,button的name為btnopen,另外靠左的三個lable的...