国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

winform異型不規則界面設計的實現方法

2020-01-24 02:28:09
字體:
來源:轉載
供稿:網友

本文實例講述了winform異型不規則界面設計的實現方法,用于界面設計時有不錯的用戶體驗,非常實用。分享給大家供大家參考之用。具體方法如下:

一、不規則WINFORM窗體

Author:unknown From:Internet
在以前版本的Visual Basic或Visual C++中,創建不規則窗體和控件是一件很復雜的事,不僅需要調用大量API函數而且工作量也不小。不過,現在在Visual C#下,情況就完全不同了。運用Windows Forms你就可以很輕易地創建出一個不規則的窗體以及窗體上的控件。一個具有不規則窗體和控件的應用程序肯定會更吸引廣大的用戶,微軟的Windows Media Player 7就顯示出這一點。作為程序員,您一定想在自己的程序中運用這點技術吧。

程序的窗體和控件都可以以非傳統的方式被創建。本文就向大家展示如何在應用程序中創建不規則窗體,以及如何在窗體上創建各式各樣的自定義形狀的控件。

注:創建不規則窗體和控件這個過程包含了大量的圖形編程工作,所以不同的計算機因內存和顯卡的不同可能會導致最終的效果有所不同。因此,在發布你的應用程序前,務必在各種不同類型的計算機上做好測試工作。

實現方法:

首先,創建一個位圖文件作為程序的窗體。位圖可以是任意形狀的,但是位圖文件區域一定要足夠大,這樣才能包含窗體上的所有控件。然后,你可以通過設置一些屬性使該圖成為程序的窗體。

把程序中的標題欄去掉,否則整個界面將顯得很不協調。當然你去掉了標題欄也就去掉了它的最大化、最小化、關閉、移動窗體等功能。為了使程序仍然具有這些功能,我們需在程序中添加一些代碼,這樣用戶就仍然可以像以前一樣和程序進行交互。

因此,你需要完成如下工作:

1.創建一個作為窗體的位圖文件。

2.創建一個Windows應用程序,用上述位圖文件作為程序的窗體同時去掉其標題欄。

3.添加原標題欄具有的功能所需的代碼。

具體步驟:

下面我就具體向大家介紹如何創建不規則窗體。

創建一個具有不規則形狀的位圖文件

1.用任何畫圖程序就可以創建不規則形狀的位圖,你可以使用最容易也是最方便的畫圖程序。

2.用一種顏色畫出一個不規則的區域作為程序的窗體,并用另一種顏色畫出該位圖的背景。(你要使該不規則區域足夠大。)

3.保存位圖文件。

下面就是一個例子:

在vs.net中創建一個新的工程:

首先,設置窗體的背景從而建立窗體形狀。

1.在窗體設計器中選中窗體使之獲得焦點。

2.在屬性對話框中進行如下設置:

●將FormBorderStyle屬性設置為None。該屬性去掉了程序的標題欄,同時也除去了標題欄的功能,不過我在后面還會向大家介紹如何添加代碼以恢復這些功能的。

●將BackgroundImage屬性設置為你創建的位圖文件。你不必在工程中添加該文件,因為你一旦指定了該文件,它就會自動被添加到工程中。

●將TransparencyKey屬性設置為位圖文件的背景顏色值(在本例中是藍色)。該屬性使得位圖的背景即上圖中的藍色部分不可見,從而窗體就呈現出一個不規則的橢圓形。

3.保存工程。按Ctrl+F5可以運行此程序。(注:因為沒有標題欄,所以你可以通過Alt+F4來關閉程序)

將FormBorderStyle屬性設置為None后,程序的標題欄就被去掉了。這樣,為了獲得原來標題欄的功能,我們必須手動添加代碼。下面我就向大家介紹如何添加代碼實現關閉功能以及移動窗體的功能。

實現窗體的關閉及移動:

1.往窗體上拖放一個按鈕控件。

2.在屬性對話框中,將該控件的Text屬性設置為“關閉”。

