Search code examples
c#startmenu

Open the start menu right next the program?


I've been researching how to open the Start Menu programmatically, but the problem is that I'd like it to open it right next to the application I'm creating.

I'm making a toolbar at the top of the screen with a button to open the start menu, but I'd like the Start Menu to open right underneath the button rather than near the taskbar. I'm using the code supplied in the answer to this question, which only sends the required keypress (LWin) to open the Start Menu.

Is this possible in C#? If so, how can I do it?


Solution

  • EDIT: Apparently, this only works on English versions of Windows, due to "Start menu" being different in each translation. This will still work, though (as long as Windows is installed in English).

    I got it! This works, though I don't know how pretty it is:

    public partial class Form1 : Form {
        private void button1_Click(object sender, EventArgs e) {
            int ShowCmd = 5;
    
            MoveWindow(FindWindow("DV2ControlHost", "Start menu"), X_POS, Y_POS, WIDTH_HERE, HEIGHT_HERE, false);
            ShowWindow(FindWindow("DV2ControlHost", "Start menu"), ShowCmd);
        }
    
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
    }
    

    It's a mix of this question here on SO and this MSDN article modified for C#. Again, I'm not sure how good the code is, but it gets the job done.