制作不规则窗体

如果是背景是图片,用TansparencyKey可能会出现一些未知的问题,  
  可以使用Region来限定范围.  
   
  给你个例子(制作不规则窗体):  
                                      在form_load()中调drawform()  
   
  private   void   drawform()  
  {  
  Bitmap   image   =   (Bitmap)Image.FromFile("小女孩.bmp");//你的图片  
  GraphicsPath   path   =   new   GraphicsPath();  
  int   width   =   image.Width;  
  int   height   =   image.Height;  
   
  Rectangle   rect   ;  
  Color   TransparentColor   =   image.GetPixel(1,1);//这个是对你图片的要求.在橡树点(1,1)处是

Transparent,或者你也可以传进来试试.  
   
  for(int   i=0;   i<width-1;   i++)  
  {  
  for(int   j=0;   j<height-1;j++)  
  {  
  if(image.GetPixel(i,   j)   !=   TransparentColor)  
  {  
  rect   =   new   Rectangle(i,j,1,1);  
  path.AddRectangle(rect);  
  }  
  }  
  }  
  this.Region   =   new   Region(path);  
  }  
                                      //再Paint事件或其他事件中loadPicture.  
  private   void   TransparentForm_Paint(object   sender,   System.Windows.Forms.PaintEventArgs   e)  
  {  
  Bitmap   bgPic   =   (Bitmap)Image.FromFile("小女孩.bmp");  
  Graphics   g   =   this.CreateGraphics();  
  g.DrawImage(bgPic,0,0,bgPic.Width,bgPic.Height);  
  g.Dispose();  
  }  
   
  当然,这个方法有个问题,如果图片太大,有性能问题.  
  不过可以把取GraphicsPath的过程跟程序分开处理.   另外保存GraphicsPath,到时直接用就OK了.

posted @ 2009-07-14 14:18  94cool  阅读(189)  评论(0)    收藏  举报