10

I open up a notepad from my program using Process.Start() but the new opened notepad covers the screen. But I do want my application to maintain its focus.

I similarly (using the same Process.Start) open up MS Excel and Word but to get focus back to my form all I need to write is:

this.Focus();

But quirk with Notepad: I open notepad (and all other processes like this)

Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.EnableRaisingEvents = true;
process.StartInfo.FileName = @"abc.log";
process.Start();

Now notepad takes the focus.

I tried these:

  1. this.Activate(), this.Focus(), needless to mention

  2. [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]  
    public static extern IntPtr SetFocus(HandleRef hWnd);
    
    {
       IntPtr hWnd = myProcess.Handle;
       SetFocus(new HandleRef(null, hWnd));
    }
    
  3. [DllImport("User32")]
    private static extern int SetForegroundWindow(IntPtr hwnd);
    
    [DllImportAttribute("User32.DLL")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    private const int SW_SHOW = 5;
    private const int SW_MINIMIZE = 6;
    private const int SW_RESTORE = 9;
    
    {
        ShowWindow(Process.GetCurrentProcess().MainWindowHandle, SW_RESTORE);
        SetForegroundWindow(Process.GetCurrentProcess().MainWindowHandle);
    }
    
  4. Another lengthier solution got from here.

All which still keeps the focus on notepad. Why is it so difficult to merely get focus to a window, that too application's own window?

EDIT: At best I can open the notepad minimized, but it still wouldn't give the focus to the form after trying all the above codes. Notepad opens minimized, but focus will be still on notepad (something that sometimes we see in windows xp) and form will be out of focused.

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368

5 Answers5

13

I tried almost everything on internet (so sure about it :)). At best I could get my form on top of all other forms, but without focus (going by @Hans Passant's method). Going by heavy blocks of codes all over, I somehow felt this aint gonna be easy. So I always used SetForegroundWindow() with chunks of other code. Never thought merely SetForegroundWindow() would do the trick.

This worked.

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

private void button1_Click(object sender, EventArgs e)
{ 
    Process process = new Process();
    process.StartInfo.FileName = @...\abc.log";
    process.Start();

    process.WaitForInputIdle(); //this is the key!!

    SetForegroundWindow(this.Handle);
}

At times this method yields in a focus on the parent form (in cases where my desired form is a modal child form of its parent form); in such cases, just add this.Focus() to the last line..

Even this worked:

Microsoft.VisualBasic.Interaction.Shell(@"notepad.exe D:\abc.log", 
                                        Microsoft.VisualBasic.AppWinStyle.NormalNoFocus);

Solution provided by here

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • This works, and answers the question as asked, but a preferable solution is to _prevent the launched process from stealing focus in the first place_ rather than the caller regaining focus after losing it. See https://stackoverflow.com/q/2121911/45375 – mklement0 Sep 15 '18 at 04:15
  • 1
    Actually, I missed that your _second_ command already does what I suggested in my previous comment, courtesy of [`Microsoft.VisualBasic.Interaction.Shell()`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.interaction.shell?view=netframework-4.7.2#Microsoft_VisualBasic_Interaction_Shell_System_String_Microsoft_VisualBasic_AppWinStyle_System_Boolean_System_Int32_) and its [`AppWinStyle`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.appwinstyle) flags. – mklement0 Sep 15 '18 at 04:32
2

If you want to start a process and focus back to the form, then start that process with minimized state, like this:

Dim psi As New ProcessStartInfo("notepad")
psi.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(psi)
Phi Bach
  • 96
  • 5
2

I had the same problem, i eventually wound up with programmatically calling alt-tab:

[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);

private void alttab()
{
     uint WM_SYSCOMMAND = 0x0112;
     int SC_PREVWINDOW = 0xF050;            

     PostMessage(Process.GetCurrentProcess().MainWindowHandle, WM_SYSCOMMAND, SC_PREVWINDOW, 0);
}

//EDIT: You should use process.MainWindowHandle instead ofcourse

hcb
  • 8,147
  • 1
  • 18
  • 17
1

Windows prevents apps from shoving their window into the user's face, you are seeing this at work. The exact rules when an app may steal the focus are documented in the MSDN docs for AllowSetForegroundWindow().

A back-door around this restriction is the AttachThreadInput() function. This code worked well, I did take a shortcut on finding the thread ID for the thread that owns the foreground window. Good enough for Notepad, possibly not for other apps. Do beware that Raymond Chen does not approve of this kind of hack.

    private void button1_Click(object sender, EventArgs e) {
        var prc = Process.Start("notepad.exe");
        prc.WaitForInputIdle();
        int tid = GetCurrentThreadId();
        int tidTo = prc.Threads[0].Id;
        if (!AttachThreadInput(tid, tidTo, true)) throw new Win32Exception();
        this.BringToFront();
        AttachThreadInput(tid, tidTo, false);
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool AttachThreadInput(int tid, int tidTo, bool attach);
    [DllImport("kernel32.dll")]
    private static extern int GetCurrentThreadId();
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks. But I'm in doubt that if BringToFront really works here. Isn't it just a measure to make the control be visible above another control? Is it not that .Activate is a better choice here? – nawfal Jan 16 '12 at 16:33
  • Also who is Raymond Chen? To approve this? :o – nawfal Jan 16 '12 at 16:34
  • Form.BringToFront() calls SetForegroundWindow(), easily seen by just trying to code. Raymond Chen is a Microsoft programmer with a popular blog. Posts answers here too, I don't want to piss him off. http://blogs.msdn.com/b/oldnewthing/ – Hans Passant Jan 16 '12 at 16:36
  • Hans this code lets my form to be shown on top of the notepad, but now the focus is neither on form nor on notepad. I tried this.Activate and this.Focus but still focus doesnt come on the app. This is a serious requirement for our client.. – nawfal Jan 18 '12 at 06:43
-1

Try this:

public partial class MainForm : Form
{
    private ShowForm showForm;

    public MainForm()
    {
        InitializeComponent();
    }

    private void showButton_Click(object sender, EventArgs e)
    {
        this.showForm = new ShowForm();
        this.showForm.FormClosed += showForm_FormClosed;
        this.showForm.Show(this);
    }

    private void showForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Focus();
    }
}
nawfal
  • 70,104
  • 56
  • 326
  • 368
Lito
  • 11