在項目開發中的窗體的設計會影響用戶對軟件的整體印象,因此窗體的效果要設計得美觀一些。
029 制作鼠標穿透窗體
在對桌面進行操作時,為了使桌面更加美觀,可以在桌面的上面加一層類似於玻璃的效果,用戶可以用鼠標透過“玻璃”對桌面進行操作。本實例通過使用鼠標穿透窗體類實現以上功能。主要用到了 API 函數 SetWindowLong 和 GetWindowLong。
1.創建一個項目,默認窗體為 Form1,將該窗體的 FormBordeStyle 屬性設置為 None,BackColor 屬性設置為 Gainsboro,Opacity 屬性設置為 60%,WindowState 屬性設置為 Maximized。
2.在 Form1 窗體中添加一個 NotifyIcon 控件,並設置其 Icon 屬性為指定的圖標,顯示提示信息;添加一個 ContextMenuStrip 控件,作為程序的快捷菜單。
namespace _029_MouseThroughForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private const uint WS_EX_LAYERED = 0x80000; private const int WS_EX_TRANSPARENT = 0x20; private const int GWL_EXSTYLE = (-20); private string Var_genre = "";//記錄當前操作的類型 #region 在窗口結構中為指定的窗口設置信息 /// <summary> /// 在窗口結構中為指定的窗口設置信息 /// </summary> /// <param name="hwnd">欲為其取得信息的窗口的句柄</param> /// <param name="nIndex">欲取回的信息</param> /// <param name="dwNewLong">由nIndex指定的窗口信息的新值</param> /// <returns></returns> [DllImport("user32", EntryPoint = "SetWindowLong")] private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong); #endregion #region 從指定窗口的結構中取得信息 /// <summary> /// 從指定窗口的結構中取得信息 /// </summary> /// <param name="hwnd">欲為其獲取信息的窗口的句柄</param> /// <param name="nIndex">欲取回的信息</param> /// <returns></returns> [DllImport("user32", EntryPoint = "GetWindowLong")] private static extern uint GetWindowLong(IntPtr hwnd, int nIndex); #endregion #region 使窗口有鼠標穿透功能 /// <summary> /// 使窗口有鼠標穿透功能 /// </summary> private void CanPenetrate() { uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE); //從當前窗口的結構中取得信息 //在窗口結構中為當前窗口設置信息 uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED); } #endregion private void Form1_Load(object sender, EventArgs e) { this.ShowInTaskbar = false;//窗體不出現在Windows任務欄中 CanPenetrate();//自定義方法,用來通過API函數SetWindowLong和GetWindowLong實現鼠標的穿透效果 this.TopMost = true;//使窗體始終在其它窗體之上 } #region 設置顏色和透明度的狀態 /// <summary> /// 設置顏色和透明度的狀態 /// </summary> private void SetEstate(Form Frm, object sender) { Var_genre = ((ToolStripMenuItem)sender).Name; string Tem_Str = Var_genre; if (Var_genre.IndexOf('_') >= 0) { Var_genre = Tem_Str.Substring(0, Tem_Str.IndexOf('_')); } switch (Var_genre) { case "ToolColor": { Color Tem_Color = Color.Gainsboro; switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString())) { case 1: Tem_Color = Color.Gainsboro; break; case 2: Tem_Color = Color.DarkOrchid; break; case 3: Tem_Color = Color.RoyalBlue; break; case 4: Tem_Color = Color.Gold; break; case 5: Tem_Color = Color.LightGreen; break; } Frm.BackColor = Tem_Color; break; } case "ToolClarity": { double Tem_Double = 0.0; switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString())) { case 1: Tem_Double = 0.1; break; case 2: Tem_Double = 0.2; break; case 3: Tem_Double = 0.3; break; case 4: Tem_Double = 0.4; break; case 5: Tem_Double = 0.5; break; case 6: Tem_Double = 0.6; break; case 7: Tem_Double = 0.7; break; case 8: Tem_Double = 0.8; break; case 9: Tem_Double = 0.9; break; } Frm.Opacity = Tem_Double; break; } case "ToolAcquiescence": { Frm.BackColor = Color.Gainsboro; Frm.Opacity = 0.6; break; } case "ToolClose": { Close(); break; } } } #endregion private void ToolColor_Glass_Click(object sender, EventArgs e) { SetEstate(this, sender); } } }
030 窗體換膚程序
本實例的基本原理是給窗體的各個組成部分更換圖片,基於該原理,首先需要分析窗體的組成部分,主要包括標題欄、左邊框、右邊框、下邊框、窗體中間區域及可能存在的菜單欄,其中標題欄和3個邊框無法通過設置相關屬性來達到更換背景圖片的目的。對於這個問題,可以通過取消窗體的 FormBorderStyle 屬性,同時在標題欄和3個邊框的位置添加 Panel 控件來解決,然後通過選擇不同的皮膚類型,為窗體的各個組成部分設置圖片,最終達到窗體換膚的效果。在以上過程中,最主要的技術問題是如何從指定的文件創建 Image 對象,以及如何獲取圖片的路徑。
1.創建一個項目,默認窗體為 Form1,設置 Form1 的 AutoscaleMode 屬性為 Inherit,DoubleBuffered 屬性為 True,IsMdiContainer 屬性為 True。
2.在窗體中添加6個 Panel 控件,分別用來作為窗體的標題欄、下邊框、左邊框、右邊框、左下角邊框和右下角邊框;添加一個 ContextMenuStrip 控件,用來作為更換皮膚的快捷菜單;添加3個 PictureBox 控件,分別用來顯示最大化、最小化和關閉圖片。
namespace _030_WinCusSkin { public partial class Form1 : Form { String strImagesPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\")); int top, left, hei, wid; bool bol = false, bo = false, bolTop = false, bolLeft = false, bolRight = false, bolBottom = false, bolLeftCornu = false, bolRightCornu = false; int x = 0, y = 0; public Form1() { InitializeComponent(); } //關閉事件。 private void picClose_Click(object sender, System.EventArgs e) { Application.Exit(); } //最大化事件。 private void picMaximize_Click(object sender, System.EventArgs e) { if (!bol) //若窗體處於普通狀態 { top = this.Top; //獲取窗體的Top屬性值 left = this.Left; //獲取窗體的Left屬性值 hei = this.Height; //獲取窗體的Height屬性值 wid = this.Width; //獲取窗體的Width屬性值 this.Top = 0; //設置窗體的Top屬性值為零 this.Left = 0; //設置窗體的Left屬性值為零 int hg = SystemInformation.MaxWindowTrackSize.Height; //獲取窗口的默認最大高度 int wh = SystemInformation.MaxWindowTrackSize.Width; //獲取窗口的默認最大寬度 this.Height = hg; //設置窗體的Height屬性值 this.Width = wh; //設置窗體的Width屬性值 bol = true; //設置窗體標記表示最大化 if (menItemSkin1.Checked) //若選擇“紫色小花”菜單項,設置最大化圖片的Image屬性 this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\purple\max.png"); if (menItemSkin2.Checked) //若選擇“藍色經典”菜單項,設置最大化圖片的Image屬性 this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\blue\max.png"); if (menItemSkin3.Checked) //若選擇“綠色菜園”菜單項,設置最大化圖片的Image屬性 this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\green\max.png"); } else //若窗體處於最大化狀態 { this.Top = top; //設置窗體的Top屬性值 this.Left = left; //設置窗體的Left屬性值 this.Height = hei; //設置窗體的Height屬性值 this.Width = wid; //設置窗體的Width屬性值 bol = false; //設置窗體標記表示普通狀態 if (menItemSkin1.Checked) this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\purple\max_Normal.png"); if (menItemSkin2.Checked) this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\blue\max_Normal.png"); if (menItemSkin3.Checked) this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\green\max_Normal.png"); } } //最小化事件。 private void picMinimize_Click(object sender, System.EventArgs e) { top = this.Top; left = this.Left; hei = this.Height; wid = this.Width; this.Height = 0; this.Width = 0; bo = true; } //窗口再次被激活時。 private void Form_Activated(object sender, System.EventArgs e) { if (bo) { this.Top = top; this.Left = left; this.Height = hei; this.Width = wid; bo = false; } } //選擇關閉。 private void mItemColse_Click(object sender, System.EventArgs e) { Application.Exit(); } //上邊框鼠標按下。 private void panel_Top_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { x = e.X; y = e.Y; this.bolTop = true; } //上邊框鼠標移動。 private void panel_Top_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (bolTop) { this.Top += e.Y - y; this.Left += e.X - x; } } //上邊框鼠標釋放。 private void panel_Top_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this.bolTop = false; } //左邊框鼠標按下。 private void panel_Left_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { x = e.X; this.bolLeft = true; } //左邊框鼠標移動。 private void panel_Left_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (bolLeft) { this.Width += x - e.X; this.Left += e.X - x; } } //左邊框鼠標離開。 private void panel_Left_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this.bolLeft = false; } //右邊框鼠標按下。 private void panel_Right_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { x = e.X; this.bolRight = true; } //右邊框鼠標移動。 private void panel_Right_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (bolRight) { this.Width += e.X - x; } } //右邊框鼠標離開。 private void panel_Right_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this.bolRight = false; } //下邊框鼠標按下。 private void panel_Bottom_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { y = e.Y; this.bolBottom = true; } //下邊框鼠標移動。 private void panel_Bottom_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (bolBottom) { this.Height += e.Y - y; } } //下邊框鼠標離開。 private void panel_Bottom_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this.bolBottom = false; } //左下角鼠標按下。 private void panelLeftCornu_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { x = e.X; y = e.Y; this.bolLeftCornu = true; } //左下角鼠標移動。 private void panelLeftCornu_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (bolLeftCornu) { this.Width += x - e.X; this.Left += e.X - x; this.Height += e.Y - y; } } //左下角鼠標離開。 private void panelLeftCornu_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { x = e.X; y = e.Y; this.bolLeftCornu = false; } //右下角鼠標按下。 private void panelRightCornu_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { this.bolRightCornu = true; } //右下角鼠標移動。 private void panelRightCornu_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (bolRightCornu) { this.Width += e.X - x; this.Height += e.Y - y; } } //右下角鼠標離開。 private void panelRightCornu_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this.bolRightCornu = false; } private void Form_Load(object sender, EventArgs e) { menItemSkin2_Click(sender, e);//默認藍色經典 } private void menItemSkin1_Click(object sender, EventArgs e) { //設置Panel控件的BackgroundImage屬性 this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\top.png"); this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\left.png"); this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\right.png"); this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\bottom.png"); //設置最小化圖片控件的Image屬性 this.picMinimize.Image = Image.FromFile(strImagesPath + @"\images\purple\min.png"); if (bol == true) //若當前窗體處於最大化狀態 { //設置最大化圖片控件的Image屬性 this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\purple\max.png"); } else { //設置最大化圖片控件的Image屬性 this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\purple\max_normal.png"); } //設置關閉圖片控件的Image屬性 this.picClose.Image = Image.FromFile(strImagesPath + @"\images\purple\close.png"); //設置菜單的選中標記 this.menItemSkin1.Checked = true; this.menItemSkin2.Checked = false; this.menItemSkin3.Checked = false; //設置窗體主菜單的背景圖像屬性 this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\menu.gif"); //設置窗體的背景圖像屬性 this.BackgroundImage = Image.FromFile(strImagesPath + @"\images\purple\background.gif"); } private void menItemSkin2_Click(object sender, EventArgs e) { this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\top.png"); this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\left.png"); this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\right.png"); this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\bottom.png"); this.picMinimize.Image = Image.FromFile(strImagesPath + @"\images\blue\min.png"); if (bol == true) { this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\blue\max.png"); } else { this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\blue\max_normal.png"); } this.picClose.Image = Image.FromFile(strImagesPath + @"\images\blue\close.png"); this.menItemSkin1.Checked = false; this.menItemSkin2.Checked = true; this.menItemSkin3.Checked = false; this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\menu.gif"); this.BackgroundImage = Image.FromFile(strImagesPath + @"\images\blue\background.gif"); } private void menItemSkin3_Click(object sender, EventArgs e) { this.panel_Top.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\top.png"); this.panel_Left.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\left.png"); this.panel_Right.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\right.png"); this.panel_Bottom.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\bottom.png"); this.picMinimize.Image = Image.FromFile(strImagesPath + @"\images\green\min.png"); if (bol == true) { this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\green\max.png"); } else { this.picMaximize.Image = Image.FromFile(strImagesPath + @"\images\green\max_normal.png"); } this.picClose.Image = Image.FromFile(strImagesPath + @"\images\green\close.png"); this.menItemSkin1.Checked = false; this.menItemSkin2.Checked = false; this.menItemSkin3.Checked = true; this.menuStrip1.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\menu.gif"); this.BackgroundImage = Image.FromFile(strImagesPath + @"\images\green\background.gif"); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void panel_Top_DoubleClick(object sender, EventArgs e) { picMaximize_Click(sender, e); } } }