程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> WinForm中的特殊窗體效果:漸變窗口和信息提示窗口

WinForm中的特殊窗體效果:漸變窗口和信息提示窗口

編輯:關於C#

在WinForm中偶爾會遇到某些特殊效果:比如某個窗口剛開始的時候是完全透明的,隨著時間的變化 ,窗體逐漸不透明,直至完全不透明。這是本文要探討的窗體效果之一:漸變窗體。

還有一種窗 體效果:有些軟件在某個特定的時間會顯示一個提示窗體,這個窗體不是直接顯示的,而是慢慢從窗口 的最下方向上移動,直至窗體完全顯示就不再移動。當我們點擊“確定”按鈕之後,窗體由 從屏幕上逐漸下移,直至完全從屏幕上完全不顯示。這也是本文討論的窗體效果之一:移動提示信息窗 口。

(一)漸變窗體

每個窗體都有一個Opacity屬性,它的值從0到1之間,表示窗體的透 明效果,0表示完全透明,1表示完全不透明。我們可以動態設置這個值,實現窗體從完全透明到完全不 透明。核心代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ThreadDemo
{
 /// <summary>
  /// 說明:這是一個漸變窗口,當程序運行的時候,窗體是完全透 明的
  /// 隨著時間的變化,窗體逐漸變為完全部透明
  /// 作者:周公
   /// 原創地址:<A href="http://blog.csdn.net/zhoufoxcn/archive/2008/06/16/2554064.aspx">http://b log.csdn.net/zhoufoxcn/archive/2008/06/16/2554064.aspx</A>
  /// </summary>
  public partial class TimerForm : Form
  {
 private double opacity = 0;//記錄當前窗體的透明度
    public TimerForm()
    {
 InitializeComponent();
      Opacity = 0;//指定窗體完全透明
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
 if (opacity <= 1)
      {
 opacity = opacity + 0.05;
         Opacity = opacity;
      }
      Console.WriteLine ("Opacity=" + Opacity);
    }
  }
}

(二)移動提示 信息窗口每個Control類都有一個Location屬性,它是一個Point值,這個值表示控件的左上角的坐標值 ,利用這個坐標值,我們可以設置窗體的位置。程序核心代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ThreadDemo
{
 /// <summary>
  /// 說明:這是信息提示窗 口,運行程序的時候,這個窗口會緩慢從屏幕下方
  /// 向上移動,知道提示信息窗口完全顯 示;
  /// 當點擊“確定”按鈕之後,這個窗口又會緩慢從屏幕區域移出
   /// 作者:周公
  /// 原創地址:<A href="http://blog.csdn.net/zhoufoxcn/archive/2008/06/16/2554064.aspx">http://b log.csdn.net/zhoufoxcn/archive/2008/06/16/2554064.aspx</A>
  /// </summary>
  public partial class NoteForm : Form 
  {
 private int screenWidth;//屏幕寬度
    private int screenHeight;//屏幕高度
     private bool finished=false;//是否完全顯示提示窗口 
    public NoteForm()
     {
 InitializeComponent();
      screenHeight = Screen.PrimaryScreen.Bounds.Height;
      screenWidth = Screen.PrimaryScreen.Bounds.Width;
      //設置提示窗口坐標在屏幕可顯示區域之外
      Location = new Point(screenWidth-Width, screenHeight);
    }
    private void NoteForm_Load(object sender, EventArgs e)
    {
 }
    private void timer1_Tick(object sender, EventArgs e)
    {
 if (! finished)//如果提示窗口沒有完全顯示
      {
 //如果提示窗口的縱坐標與提示 窗口的高度之和大於屏幕高度
        if (Location.Y + Height >= screenHeight)
        {
 Location = new Point(Location.X, Location.Y - 5);
        }
      }
      else//如果提示窗口已經完成了 顯示,並且點擊了確定按鈕
      {
 //如果提示窗口沒有完全從屏幕上消失
        if (Location.Y < screenHeight)
        {
 Location = new Point(Location.X, Location.Y + 5);
        }
      }
     }
    private void btnOK_Click(object sender, EventArgs e)
    {
 //設置完成了顯示,以便讓提示控件移出屏幕可顯示區域
      finished = true;
    }
  }
}

說明:整個程序源代碼(包括可執行文件)可從http://download.csdn.net/zhoufoxcn下載。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved