2

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?

Community
  • 1
  • 1
Abluescarab
  • 537
  • 8
  • 25

1 Answers1

2

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.

Community
  • 1
  • 1
Abluescarab
  • 537
  • 8
  • 25
  • Not good: "Start menu" won't match on other translation of Windows – Onkelborg Mar 05 '13 at 12:38
  • @Onkelborg I'm assuming that I have to reference the handle directly, then? Will that stay the same between translations? – Abluescarab Mar 05 '13 at 12:39
  • I can't guarantee anything, but I can guarantee that the code above will fail. Rule of thumb: strings that are displayed to the user are subject to translation, but strings that aren't won't be touched, ex. a class name. DV2ControlHost will probably not be changed due to translation, but "Start menu" will. You'll need to find other unique identifications apart from visible strings. – Onkelborg Mar 05 '13 at 12:46
  • @Onkelborg This is almost proving to be more trouble than it's worth... though I'm determined to find some way to do this. – Abluescarab Mar 05 '13 at 12:48
  • Well, I don't know who your users are, but if the use something else then english Windows then you have to find some other way, or just skip the feature. – Onkelborg Mar 05 '13 at 12:58
  • @Onkelborg I don't generally translate my programs, but the audience is basically anyone that wants the program, meaning that I'd like to support anyone. I could just do a folder that displays in a dropdown. – Abluescarab Mar 05 '13 at 13:03
  • It's irrelevant if you translate your program, it's the translation of Windows that's relevant. That's out of your control, but it will be considered a bug among your users if your program doesn't work just because they aren't using english Windows. – Onkelborg Mar 05 '13 at 21:26