1

I have a program which runs in the background, and when something happens a message box appears, I want to have it so clicking yes will switch to a specified program.

I just can't find what ClassName and CaptionName work with it, I need it to work with the game World of Warcraft.

The Window Title is "Wold of Warcraft" on Task manager its called "World of Warcraft Retail", when I inspect its' properties it says "Wow-64" Properties, on properties it says product name is "World of Warcraft" so I've tried every combination of these and nothing works. The code works if I put:
BringToFront("Notepad", "Untitled - Notepad");

So it works, I just don't know what I need for it to apply to WoW.

My code is:

    [DllImport("User32.dll")]
    public static extern Int32 SetForegroundWindow(int hWnd);
    [DllImport("user32.dll")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);

    private void BringToFront(string className, string CaptionName)
    {
        SetForegroundWindow(FindWindow(className, CaptionName));
    }

    private void Alert()
    {        
        string title = "WoW Queue Alert: Message";
        string message = "The Queue is ready to accept!";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        result = MessageBox.Show(new Form() { TopMost = true }, message, title, buttons, MessageBoxIcon.Information);
        if (result == System.Windows.Forms.DialogResult.Yes)
        {
        BringToFront("World of Warcraft Retail", "World of Warcraft");
        } 
    }

I really don't see anything special about WoW, and going by how the notepad example works the correct code should be:

            BringToFront("World of Warcraft Retail", "World of Warcraft");

Being a fullscreen program should affect it, and I can't see there being anyway Blizzard has implemented something to stop this function.

Edit: I just set the ClassName to null and works, as caption name is just the window title. No idea what the ClassName is I tried everything I could find.

bob
  • 209
  • 1
  • 2
  • 12
  • What is `FindWindow` returning? – DavidG Aug 30 '14 at 16:05
  • Error checking is *not* optional when you pinvoke winapi functions. You don't have the friendly .NET exceptions to keep you out of trouble anymore, you have to raise them yourself. – Hans Passant Aug 30 '14 at 17:29

1 Answers1

0

You should use some window inspection tool to inspect the window of World of Warcraft to get its Class Name and Window Name. A suggestion would be Spy++ if you installed Visual Studio (it comes along with it). It can help you detect the window's Class Name and Window Name, and Windows Messages sent to that window easily.

For example, the picture below shows all windows in current environment. Highlighted entry is a PowerPoint instance, which has Caption = "Microsoft PowerPoint" and ClassName = "PPTFrameClass".

spy++

Another approach will be writing a "always-inactivate" winform and call GetForegroundWindow from that form to get the window handle of the window you want.

To create an "always-inactivate" winform, simply override its CreateParams property:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams p = base.CreateParams;

        p.ExStyle |= 0x8000000; // WS_EX_NOACTIVATE - requires Win 2000 or higher

        return p;
    }
}

By overriding this, the form won't get focus even on a mouse click. Therefore, you could create a button to trigger GetForegroundWindow(). Signature of the function in C# looks like below:

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

which returns the window handle of current fore ground window.

nevets
  • 4,631
  • 24
  • 40
  • I'm using Visual Studio Express which doesn't appear to have spy++ I downloaded this: http://web.archive.org/web/20090916053057/http://www.windows-spy.com/ which is meant to do all Spy++ does but as an external program, and I can't see anything from the information displayed that is the Class/Caption Names. – bob Aug 30 '14 at 17:29
  • I edited my post to include a screen shot of spy++, did you see something like this? – nevets Aug 30 '14 at 17:41
  • I explored the link you gave, and seems the application gives the Class-Caption pair in "Windows list". Under window list, every entry reads as: Handle-ClassName-CaptionName – nevets Aug 30 '14 at 17:44