3.雙擊按鈕添加一個Click事件處理函數。

4.在代碼編輯器中添加如下代碼:

private void button1_Click(object sender, System.EventArgs e) {   this.Close(); } 

二、不規則按鈕Author:unknown From:Internet現在,我們已經創建了一個不規則的窗體,并實現了一些基本的移動窗體、關閉窗體的功能。然而,窗體上的按鈕控件還是老一套,那么方方正正,使得整個界面不美觀。接下來我就向大家介紹如何創建自定義形狀的控件。 前面我們創建不規則窗體的時候用到了TransparencyKey屬性,但是控件是沒有該屬性的,所以我們得找其他的方法來實現控件的不規則形狀了。在窗體上畫一個自定義形狀的控件時,你需要精確的告知窗體在什么位置以及如何畫該控件。在.Net Framework中有相應的類和方法來幫你實現這些,所以你不必擔心具體實現。 .Net Framework中的類提供給控件一個指示說明,該指示說明能確定控件被畫的形狀。通過不同的指示說明,你就可以按你想要的方法來畫控件了。該指示說明利用了GraphicsPath這個類,這個類代表了一系列用來畫圖的直線和曲線。首先,你得指定一個GraphicsPath類的對象并告知它你要畫什么圖形。然后,你將控件的Region屬性設置為上述GraphicsPath類的對象。這樣,你就可以創建任何自定義形狀的控件了。

步驟如下:

● 創建一個GraphicsPath類的實例對象。

● 指定好該對象的各項細節(如大小、形狀等等)。

● 將控件的Region屬性設置為上面建立的GraphicsPath類的實例對象。 創建一個像文本的按鈕控件:

1.拖放一個按鈕控件到窗體上。

2.在屬性對話框中進行如下設置:

● 將Name屬性設置為CustomButton。

● 將BackColor屬性設置為一個和窗體背景顏色不同的顏色值。

● 將其Text屬性設置為空字符串。

3.添加窗體的Paint事件的事件處理函數。

4.添加以下代碼,用GraphicsPath類的實例對象來畫控件。

下面的代碼以一串字符串的形式畫該按鈕控件,同時,程序還設置了字符串的字體、大小、風格等屬性。字符串被賦給GraphicsPath類的實例對象。然后,該實例對象就被設置為按鈕控件的Region屬性。這樣一個自定義形狀的控件就完成了。 

