0

I am wondering how can I restore application window to specific location on the screen.

I did managed to write some code, but somehow I do have a feeling that there must be and easier and more efficient way to do it programaticaly... as my current version is restoring window first and then moving it to required location. I would like to restore it without being forced to move it afterwards.

my code:

public void Send()
    {
        if (Process.GetProcessesByName("report").Any())
        {
            Process proc = Process.GetProcessesByName("report").First();

            if (proc != null)
            {
                IntPtr pointer = proc.MainWindowHandle;
                SetForegroundWindow(pointer);
                SendMessage(pointer, WM_SYSCOMMAND, SC_RESTORE, 0);
                Program.MoveWindow(pointer, 0, 0, 100, 100, true);

            }
        }

        // do something
  }

of course this is followed with :

        [DllImport("user32.dll")]
    static extern int SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    internal static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
    static Int32 WM_SYSCOMMAND = 0x0112;
    static Int32 SC_RESTORE = 0xF120;

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

I would be very grateful for further solutions and explanation.

Arcadio R
  • 21
  • 3
  • `I do have a feeling that there must be and easier and more efficient way to do it` - Not really, no. – Visual Vincent Dec 14 '16 at 16:03
  • Your current code is confirmed [**by this answer**](http://stackoverflow.com/a/3127320/3740093). – Visual Vincent Dec 14 '16 at 16:08
  • @VisualVincent Thank you for your answer. however is there maybe a way to set up specific x & y coordinates to on start method? Let's say start app and run it in left bottom corrner or right top corrner? – Arcadio R Dec 14 '16 at 17:27
  • This is the standard way of moving a window using the Windows API. Well, sort of. You should replace the `SendMessage` and `MoveWindow` calls with a single call to [`SetWindowPos`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633545.aspx). The x, y, width, and height parameters are the same as `MoveWindow`, but the `uFlags` parameter allows you to restore the window (or do whatever else you want). Using `SendMessage` here is hacky and wrong. – Cody Gray - on strike Dec 14 '16 at 17:31
  • As far as displaying the window at a certain position when it starts up, that's up to the process that displays the window. It makes that decision. Do you control that process? Can you modify its code? – Cody Gray - on strike Dec 14 '16 at 17:31
  • @CodyGray This is 3rd party application that has been created at work. I am trying to make my life easier, but unfortunately I do not have access to it's code. This is just simple report that is pulling some data from DB and displays it. It's getting quite annoying as results of this report are being shown in kind of pop up window which is disrtupting from other tasks and it's stilling it's focus. I just want it to be shown in specific location on screen which will allow me to see what's going on, but without steeling focus from my current window that I am working on. – Arcadio R Dec 14 '16 at 17:53
  • Thanks for suggestion, I will have a read about SetWindowPos – Arcadio R Dec 14 '16 at 17:53
  • If you want it to display without stealing focus, why are you calling `SetForegroundWindow`? – Cody Gray - on strike Dec 14 '16 at 17:54
  • this is because I would like to bring focus and restore window when I want it to, where i want it to. At the moment all that is being done automaticaly. Once report is started and result ready to view this is showing the window with all results and set its focus to this window. I have managed to keep it minimized in taskbar. What my idea is to create notification bar that is going to show when report is ready and will restore and show that data only when I click on this notification. Not earlier, not later. This will bring focus back to that report and will allow me to carry on with my job. – Arcadio R Dec 14 '16 at 18:11
  • @CodyGray I hope that make sense. – Arcadio R Dec 14 '16 at 18:11

0 Answers0