如何切换/切换Windows XP从“显示”到“自动隐藏”(反之亦然)?

gfttwv5a  于 7个月前  发布在  Windows
关注(0)|答案(8)|浏览(79)

基本上,我想做一个简单的切换程序(将Map到一些键盘快捷键),如果在正常模式下,将屏幕设置为自动隐藏模式(反之,如果在自动隐藏模式下,将屏幕设置为正常显示模式)。
你知道如何在C#中实现它吗?(或者Win32 C++,但任何实际上可以做到这一点的东西都很好。
谢谢,希望我说得够清楚了.

我真的不想要任何全屏应用程序,将重叠的屏幕,只有无窗口的程序,切换显示模式和退出。我从自动隐藏切换到正常视图定期,并希望简化它。(使用Win7。)

编辑。例如

#include <windows.h>

int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    SetWindowPos(FindWindow(L"Shell_traywnd", NULL ), 0, 0, 0, 0, 0, 0x40);
}

字符串
不会这样做,它只显示已经可见=true的隐藏,但不会将其切换到/从自动隐藏。(同样适用于0x 80。)

qnzebej0

qnzebej01#

以下是我用途:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);

[DllImport("shell32.dll")]
public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData);

public enum AppBarMessages
{
    New              = 0x00,
    Remove           = 0x01,
    QueryPos         = 0x02,
    SetPos           = 0x03,
    GetState         = 0x04,
    GetTaskBarPos    = 0x05,
    Activate         = 0x06,
    GetAutoHideBar   = 0x07,
    SetAutoHideBar   = 0x08,
    WindowPosChanged = 0x09,
    SetState         = 0x0a
}

[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
    public UInt32 cbSize;
    public IntPtr hWnd;
    public UInt32 uCallbackMessage;
    public UInt32 uEdge;
    public Rectangle rc;
    public Int32 lParam;
}

public enum AppBarStates
{
    AutoHide    = 0x01,
    AlwaysOnTop = 0x02
}

/// <summary>
/// Set the Taskbar State option
/// </summary>
/// <param name="option">AppBarState to activate</param>
public void SetTaskbarState(AppBarStates option)
{
    APPBARDATA msgData = new APPBARDATA();
    msgData.cbSize = (UInt32)Marshal.SizeOf(msgData);
    msgData.hWnd = FindWindow("System_TrayWnd", null);
    msgData.lParam = (Int32)(option);
    SHAppBarMessage((UInt32)AppBarMessages.SetState, ref msgData);
}

/// <summary>
/// Gets the current Taskbar state
/// </summary>
/// <returns>current Taskbar state</returns>
public AppBarStates GetTaskbarState()
{
    APPBARDATA msgData = new APPBARDATA();
    msgData.cbSize = (UInt32)Marshal.SizeOf(msgData);
    msgData.hWnd = FindWindow("System_TrayWnd", null);
    return (AppBarStates)SHAppBarMessage((UInt32)AppBarMessages.GetState, ref msgData);
}

字符串
当上面的代码实现时,只需通过以下方式将任务栏设置为自动隐藏:SetTaskbarState(AppBarStates.AutoHide);
通过以下方式获取当前状态:

AppBarStates currentState = GetTaskbarState();

lnvxswe2

lnvxswe22#

我跟随@Quispie回答,但它一开始在Windows 10中不起作用,但给了我解决它的基础和来源(所以荣誉),还有http://www.pinvoke.net/

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);

[DllImport("shell32.dll")]
public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData);

public enum AppBarMessages
{
    New = 0x00,
    Remove = 0x01,
    QueryPos = 0x02,
    SetPos = 0x03,
    GetState = 0x04,
    GetTaskBarPos = 0x05,
    Activate = 0x06,
    GetAutoHideBar = 0x07,
    SetAutoHideBar = 0x08,
    WindowPosChanged = 0x09,
    SetState = 0x0a
}

