使用Window的API來隱藏作業系統下方的那條工具列

以及調整視窗畫面讓程式變成全螢幕

以下是方法1,可能會有工作列沒辦法隱藏的問題,那就請看方法2

(目前我用方法2比較成功!!=.=)

要using  System.Runtime.InteropServices;

首先先宣告

方法1:

      private const int SW_HIDE = 0;
      private const int SW_SHOW = 1;

      private const int SM_CXSCREEN = 0;
      private const int SM_CYSCREEN = 1;

      static readonly IntPtr HWND_TOP = new IntPtr(0);
      static readonly UInt32 SWP_SHOWWINDOW = 0x0040;
      private int hwnd=FindWindow("Shell_TrayWnd", "");

        //這是找Window下方那條工具列
        [DllImport("user32.dll")]
        private static extern int FindWindow(string className, string windowText);

        //這是去隱藏視窗跟顯示視窗
        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int command);

        //這是改變視窗的位置或是大小
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(int hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        //這是去抓"螢幕"的寬跟高要用的
        [DllImport("user32.dll")]
        public static extern int GetSystemMetrics(int screensize);

好了,宣告完上面那些亂七八糟的東西後

就可以直接用那些函式了

//把工具列隱藏

ShowWindow(hwnd, SW_HIDE);

//把工具列顯示出來

ShowWindow(hwnd, SW_SHOW);

 

//工具列隱藏後,我們的視窗不會自動變成螢幕的大小

//會變成下方一條空出來很難看,所以再用這行把視窗調整成螢幕的大小

//在這裡用c#的那些什麼最大化的程式都是無效的,一定要用這個吧...

 SetWindowPos((int)this.Handle, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW);

 

好了完成 ~~隱藏工具列後要記得讓它顯示出來阿

不然你就沒有工具列可以按了=.=

 

補充

移動滑鼠座標的API

宣告

[DllImport("user32.dll")]
private static extern bool SetCursorPos(int X,int Y);

就可以用了

SetCursorPos(0,0);

 

方法2:

先宣告

        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(int hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);


        [DllImport("user32.dll")]
        public static extern int GetSystemMetrics(int screensize);

 

        public const int SM_CXSCREEN = 0;

        public const int SM_CYSCREEN = 1;

        private static IntPtr HWND_TOP = IntPtr.Zero;

        private const int SWP_SHOWWINDOW = 64;

開一個顯示全螢幕的函式


            this.WindowState = FormWindowState.Normal; 
            this.FormBorderStyle = FormBorderStyle.None;
            SetWindowPos((int)this.Handle, HWND_TOP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW);

然後在回復成普通視窗的函式裡寫

            this.ControlBox = true;
            this.Text = F_Title;
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;

 

就好啦

arrow
arrow
    全站熱搜

    welkingunther 發表在 痞客邦 留言(4) 人氣()