65.9K
CodeProject 正在变化。 阅读更多。
Home

交互式异形窗体

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.59/5 (14投票s)

2002年3月20日

viewsIcon

103629

downloadIcon

2690

此演示展示了如何使用 Region、GraphicsPath 和图片类创建形状窗体和自定义按钮。

Sample Image - ShapedForm.jpg

引言

此演示展示了如何使用 Region、GraphicsPath 和图片类创建形状窗体和自定义按钮。它还演示了使用图片控件显示动画图片。

所有图形均来自 Microsoft Media Player,并已调整大小以减小图形文件的大小。

步骤

  1. 创建一个带有背景色的窗体,然后将 TransparenceKey 属性设置为该颜色,并将 FormBorderStyle 设置为 None。

  2. 重写 Form_Paint 函数

    你可以通过

    protected override void  OnPaint(PaintEventArgs e)

    或者添加一个新的处理程序

    this.Paint += new System.Windows.Form.PaintEventHandler(Form_Paint)

  3. 使用 Region 和 GraphicsPath 设置要显示的 Region。
    im = new Bitmap("mediaPlayer.jpg"); 
    
    private void Form_Paint(object sender, PaintEventArgs e) 
    { 
       Graphics g = e.Graphics; 
    
       Rectangle mainRect = new Rectangle(0, 0, 695, 278); 
    
       Region mainRegion = new Region(mainRect); 
    
       e.Graphics.SetClip(mainRegion, CombineMode.Replace); 
    
       // Create a GraphicsPath object and add a curve. 
       GraphicsPath myPath = new GraphicsPath(); 
    
       ... 
    
       Region ExcludeRegion3 = new Region(myPath); 
    
       e.Graphics.ExcludeClip(ExcludeRegion3); 
    
       ... 
    
       e.Graphics.DrawImage(im, 0, 0, 495,278); 
    
       // Reset clipping region to infinite. 
       e.Graphics.ResetClip(); 
    } 
    

注意:将所有图形保存在 bin/debug 目录下。

如果您有任何意见,我非常乐意听取。您可以通过 潘继斌 联系我。

© . All rights reserved.