[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
    public int cbSize; // initialize this field using: Marshal.SizeOf(typeof(APPBARDATA));
    public IntPtr hWnd;
    public uint uCallbackMessage;
    public uint uEdge;
    public RECT rc;
    public int lParam;
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left, Top, Right, Bottom;

    public RECT(int left, int top, int right, int bottom)
    {
        Left = left;
        Top = top;
        Right = right;
        Bottom = bottom;
    }

    public RECT(System.Drawing.Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }

    public int X
    {
        get { return Left; }
        set { Right -= (Left - value); Left = value; }
    }

    public int Y
    {
        get { return Top; }
        set { Bottom -= (Top - value); Top = value; }
    }

    public int Height
    {
        get { return Bottom - Top; }
        set { Bottom = value + Top; }
    }

    public int Width
    {
        get { return Right - Left; }
        set { Right = value + Left; }
    }

    public System.Drawing.Point Location
    {
        get { return new System.Drawing.Point(Left, Top); }
        set { X = value.X; Y = value.Y; }
    }

    public System.Drawing.Size Size
    {
        get { return new System.Drawing.Size(Width, Height); }
        set { Width = value.Width; Height = value.Height; }
    }

    public static implicit operator System.Drawing.Rectangle(RECT r)
    {
        return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
    }

    public static implicit operator RECT(System.Drawing.Rectangle r)
    {
        return new RECT(r);
    }

    public static bool operator ==(RECT r1, RECT r2)
    {
        return r1.Equals(r2);
    }

    public static bool operator !=(RECT r1, RECT r2)
    {
        return !r1.Equals(r2);
    }

    public bool Equals(RECT r)
    {
        return r.Left == Left && r.Top == Top && r.Right == Right && r.Bottom == Bottom;
    }

    public override bool Equals(object obj)
    {
        if (obj is RECT)
            return Equals((RECT)obj);
        else if (obj is System.Drawing.Rectangle)
            return Equals(new RECT((System.Drawing.Rectangle)obj));
        return false;
    }

    public override int GetHashCode()
    {
        return ((System.Drawing.Rectangle)this).GetHashCode();
    }

    public override string ToString()
    {
        return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
    }
}

public enum AppBarStates
{
    AlwaysOnTop = 0x00,
    AutoHide = 0x01
}

/// <summary>
/// Set the Taskbar State option
/// </summary>
/// <param name="option">AppBarState to activate</param>
public void SetTaskbarState(AppBarStates option)
{
    APPBARDATA msgData = new APPBARDATA();
    msgData.cbSize = Marshal.SizeOf(msgData);
    msgData.hWnd = FindWindow("System_TrayWnd", null);
    msgData.lParam = (int)option;
    SHAppBarMessage((UInt32)AppBarMessages.SetState, ref msgData);
}

/// <summary>
/// Gets the current Taskbar state
/// </summary>
/// <returns>current Taskbar state</returns>
public AppBarStates GetTaskbarState()
{
    APPBARDATA msgData = new APPBARDATA();
    msgData.cbSize = Marshal.SizeOf(msgData);
    msgData.hWnd = FindWindow("System_TrayWnd", null);
    return (AppBarStates)SHAppBarMessage((UInt32)AppBarMessages.GetState, ref msgData);
}

字符串

j8yoct9x

j8yoct9x3#

隐藏密码

这是一个比C#更与WIN32 API相关的问题。你可以使用this(当然需要翻译成dot net)来隐藏任务栏。
您可以使用http://www.pinvoke.net将WIN32 API调用转换为dot net。

将自动隐藏设置为隐藏

您可以通过使用描述here的键操作注册表来实现这一点。
这应该是一个简单的任务,祝你好运。

jpfvwuh4

jpfvwuh44#

该控件是一个应用栏,您可以使用SHAppBarMessage控制它

pobjuy32

pobjuy325#

对于所有从谷歌来到这里并使用Windows 10的人来说,像我一样,来自@Quispie和@nicruo的答案是可以的,但需要额外的if
原因是类名因版本而异(显然,因为我不再有任何其他Windows,但10)。

msgData.hWnd = FindWindow("System_TrayWnd", null);
if (msgData.hWnd == IntPtr.Zero)
    msgData.hWnd = FindWindow("Shell_TrayWnd", null);

字符串

w1jd8yoj

w1jd8yoj6#

VB“显示”和“自动隐藏”任务栏- Windows 10

我已经在VB中翻译了这个,这可能对其他人有用(Windows 10;应该在32位和64位中工作):

Option Explicit On
    Option Strict On
    Imports System.Runtime.InteropServices

    Module WindowsTaskbarSettings

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer

    'https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shappbarmessage
    'https://learn.microsoft.com/nl-nl/windows/win32/api/shellapi/ns-shellapi-appbardata

    'https://learn.microsoft.com/en-us/windows/win32/shell/abm-getstate 'requires csize to be set
    'https://learn.microsoft.com/en-us/windows/win32/shell/abm-setstate 'requires hwnd and csize to be set

    Structure APPBARDATA
        Dim cbSize As Integer
        Dim hwnd As Long
        Dim uCallbackMessage As Integer '[Delegate]
        Dim uEdge As Integer
        Dim rc As RECT
        Dim lParam As Integer 'message specific, see 
    End Structure

    Structure RECT
        Dim Left As Integer
        Dim Top As Integer
        Dim Right As Integer
        Dim Bottom As Integer
    End Structure

    Public Enum AppBarMessages
        Newx = &H0
        Remove = &H1
        QueryPos = &H2
        SetPos = &H3
        GetState = &H4
        GetTaskBarPos = &H5
        Activate = &H6
        GetAutoHideBar = &H7
        SetAutoHideBar = &H8
        WindowPosChanged = &H9
        SetState = &HA
    End Enum

    Public Enum AppBarStates
        AutoHide = &H1
        AlwaysOnTop = &H2
    End Enum

    Public Sub AutoHide_Toggle()

        If GetTaskbarStateAutoHide() Then
            SetTaskbarState(AppBarStates.AlwaysOnTop)
        Else
            SetTaskbarState(AppBarStates.AutoHide)
        End If
    End Sub

    Public Sub SetTaskbarState(StateOption As AppBarStates)
        'sets the Taskbar State to StateOption (AllwaysOnTop or AutoHide)

        Dim msgData As New APPBARDATA
        msgData.cbSize =  Marshal.SizeOf(msgData)
         'not necessary to use handle of Windows Taskbar, but can be found by
         'msgData.hwnd = CInt(FindWindow("Shell_TrayWnd", ""))

        'Set the State which will be requested
        msgData.lParam = StateOption

        'Ansd send the message to set this state
        SHAppBarMessage(AppBarMessages.SetState, msgData)
        'Remark on my small (1280x800) screen the desktop area remains the same, but on my larger (1080x1920) screen
        'the desktop icons are displaced when autohide is set on !!! Don't understand why (it then thinks the screen is only 800 high)
    End Sub

    Public Function GetTaskbarStateAutoHide() As Boolean
        'true if AutoHide is on, false otherwise

        Dim msgData As New APPBARDATA
        Dim ret As Integer
        msgData.cbSize =  Marshal.SizeOf(msgData)
        ' also here not necessay to find handle to Windows Taskbar

        ret = SHAppBarMessage(AppBarMessages.GetState, msgData)

        GetTaskbarStateAutoHide = CBool(ret And &H1)
    End Function

