CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2003
    Location
    The Dutch Mountains
    Posts
    125

    Set another application focus

    Hi,
    I'm try to set another application, outside myapplication, as focused window.

    Can someone gimmy some help? I've allready tryed something like this

    System.Diagnostics.Process.GetProcessesByName("notepad");

    I don't start the application in myapplication so I can't set a handle to it.

    Greetz Otto
    Give up your guns and face the law!!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Set another application focus

    Get the hWnd to the app's top level window and pinvoke to SetForegroundWindow.

  3. #3
    Join Date
    Oct 2003
    Location
    The Dutch Mountains
    Posts
    125

    Re: Set another application focus

    Thanks, I worked it all out.

    //Import the SetForeground API to activate it
    [DllImport("User32.dll")]
    private static extern IntPtr SetForegroundWindow(int hWnd);

    public MyProjectFunctions()
    {


    }
    public void SetWindowFocus(string processName)
    {
    Process[] pname = Process.GetProcessesByName(processName);
    if (pname.Length > 0)
    {
    foreach (Process processes in pname)
    {
    if (processes.ProcessName.Equals(processName))
    {
    SetForegroundWindow((int)processes.MainWindowHandle);
    }
    }
    }

    }
    Give up your guns and face the law!!

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Set another application focus

    Just to add my 2 cents.

    For some inexplicable reason I have had problems with using only SetForeGroundWindow. I also then made use of the BringWindowToTop API, which assisted in bringing the necessary window to the front.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured