使用c#隐藏任务栏时出现问题

时间:2009-07-21 02:55:14

标签: c#

我正在使用以下类来隐藏任务栏。 但问题是,在隐藏之后,那个地方没有焦点。 它就像被封锁了。如何克服这一点。感谢。

public class Taskbar
{
    [DllImport("user32.dll")]
    public static extern int FindWindow(string className, string windowText);
    [DllImport("user32.dll")]
    public static extern int ShowWindow(int hwnd, int command);

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

    public int _taskbarHandle;
    protected static int Handle
    {
        get
        {
            return FindWindow("Shell_TrayWnd", "");
        }
    }

    public Taskbar()
    {
        _taskbarHandle = FindWindow("Shell_TrayWnd", "");
    }

    public static void Show()
    {
        ShowWindow(Handle, SW_SHOW);
    }

    public static void Hide()
    {
        ShowWindow(Handle, SW_HIDE);
    }
}

2 个答案:

答案 0 :(得分:1)

您是否尝试运行全屏应用程序?如果是,请阅读How To Cover the Task Bar with a WindowHow To Make a Windows Form App Truly Full Screen (and Hide Taskbar) in C#

在同一篇文章中,您可以使用此代码运行真正的全屏应用程序

public class WinApi
{
    [DllImport(”user32.dll”, EntryPoint = “GetSystemMetrics”)]
    public static extern int GetSystemMetrics(int which);

    [DllImport(”user32.dll”)]
    public static extern void
        SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                     int X, int Y, int width, int height, uint flags);        

    private const int SM_CXSCREEN = 0;
    private const int SM_CYSCREEN = 1;
    private static IntPtr HWND_TOP = IntPtr.Zero;
    private const int SWP_SHOWWINDOW = 64; // 0×0040

    public static int ScreenX
    {
        get { return GetSystemMetrics(SM_CXSCREEN);}
    }

    public static int ScreenY
    {
        get { return GetSystemMetrics(SM_CYSCREEN);}
    }

    public static void SetWinFullScreen(IntPtr hwnd)
    {
        SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
    }
}

答案 1 :(得分:0)

使用此代码:

public class FullScreenEngine
{
    // Fields
    private IntPtr _hWndInputPanel;
    private IntPtr _hWndSipButton;
    private IntPtr _hWndTaskBar;
    private Rectangle _desktopArea;

    public FullScreenEngine()
    {
        Init();
    }

    public bool SetFullScreen(bool mode)
    {
        try
        {
            if (mode)
            {
                if (_hWndTaskBar.ToInt64() != 0L)
                {
                    ShowWindow(_hWndTaskBar, SW_HIDE);
                }
                if (_hWndInputPanel.ToInt64() != 0L)
                {
                    ShowWindow(_hWndInputPanel, SW_HIDE);
                }
                if (_hWndSipButton.ToInt64() != 0L)
                {
                    ShowWindow(_hWndSipButton, SW_HIDE);
                }
                WorkArea.SetWorkArea(new RECT(Screen.PrimaryScreen.Bounds));
            }
            else
            {
                if (_hWndTaskBar.ToInt64() != 0L)
                {
                    ShowWindow(_hWndTaskBar, SW_SHOW);
                }
                if (_hWndInputPanel.ToInt64() != 0L)
                {
                    //ShowWindow(_hWndInputPanel, SW_SHOW);
                }
                if (_hWndSipButton.ToInt64() != 0L)
                {
                    ShowWindow(_hWndSipButton, SW_SHOW);
                }
                WorkArea.SetWorkArea(new RECT(_desktopArea));
            }
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

    private bool Init()
    {
        try
        {
            _desktopArea = Screen.PrimaryScreen.WorkingArea;
            _hWndInputPanel = FindWindowW("SipWndClass", null);
            _hWndSipButton = FindWindowW("MS_SIPBUTTON", null);
            _hWndTaskBar = FindWindowW("HHTaskBar", null);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

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

    [DllImport("coredll.dll")]
    private static extern int ShowWindow(IntPtr hwnd, uint command);

    [DllImport("coredll.dll")]
    private static extern IntPtr FindWindowW(string lpClassName, string lpWindowName);

    // Nested Types
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;

        public RECT(Rectangle rect) : this()
        {
            Left = rect.Left;
            Right = rect.Left+rect.Width;
            Top = rect.Top;
            Bottom = rect.Top + rect.Height;
        }
    }

    private static class WorkArea
    {
        [DllImport("coredll.dll")]
        private static extern bool SystemParametersInfo(uint uAction, uint uparam, ref RECT rect, uint fuWinIni);

        private const uint WM_SETTINGCHANGE = 0x1a;
        const uint SPI_GETWORKAREA = 48;
        const uint SPI_SETWORKAREA = 47;

        public static bool SetWorkArea(RECT rect)
        {
            return SystemParametersInfo(SPI_SETWORKAREA, 0, ref rect, WM_SETTINGCHANGE);
        }

        public static RECT GetWorkArea()
        {
            var rect = new RECT();
            SystemParametersInfo(SPI_GETWORKAREA, 0, ref rect, 0);
            return rect;
        }
    }
}