天天看點

用Borland C# Builder制作不規則窗體

徐長友   

悠遊線上

作不規則窗體涉及到API的調用和大量的程式設計,是件很複雜的事情。下面我們可以使用Borland C# Builder輕松的實作一個不規則窗體,以下面用一個示例來講述其制作過程。

一.準備不規則窗體位圖 

二.窗體的設定

三.代碼的完成

一.準備不規則窗體位圖 

為了友善起見,我們随便找幾個别的軟體用的Skin。

這裡使用金山影霸 2003的安裝目錄下的skins/ocean/KingDVD_Disable.BMP

當然完全可以使用畫圖工具,制作一個有形狀的位圖,背景使用一種特别的顔色,如白色。這個顔色會在後面用得上。

用Borland C# Builder制作不規則窗體

[ 相關貼圖 ]

用Borland C# Builder制作不規則窗體

二.窗體的設定

1.建立C# Application

用Borland C# Builder制作不規則窗體

[ 相關貼圖 ]

用Borland C# Builder制作不規則窗體

2.選中建立的窗體,設定其相應屬性:

(1).将 FormBorderStyle 屬性設定為 None。

(2).将窗體的 BackgroundImage 屬性設定為先前面的位圖檔案。

(3).将 TransparencyKey 屬性設定為位圖檔案的背景色,本例中為白色。(此屬性告訴應用程式窗體中的哪些部分需要設定為透明。 )

(4).加一個picturebox1,就是一關閉位圖,點選時關閉應用程式。

(5).加一個contextMenu1,添加一菜單項“退出”,将winform的contextMenu設為contextMenu1。

按F9運作你的程式,就可以看到你的不規則窗體了。

用Borland C# Builder制作不規則窗體

[ 相關貼圖 ]

用Borland C# Builder制作不規則窗體

三.代碼的完成

   前面做的視窗還不能移動。加入下面的代碼使其能移動起來,帖出完整的示例代碼:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace SForm

{

 /// <summary>

 /// Summary description for WinForm.

 /// </summary>

 public class WinForm : System.Windows.Forms.Form

 {

  /// <summary>

  /// Required designer variable.

  /// </summary>

  private System.ComponentModel.Container components = null;

  private System.Windows.Forms.ContextMenu contextMenu1;

  private System.Windows.Forms.MenuItem menuItem1;

  private Point mouseOffset;        //記錄滑鼠指針的坐标

  private bool isMouseDown = false;

  private System.Windows.Forms.PictureBox pictureBox1; //記錄滑鼠按鍵是否按下

  public WinForm()

  {

   //

   // Required for Windows Form Designer support

   //

   InitializeComponent();

   //

   // TODO: Add any constructor code after InitializeComponent call

   //

  }

  /// <summary>

  /// Clean up any resources being used.

  /// </summary>

  protected override void Dispose (bool disposing)

  {

   if (disposing)

   {

    if (components != null)

    {

     components.Dispose();

    }

   }

   base.Dispose(disposing);

  }

  #region Windows Form Designer generated code

  /// <summary>

  /// Required method for Designer support - do not modify

  /// the contents of this method with the code editor.

  /// </summary>

  private void InitializeComponent()

  {

   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WinForm));

   this.contextMenu1 = new System.Windows.Forms.ContextMenu();

   this.menuItem1 = new System.Windows.Forms.MenuItem();

   this.pictureBox1 = new System.Windows.Forms.PictureBox();

   this.SuspendLayout();

   // 

   // contextMenu1

   // 

   this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

      this.menuItem1});

   // 

   // menuItem1

   // 

   this.menuItem1.Index = 0;

   this.menuItem1.Text = "退出";

   this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

   // 

   // pictureBox1

   // 

   this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));

   this.pictureBox1.Location = new System.Drawing.Point(290, 8);

   this.pictureBox1.Name = "pictureBox1";

   this.pictureBox1.Size = new System.Drawing.Size(14, 13);

   this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

   this.pictureBox1.TabIndex = 0;

   this.pictureBox1.TabStop = false;

   this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);

   // 

   // WinForm

   // 

   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

   this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));

   this.ClientSize = new System.Drawing.Size(365, 235);

   this.ContextMenu = this.contextMenu1;

   this.Controls.Add(this.pictureBox1);

   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

   this.MaximizeBox = false;

   this.MinimizeBox = false;

   this.Name = "WinForm";

   this.Text = "WinForm";

   this.TransparencyKey = System.Drawing.Color.White;

   this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseDown);

   this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseUp);

   this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.WinForm_MouseMove);

   this.ResumeLayout(false);

  }

  #endregion

  /// <summary>

  /// The main entry point for the application.

  /// </summary>

  [STAThread]

  static void Main() 

  {

   Application.Run(new WinForm());

  }

  private void menuItem1_Click(object sender, System.EventArgs e)

  {

   this.Close();

  }

  private void WinForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

  {

      if (isMouseDown)

      {

        Point mousePos = Control.MousePosition;

        mousePos.Offset(mouseOffset.X, mouseOffset.Y);

        Location = mousePos;

      }

  }

  private void WinForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

  {

      int xOffset;

      int yOffset;

      if (e.Button == MouseButtons.Left)

      {

        xOffset = -e.X - SystemInformation.FrameBorderSize.Width;

        yOffset = -e.Y - SystemInformation.CaptionHeight -

          SystemInformation.FrameBorderSize.Height;

        mouseOffset = new Point(xOffset, yOffset);

        isMouseDown = true;

      }

  }

  private void WinForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

  {

                        // 修改滑鼠狀态isMouseDown的值

                        // 確定隻有滑鼠左鍵按下并移動時,才移動窗體

                        if (e.Button == MouseButtons.Left) 

                        {

                                isMouseDown = false;

      }

  }

  private void pictureBox1_Click(object sender, System.EventArgs e)

  {

   this.Close();

  }

 }

}

注:如果螢幕的顔色深度設定大于 24 位,TransparencyKey 設定成白色,窗體的非透明部分還是會顯示出來,不知道為什麼。

有興趣的朋友交流一下,首頁: 

用Borland C# Builder制作不規則窗體

http://yousoft.hi.com.cn

繼續閱讀