可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Basically I want to make simple toggle program (that will be mapped to some keyboard shortcut) that set taskbar to auto-hide mode if in normal mode (and conversely, to normal show mode if in auto-hide).
Do You know how to implement it in C#? (or Win32 C++, but anything that will actually do it is fine.)
Thanks. Hope I've made myself clear.
--
I don't really want any full screen app that will overlap taskbar, only windowless program that toggles show mode and quit. I switch from auto-hide to normal view on regular basis and want to simplify it. (Using Win7.)
--
edited. For example
#include <windows.h>
int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
SetWindowPos(FindWindow(L"Shell_traywnd", NULL ), 0, 0, 0, 0, 0, 0x40);
}
will not do the trick, it only shows taskbar, which is already visible=true, but not switch it to/from auto-hide. (Same applies for 0x80.)
回答1:
The taskbar is a appbar and you can control it with SHAppBarMessage
回答2:
Here are the functions I use:
[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);
}
When the code above is implemented just set the Taskbar to autohide by: SetTaskbarState(AppBarStates.AutoHide);
Get the current state by:
AppBarStates currentState = GetTaskbarState();
回答3:
I followed @Quispie answer but it didn't worked at first in Windows 10, but gave me the foundation and source to solve it (so kudos) and also 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);
}
回答4:
Hiding the taskbar
It's a more WIN32 API related issue than C#. You can use this (needed to be translated to dot net of course) to hide the task bar.
You can use http://www.pinvoke.net to translate the WIN32 API calls to dot net.
Set auto-hide to the taskbar
You can achieve that by manipulating the registry using the keys that described here.
It should be an easy task, Good luck.
回答5:
I maked a taskbar class from this code like this:
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);
}
}
The problem is, when I'm performing
taskbar.SetTaskbarState(Taskbar.AppBarStates.AlwaysOnTop);
after
taskbar.SetTaskbarState(Taskbar.AppBarStates.AutoHide);
My start button is no more activated (i can't open the startmenu, clicking on it dosen't causes everything). I'm using Windows 10. Does anyone know a solution about that?
回答6:
This is a solution that I use: 7+ Taskbar Tweaker (http://rammichael.com/7-taskbar-tweaker). Kudos to the author!
A simple free program that makes it possible to toggle task bar auto-hide in one action - assigning it to double click or middle click on empty space of the task bar OR set it yourself (see advanced options + help, right click try icon).
This is essential for me as I use the task bar in vertical position and on a laptop I run out of space when I project to XVGA, which is frequent. And it was a real pain, so many clicks to toggle it.
It's a shame Microsoft has not developed this further since the 90's. Today, I think one option would be that it behaves as the ribbon in the office programs.
Cheers!
Marjan
回答7:
For all those who arrived here from Google and are using Windows 10, like me, the answers from @Quispie and @nicruo are OK but need an extra if
.
The reason for that is the class name differs from version to version (apparently, as I no longer have any other Windows but 10).
msgData.hWnd = FindWindow("System_TrayWnd", null);
if (msgData.hWnd == IntPtr.Zero)
msgData.hWnd = FindWindow("Shell_TrayWnd", null);