最近因為項目需要需要實現桌面圖標和任務狀態欄的隱藏功能,實現的方式很多,比如修改注冊表值,調用windows API函數等。經過一番的查閱,這個功能暫時實現了,或許不是很好的方法,但是我預期的效果達到了,希望各位博友批評指正。^_^
好了,廢話不多說~ 按步驟 GO!
Step1:通過VS建立一個winform項目,在代碼界面添加引用
using System.Runtime.InteropServices;
這一步驟很重要,因為下面有很多函數和功能是封裝在上面的類庫中
Step2: 照着下圖拖控件
Step3: 調用windows API 函數,也即在form2的代碼界面添加如下代碼:
[DllImport("User32.dll", EntryPoint = "FindWindow")] public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
Step4: 定義如下兩個函數,實現桌面圖標和狀態欄的顯示和隱藏:
/// <summary> /// 隱藏任務欄和桌面圖標 /// </summary> private void hideTaskbar() { IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null); IntPtr hStar = FindWindow("Button", null); IntPtr desktopPtr = FindWindow("Progman", null); if (trayHwnd != IntPtr.Zero) { ShowWindow(desktopPtr, 0);//隱藏桌面圖標 (0是隱藏,1是顯示) ShowWindow(trayHwnd, 0);//隱藏任務欄 ShowWindow(hStar, 0);//隱藏windows 按鈕 } } /// <summary> /// 顯示任務欄和桌面圖標 /// </summary> private void showTaskbar() { IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null); IntPtr hStar = FindWindow("Button", null); IntPtr desktopPtr = FindWindow("Progman", null); if (trayHwnd != IntPtr.Zero) { ShowWindow(desktopPtr, 1); ShowWindow(trayHwnd, 1); ShowWindow(hStar, 1); } }
Step5: 在button的click事件中添加上面函數的調用
/// <summary> /// 隱藏桌面。。。按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { hideTaskbar(); } /// <summary> /// 顯示桌面 。。。按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { showTaskbar(); }
啟動調試。。。 嘿嘿 是不是實現了~!
Over!
到此這個功能算是實現了~! 最后,希望大家多多支持~,有啥問題歡迎給我留言哦~