Trying to enable.disable auto-hide on taskbar, code together but not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Trying to enable.disable auto-hide on taskbar, code together but not working

  1. #1
    Join Date
    Nov 2013
    Posts
    3

    Trying to enable.disable auto-hide on taskbar, code together but not working

    Background: I'm using VS 2010, dotNet 4.0 -- and this is literally my first day programming in c#. I'm intermediate in vb6 but haven't coded in awhile so please use layman's terms. I've been at this for hours before deciding to finally ask on a forum.

    Everything seems to be triggering except that the taskbar is not actually becoming hidden. Don't mind the HelloWorld namespace as I was following some tutorials earlier.

    Code:
    //Int can be used only on 32bit systems when using pointers while IntPtr can be used for all. IntPtr is safer with unknown lengths
    //IntPtr should only be needed with 64bit apps but I'm using it in this case just as practice
    //UInt, from my understanding, will not properly store negative values. It also will not properly raise error codes - bad!
    
    //using System.Collections.Generic; //these came with the app and im not even sure if I need them.
    //using System.Linq;                //must research this later.
                                        
    
    using System; //for lots of stuff, I'm just not sure to what extent yet
    using System.Text; //for stringbuilder
    using System.Runtime.InteropServices; //for dllimport
    //using System.Diagnostics; //debug I was using this for something but I forgot what.
    
    namespace HelloWorld
    {
        class Program
        {
    
            [DllImport("user32.dll")]
            private static extern IntPtr FindWindow(string className, string windowText);
            //used to find my consoles window as well any other specific window
            [DllImport("user32.dll")]
            private static extern int ShowWindow(IntPtr hwnd, int command);
            private const int SW_HIDE = 0;
            private const int SW_SHOW = 1;
            //completely hides or shows a window
    
            [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
                private static extern IntPtr GetForegroundWindow();
            //gets the handle of the most foreground window
            [DllImport("user32.dll", EntryPoint = "GetWindowText",
                ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
                private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
            //gets text from a window handle
    
    
            [StructLayout(LayoutKind.Sequential)]
            public struct APPBARDATA
            {
                public int cbSize;
                public IntPtr hWnd;
                public int uCallbackMessage;
                public int uEdge;
                //public Rectangle rc;
                public int lParam;
            }
            public enum AppBarStates
            {
                AutoHide =
                0x00000001,
                AlwaysOnTop =
                0x00000002
            }
            //data structure for setting autohide or show on taskbar
            [DllImport("shell32.dll")]
            private static extern int SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
            public enum AppBarMessages
            {
                New =
                0x00000000,
                Remove =
                0x00000001,
                QueryPos =
                0x00000002,
                SetPos =
                0x00000003,
                GetState =
                0x00000004,
                GetTaskBarPos =
                0x00000005,
                Activate =
                0x00000006,
                GetAutoHideBar =
                0x00000007,
                SetAutoHideBar =
                0x00000008,
                WindowPosChanged =
                0x00000009,
                SetState =
                0x0000000a
            }
            //used to execute the hide or show of taskbar
    
    //        [STAThread]
            private static void Main(string[] args)
            {
                Console.Title = "Auto Taskbar Hider - Trackable Title";
                //name my application so I can find it easily later
                BeginApplication();
                //just moving it out of the way for educational purposes
    
            }
    
            private static void BeginApplication()
            {
                Boolean AlwaysOn = true;
                //used to create a constant loop
                while (AlwaysOn)
                {
                    Console.WriteLine("Starting");
                    //DEBUG
                    IntPtr hWnd = GetForegroundWindow(); //use fg for some purpose
                    //get window handle
                    StringBuilder strbTitle = new StringBuilder(255);
                    int nLength = Program.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
                    //im not sure if this would ever return negative
                    string strTitle = strbTitle.ToString();
                    //build title using handle            
    
                    SetTaskbarState(AppBarStates.AutoHide);
                    Console.WriteLine("Are ye hidden yet?");
                    System.Threading.Thread.Sleep(5000);
                    //sleep the app to preserve cpu
                    
                    //setConsoleWindowVisibility(false, Console.Title);   
                    ////debug uncomment above -- works, hides console window from view. is there a way to simply not have a console while having app run?
    
    
                    //IntPtr hwnd = FindWindow("Shell_TrayWnd", "");
                    //ShowWindow(hwnd, SW_SHOW);
                    ////debug works but doesnt allow windows to auto expand to hidden region. start menu also remains
                }
                //end>while (AlwaysOn)
    
            }
    
            //private static void setConsoleWindowVisibility(bool Visible, string ConsoleTitle)
            //{
            //    IntPtr hWnd = FindWindow(null, ConsoleTitle);
            //    //findwindow handle using caption of a window
            //    if (hWnd != IntPtr.Zero)
            //    { //if found
            //        if (!Visible)
            //            //Hide the window                   
            //            ShowWindow(hWnd, 0); // 0 = SW_HIDE               
            //        else
            //            //Show window again                   
            //            ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA          
            //    }
            //}
            ////find an application using the window text and then show or hide it
            ////debug not used atm
    
            public static void SetTaskbarState(AppBarStates option)
            {
                APPBARDATA msgData = new APPBARDATA();
                msgData.cbSize = (int)Marshal.SizeOf(msgData);
                msgData.hWnd = FindWindow("System_TrayWnd", null);
                msgData.lParam = (int)(option);
                SHAppBarMessage((int)AppBarMessages.SetState, ref msgData);
            }
            //set taskbar state to whatever you desire
    
            //public AppBarStates GetTaskbarState()
            //{
            //    APPBARDATA msgData = new APPBARDATA();
            //    msgData.cbSize = (int)Marshal.SizeOf(msgData);
            //    msgData.hWnd = FindWindow("System_TrayWnd", null);
            //    return (AppBarStates)SHAppBarMessage((int)AppBarMessages.GetState, ref msgData);
            //}
            ////supposed to get taskerbar state but I shouldn't need this
            ////debuf optional features
    
            //private static void SetTaskbarState(AppBarStates option)
            //{
            //    APPBARDATA msgData = new APPBARDATA();
            //    msgData.cbSize = (int)Marshal.SizeOf(msgData);
            //    msgData.hWnd = FindWindow("System_TrayWnd", null);
            //    msgData.lParam = (int)(option);
            //    SHAppBarMessage((int)AppBarMessages.SetState, ref msgData);
            //}
            //sets the taskbar to either autohide or not
        }
    }
    I did have a few other miscellaneous questions; but they are not super relevant.
    1) I noticed you can call ONLY static routines from within a currently static routine. Is this standard? Does being static mean it's more or less just isolated in memory/pointer or does that mean the content within remains in memory and is not cleared upon exiting the routine?
    2) Is there potentially an easier way to do this while trimming down my code?
    3) For future practices: is the use of DllImport pretty standard/effecient? Eg: API calls and what not.

    Thank you.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: Trying to enable.disable auto-hide on taskbar, code together but not working

    Most of the Taskbar settings are saved in the registry. Some of the taskbar featreus requires you to send a broadcast message to refresh explorer.exe, with the new settings.

    There is a small workaround if you only want to hide and show the taskbar without making use of the appbar structure

    you could use the SetWindowPos API with the SWP_HIDEWINDOW and SWP_SHOWWINDOW constants. The name of the Window is Shell_traywnd. So firts use FindWindow API to find the said window and then use the SetWindowPos API

  3. #3
    Join Date
    Nov 2013
    Posts
    3

    Re: Trying to enable.disable auto-hide on taskbar, code together but not working

    Thank you, I will look into this soon after I'm out of work. It also occurred to me that I was not running the Duggar as an administrator mode

  4. #4
    Join Date
    Nov 2013
    Posts
    3

    Re: Trying to enable.disable auto-hide on taskbar, code together but not working

    Quote Originally Posted by HanneSThEGreaT View Post
    Most of the Taskbar settings are saved in the registry. Some of the taskbar featreus requires you to send a broadcast message to refresh explorer.exe, with the new settings.

    There is a small workaround if you only want to hide and show the taskbar without making use of the appbar structure

    you could use the SetWindowPos API with the SWP_HIDEWINDOW and SWP_SHOWWINDOW constants. The name of the Window is Shell_traywnd. So firts use FindWindow API to find the said window and then use the SetWindowPos API
    This causes the bar to disappear but the start menu remains, like the other function I have. Unfortunately when trying to maximize a window it acts as though the bar is still there. This is mostly why I am trying to get the autohide to work. Running as administrator had no effect.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,442

    Re: Trying to enable.disable auto-hide on taskbar, code together but not working

    Please don't change the user settings in your app (the exception to this is you are in it of a corp where you are controllong all users within a network).

    Users hate when a program modifies their settings.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center