End Module

字符串

de90aj5v

de90aj5v7#

我从这段代码中创建了一个类,像这样:

public class Taskbar
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("shell32.dll")]
public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData);
public enum AppBarMessages
{
    New = 0x00,
    Remove = 0x01,
    QueryPos = 0x02,
    SetPos = 0x03,
    GetState = 0x04,
    GetTaskBarPos = 0x05,
    Activate = 0x06,
    GetAutoHideBar = 0x07,
    SetAutoHideBar = 0x08,
    WindowPosChanged = 0x09,
    SetState = 0x0a
}
[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
    public UInt32 cbSize;
    public IntPtr hWnd;
    public UInt32 uCallbackMessage;
    public UInt32 uEdge;
    public Rectangle rc;
    public Int32 lParam;
}
public enum AppBarStates
{
    AutoHide = 0x01,
    AlwaysOnTop = 0x02
}
/// <summary>
/// Set the Taskbar State option
/// </summary>
/// <param name="option">AppBarState to activate</param>
public void SetTaskbarState(AppBarStates option)
{
    APPBARDATA msgData = new APPBARDATA();
    msgData.cbSize = (UInt32)Marshal.SizeOf(msgData);
    msgData.hWnd = FindWindow("System_TrayWnd", null);
    msgData.lParam = (Int32)(option);
    SHAppBarMessage((UInt32)AppBarMessages.SetState, ref msgData);
}
/// <summary>
/// Gets the current Taskbar state
/// </summary>
/// <returns>current Taskbar state</returns>
public AppBarStates GetTaskbarState()
{
    APPBARDATA msgData = new APPBARDATA();
    msgData.cbSize = (UInt32)Marshal.SizeOf(msgData);
    msgData.hWnd = FindWindow("System_TrayWnd", null);
    return (AppBarStates)SHAppBarMessage((UInt32)AppBarMessages.GetState, ref msgData);
}
}

字符串
问题是当我在表演的时候

taskbar.SetTaskbarState(Taskbar.AppBarStates.AlwaysOnTop);


taskbar.SetTaskbarState(Taskbar.AppBarStates.AutoHide);


我的开始按钮不再激活(我无法打开开始菜单,点击它不会导致一切)。我使用的是Windows 10。有人知道解决方案吗?

57hvy0tb

57hvy0tb8#

这是一个用C++实现的切换示例。在Windows 11 23H2上测试。

#include <Windows.h>
#include <WinUser.h>
#include <shellapi.h>

bool getTaskbarAutohideState()
{
    APPBARDATA msgData{};
    msgData.cbSize = sizeof(msgData);
    msgData.hWnd = FindWindow("System_TrayWnd", nullptr);
    LPARAM state = SHAppBarMessage(ABM_GETSTATE, &msgData);
    return state & ABS_AUTOHIDE;
}

void setTaskbarAutohide(bool enabled) 
{
    APPBARDATA msgData{};
    msgData.cbSize = sizeof(msgData);
    msgData.hWnd = FindWindow("System_TrayWnd", nullptr);
    msgData.lParam = enabled ? ABS_AUTOHIDE : ABS_ALWAYSONTOP;
    SHAppBarMessage(ABM_SETSTATE, &msgData);
}

void toggleTaskbarAutohide()
{
    setTaskbarAutohide(!getTaskbarAutohideState());
}

int main(int argc, char const *argv[])
{
    toggleTaskbarAutohide();

    return 0;
}

字符串
完整的Github repo在这里构建二进制here

相关问题