實現自定義Windows窗體樣式,酷!

 

使用C#(在VS2005下)實現自定義的Windwos窗體是多麼簡單,發揮你的想像,你也可以做出像QQ那樣外觀的應用程序。

1。首先要在PS等圖形處理軟件下做好自己的樣式,如下圖所示:

 

注意:圖片格式爲32位的Bmp格式,且要透明的區域用單色,如純紅,透明區域最好是能對稱,否則效果很差。

2。實現代碼如下: 

/// <summary>
    
/// 創建自定義窗體或控件
    
/// Wendy,Bear據網上資料蒐集整理優化
    
/// </summary>
    public class BitmapRegion
    {
        
public BitmapRegion()
        { }


        
/// <summary>        
        
/// 創建支持位圖區域的控件(目前有button和form) 
        
/// </summary>  
        
/// <param name="control">控件</param>  
        
/// <param name="bitmap">位圖</param>  
        public static void CreateControlRegion(Control control, Bitmap bitmap)
        {
            
            
//判斷是否存在控件和位圖 
            if (control == null || bitmap == null)
                
return;
           
            
//設置控件大小爲位圖大小 
            control.Width = bitmap.Width;
            control.Height 
= bitmap.Height;
            
            
//當控件是form時 
            if (control is System.Windows.Forms.Form)
            {
              
                
//強制轉換爲FORM 
                Form form = (Form)control;
         
                
//當FORM的邊界FormBorderStyle不爲NONE時,應將FORM的大小設置成比位圖大小稍大一點 
                form.Width = control.Width;
                form.Height 
= control.Height;
                
                
//沒有邊界 
                form.FormBorderStyle = FormBorderStyle.None;
               
                
//將位圖設置成窗體背景圖片 
                form.BackgroundImage = bitmap;
                
//
                
//將背景圖片設爲居中
                
//董,2006.3.28
                form.BackgroundImageLayout = ImageLayout.Center;
               
                
//計算位圖中不透明部分的邊界 
                GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);                
                
                
//應用新的區域                
                form.Region = new Region(graphicsPath);
               
            }
            
            
//當控件是button時 
            else if (control is System.Windows.Forms.Button)
            {
                
                
//強制轉換爲 button 
                Button button = (Button)control;
                 
                
//不顯示button text 
                button.Text = "";

               
                
//改變 cursor的style 
                button.Cursor = Cursors.Hand;
                
                
//設置button的背景圖片 
                button.BackgroundImage = bitmap;

                
                
//計算位圖中不透明部分的邊界 
                GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
                 
                
//應用新的區域 
                button.Region = new Region(graphicsPath);

            }
        }
        
/// <summary>         
        
/// 計算位圖中不透明部分的邊界 
        
/// 注意:此方法只適用規則的四角對稱的圖形
        
/// </summary>  
        
/// <param name="bitmap">要繪製區域的位圖</param>  
        
/// <returns>計算後的圖形路徑</returns>  
        private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
        {

            
//創建 GraphicsPath 
            GraphicsPath graphicsPath = new GraphicsPath();

            
//使用左上角的一點的顏色作爲我們透明色 
            Color colorTransparent = bitmap.GetPixel(00);

            
//第一個找到點的X 
            int colOpaquePixel = 0;

            
// 遍歷所有行(Y方向) 
            for (int row = 0; row < bitmap.Height;)
            {
                
// Reset value  
                
//重設 
                colOpaquePixel = 0;
               
                
//遍歷所有列(X方向) 
                for (int col = 0; col < bitmap.Width; col++)
                {

                    
//如果是不需要透明處理的點則標記,然後繼續遍歷 
                    if (bitmap.GetPixel(col, row) != colorTransparent)
                    {                        
                        
//記錄當前 
                        colOpaquePixel = col;

                        
//記錄右邊界
                        int colNext = bitmap.Width - col;

                        
//將不透明點加到graphics path 
                        if (col == 0)
                        {
                            
//將中間大部分不透明區域加入
                            graphicsPath.AddRectangle(new Rectangle(colOpaquePixel+1, row, colNext - colOpaquePixel-2, bitmap.Height - row - row-1));

                            
//置row爲最大以便跳出循環
                            row = bitmap.Height;
                        }
                        
else
                        {
                            
//頂部區域單行加入
                            graphicsPath.AddRectangle(new Rectangle(colOpaquePixel+1, row, colNext - colOpaquePixel-21));

                            
//底部區域單行加入
                            graphicsPath.AddRectangle(new Rectangle(colOpaquePixel+1, bitmap.Height-row-2, colNext - colOpaquePixel - 21));
                            row
++;
                        }

                        
//跳出循環繼續遍歷下一列
                        break;

                    }
                }
            }
            
            
//返回
            return graphicsPath;
        }

    }
//end class

 

3.調用代碼如下:

//繪製自定義窗體
//Resources.LoginForm爲資源,也就是做好的圖片
  BitmapRegion.CreateControlRegion(thisnew Bitmap(Resources.LoginForm));

 

實際效果如下:

發佈了46 篇原創文章 · 獲贊 0 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章