private void CustomButton_Paint( object sender, System.Windows.Forms.PaintEventArgs e ) {   //初始化一個GraphicsPath類的對象   System.Drawing.Drawing2D.GraphicsPath myGraphicsPath = new System.Drawing.Drawing2D.GraphicsPath();   //確定一個字符串,該字符串就是控件的形狀   string stringText = "Click Me!";   //確定字符串的字體   FontFamily family = new FontFamily("Arial");   //確定字符串的風格   int fontStyle = (int)FontStyle.Bold;   //確定字符串的高度   int emSize = 35;   //確定字符串的起始位置,它是從控件開始計算而非窗體   PointF origin = new PointF(0, 0);   //一個StringFormat對象來確定字符串的字間距以及對齊方式   StringFormat format = new StringFormat(StringFormat.GenericDefault);   //用AddString方法創建字符串   myGraphicsPath.AddString(stringText, family, fontStyle, emSize, origin, format);   //將控件的Region屬性設置為上面創建的GraphicsPath對象   CustomButton.Region = new Region(myGraphicsPath); } 

三、GDI+編程的10個基本技巧

//創建繪圖表面有兩種常用的方法。下面設法得到PictureBox的繪圖表面。  private void Form1_Load(object sender, System.EventArgs e)  {  //得到pictureBox1的繪圖表面  Graphics g = this.pictureBox1.CreateGraphics();  }  private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)  {  //得到pictureBox1的繪圖表面  Graphics g = e.Graphics;  }  //可以利用Graphics對象繪制出各種圖形圖案。控件的Paint事件和OnPaint方法都可以繪圖都是好時機。在OnPaint方法里繪制圖案一定從參數e里面得到Graphics屬性。下面是兩個例子。  protected override void OnPaint(PaintEventArgs e)  {  e.Graphics.Clear(Color.White);  float x, y, w, h;  x = this.Left+2;  y = this.Top+2;  w = this.Width-4;  h = this.Height-4;  Pen pen = new Pen(Color.Red, 2);  e.Graphics.DrawRectangle(pen, x, y, w, h);  base.OnPaint (e);  }  private void PictureBoxII_Resize(object sender, EventArgs e)  {  this.Invalidate();  }  private void button1_Click(object sender, System.EventArgs e)  {  this.pictureBoxII1.CreateGraphics().FillEllipse(  Brushes.Blue, 10, 20, 50, 100);  }  //和文本有關的三個類:  //FontFamily――定義有著相似的基本設計但在形式上有某些差異的一組字樣。無法繼承此類。  //Font――定義特定的文本格式,包括字體、字號和字形屬性。無法繼承此類。  //StringFormat――封裝文本布局信息(如對齊方式和行距),顯示操作(如省略號插入和國家標準 (National) 數字位替換)和 OpenType 功能。無法繼承此類。  //下面的程序顯示了一段文字。  private void button2_Click(object sender, System.EventArgs e)  {  Graphics g = this.pictureBoxII1.CreateGraphics();  g.FillRectangle(Brushes.White, this.pictureBoxII1.ClientRectangle);  string s = "aaaaaaaaaaaaaaaaaaaaaaaaaa";  FontFamily fm = new FontFamily("ËÎÌå");  Font f = new Font(fm, 20, FontStyle.Bold, GraphicsUnit.Point);  RectangleF rectF = new RectangleF(30, 20, 180, 205);  StringFormat sf = new StringFormat();  SolidBrush sbrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));  sf.LineAlignment = StringAlignment.Center;  sf.FormatFlags = StringFormatFlags.DirectionVertical;  g.DrawString(s, f, sbrush, rectF, sf);  }  //GDI+的路徑――GraphicsPath類  //GraphicsPath類提供了一系列屬性和方法,利用它可以獲取路徑上的關鍵點,可以添加直線段、圓等幾何元素。可以獲得包圍矩形,進行拾取測試。這些功能都怎么用,要仔細看一下。  private void button3_Click(object sender, System.EventArgs e)  {  //繪圖表面  Graphics g = this.pictureBoxII1.CreateGraphics();  //填充成白色  g.FillRectangle(Brushes.White, this.ClientRectangle);  //弄一個繪圖路徑¶  GraphicsPath gp = new GraphicsPath();  //添加一些集合圖形  gp.AddEllipse(20, 20, 300, 200);  gp.AddPie(50, 100, 300, 100, 45, 200);  gp.AddRectangle(new Rectangle(100, 30, 100, 80));  //在繪圖表面上繪制繪圖路徑  g.DrawPath(Pens.Blue, gp);  //平移  g.TranslateTransform(200, 20);  //填充繪圖路徑¶  g.FillPath(Brushes.GreenYellow, gp);  gp.Dispose();  }  //區域――Region類  //從已有的矩形和路徑可以創建Region。使用Graphics.FillRegion方法繪制Region。該類指示由矩形和由路徑構成的圖形形狀的內部。無法繼承此類。  //漸變色填充  //需要使用兩個刷子:  //線性梯度刷子(LinearGradientBrush)  //路徑梯度刷子(PathGuadientBrush)  private void button4_Click(object sender, System.EventArgs e)  {  //繪圖表面  Graphics g = this.pictureBoxII1.CreateGraphics();  g.FillRectangle(Brushes.White, this.pictureBoxII1.ClientRectangle);  //定義一個線性梯度刷子  LinearGradientBrush lgbrush =  new LinearGradientBrush(  new Point(0, 10),  new Point(150, 10),  Color.FromArgb(255, 0, 0),  Color.FromArgb(0, 255, 0));  Pen pen = new Pen(lgbrush);  //用線性筆刷梯度效果的筆繪制一條直線段并填充一個矩形  g.DrawLine(pen, 10, 130, 500, 130);  g.FillRectangle(lgbrush, 10, 150, 370, 30);  //定義路徑并添加一個橢圓  GraphicsPath gp = new GraphicsPath();  gp.AddEllipse(10, 10, 200, 100);  //用該路徑定義路徑梯度刷子  PathGradientBrush brush =  new PathGradientBrush(gp);  //顏色數組  Color[] colors = {  Color.FromArgb(255, 0, 0),  Color.FromArgb(100, 100, 100),  Color.FromArgb(0, 255, 0),  Color.FromArgb(0, 0, 255)};  //定義顏色漸變比率  float[] r = {0.0f, 0.3f, 0.6f, 1.0f};  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 = {Color.FromArgb(255, 0, 0)};  brush2.SurroundColors = color2;  //用第二個梯度刷填充橢圓  g.FillEllipse(brush2, 300, 0, 200, 100);  }  //GDI+的坐標系統  //通用坐標系――用戶自定義坐標系。  //頁面坐標系――虛擬坐標系。  //設備坐標系――屏幕坐標系。  //當頁面坐標系和設備坐標系的單位都是象素時,它們相同。  private void button10_Click(object sender, System.EventArgs e)  {  Graphics g = this.pictureBoxII1.CreateGraphics();  g.Clear(Color.White);  this.Draw(g);  }  private void Draw(Graphics g)  {  g.DrawLine(Pens.Black, 10, 10, 100, 100);  g.DrawEllipse(Pens.Black, 50, 50, 200, 100);  g.DrawArc(Pens.Black, 100, 10, 100, 100, 20, 160);  g.DrawRectangle(Pens.Green, 50, 200, 150, 100);  }  private void button5_Click(object sender, System.EventArgs e)  {  //左移  Graphics g = this.pictureBoxII1.CreateGraphics();  g.Clear(Color.White);  g.TranslateTransform(-50, 0);  this.Draw(g);  }  private void button6_Click(object sender, System.EventArgs e)  {  //右移  Graphics g = this.pictureBoxII1.CreateGraphics();  g.Clear(Color.White);  g.TranslateTransform(50, 0);  this.Draw(g);  }  private void button7_Click(object sender, System.EventArgs e)  {  //旋轉  Graphics g = this.pictureBoxII1.CreateGraphics();  g.Clear(Color.White);  g.RotateTransform(-30);  this.Draw(g);  }  private void button8_Click(object sender, System.EventArgs e)  {  //放大  Graphics g = this.pictureBoxII1.CreateGraphics();  g.Clear(Color.White);  g.ScaleTransform(1.2f, 1.2f);  this.Draw(g);  }  private void button9_Click(object sender, System.EventArgs e)  {  //縮小  Graphics g = this.pictureBoxII1.CreateGraphics();  g.Clear(Color.White);  g.ScaleTransform(0.8f, 0.8f);  this.Draw(g);  }  //全局坐標――變換對于繪圖表面上的每個圖元都會產生影響。通常用于設定通用坐標系。  //一下程序將原定移動到控件中心,并且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);  //局部坐標系――只對某些圖形進行變換,而其它圖形元素不變。  protected override void OnPaint(PaintEventArgs e)  {  Graphics g = e.Graphics;  //客戶區設置為白色  g.FillRectangle(Brushes.White, this.ClientRectangle);  //y軸朝上  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);  //在全局坐標下繪制橢圓  g.DrawEllipse(Pens.Red, -100, -100, 200, 200);  g.FillRectangle(Brushes.Black, -108, 0, 8, 8);  g.FillRectangle(Brushes.Black, 100, 0, 8, 8);  g.FillRectangle(Brushes.Black, 0, 100, 8, 8);  g.FillRectangle(Brushes.Black, 0, -108, 8, 8);  //創建一個橢圓然后在局部坐標系中進行變換  GraphicsPath gp = new GraphicsPath();  gp.AddEllipse(-100, -100, 200, 200);  Matrix mat2 = new Matrix();  //平移  mat2.Translate(150, 150);  //旋轉  mat2.Rotate(30);  gp.Transform(mat2);  g.DrawPath(Pens.Blue, gp);  PointF[] p = gp.PathPoints;  g.FillRectangle(Brushes.Black, p[0].X-2, p[0].Y+2, 4, 4);  g.FillRectangle(Brushes.Black, p[3].X-2, p[3].Y+2, 4, 4);  g.FillRectangle(Brushes.Black, p[6].X-4, p[6].Y-4, 4, 4);  g.FillRectangle(Brushes.Black, p[9].X-4, p[9].Y-4, 4, 4);  gp.Dispose();  //base.OnPaint (e);  }  //Alpha混合 //Color.FromArgb()的A就是Alpha。Alpha的取值范圍從0到255。0表示完全透明,255完全不透明。  //當前色=前景色×alpha/255+背景色×(255-alpha)/255  protected override void OnPaint(PaintEventArgs e)  {  Graphics g = e.Graphics;  //創建一個填充矩形  SolidBrush brush = new SolidBrush(Color.BlueViolet);  g.FillRectangle(brush, 180, 70, 200, 150);  //創建一個位圖,其中兩個位圖之間有透明效果  Bitmap bm1 = new Bitmap(200, 100);  Graphics bg1 = Graphics.FromImage(bm1);  SolidBrush redBrush =  new SolidBrush(Color.FromArgb(210, 255, 0, 0));  SolidBrush greenBrush =  new SolidBrush(Color.FromArgb(210, 0, 255, 0));  bg1.FillRectangle(redBrush, 0, 0, 150, 70);  bg1.FillRectangle(greenBrush, 30, 30, 150, 70);  g.DrawImage(bm1, 100, 100);  //創建一個位圖,其中兩個位圖之間沒有透明效果  Bitmap bm2 = new Bitmap(200, 100);  Graphics bg2 = Graphics.FromImage(bm2);  bg2.CompositingMode = CompositingMode.SourceCopy;  bg2.FillRectangle(redBrush, 0, 0, 150, 170);  bg2.FillRectangle(greenBrush, 30, 30, 150, 70);  g.CompositingQuality = CompositingQuality.GammaCorrected;  g.DrawImage(bm2, 300, 200);  //base.OnPaint (e);  }  //反走樣  protected override void OnPaint(PaintEventArgs e)  {  Graphics g = e.Graphics;  //放大8倍  g.ScaleTransform(8, 8);  //沒有反走樣的圖形和文字  Draw(g);  //設置反走樣  g.SmoothingMode = SmoothingMode.AntiAlias;  //右移40  g.TranslateTransform(40, 0);  //再繪制就是反走樣之后的了  Draw(g);  //base.OnPaint (e);  }  private void Draw(Graphics g)  {  //繪制圖形和文字  g.DrawLine(Pens.Gray, 10, 10, 40, 20);  g.DrawEllipse(Pens.Gray, 20, 20, 30, 10);  string s = "反走樣測試";  Font font = new Font("宋體", 5);  SolidBrush brush = new SolidBrush(Color.Gray);  g.DrawString(s, font, brush, 10, 40);  }

相信本文所述對大家的C#程序設計有一定的借鑒價值。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久治县| 榆社县| 盐亭县| 鹤山市| 韩城市| 宁晋县| 绥棱县| 桂平市| 屯留县| 县级市| 宝清县| 高安市| 孝义市| 新晃| 宝丰县| 德保县| 正定县| 和硕县| 扬州市| 南岸区| 鄂伦春自治旗| 宜都市| 饶阳县| 遂溪县| 益阳市| 堆龙德庆县| 黎城县| 龙胜| 平安县| 黎川县| 普宁市| 临武县| 密云县| 新宁县| 罗山县| 柞水县| 马鞍山市| 宁陕县| 厦门市| 洪泽县| 大庆市|