1

I was wondering whether it is possible to resize programs other than the actual application itself. IE, I want to resize and move Word and my application to fill 70% and 30% of the screen respectively.

Private Sub MinimiseButton_Copy_Click(sender As Object, e As RoutedEventArgs) Handles MinimiseButton_Copy.Click
    Me.Left = SystemParameters.PrimaryScreenWidth - Me.Width + 14
    Me.Top = -14
    Me.Height = SystemParameters.PrimaryScreenHeight

    Dim parry As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("winword")
    Dim word As System.Diagnostics.Process = parry(0)
    SetWindowPos(word.Handle, 0, 0, 0, SystemParameters.PrimaryScreenWidth - Me.Width, SystemParameters.PrimaryScreenHeight - 28, &H10)
End Sub

<DllImport("user32.dll", CharSet:=CharSet.Auto)> Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, W As Integer, H As Integer, uFlags As UInteger) As Boolean
End Function
JosephGarrone
  • 4,081
  • 3
  • 38
  • 61
  • Should Word be already be loaded in memory when your application executes, or do you plan to invoke word from your process? If you are invoking word via automation then it should be painless to place it where you want. If it is alraedy running you could still use automation but you may want to look at how to get the HWnd of MS Word. – Ross Bush Dec 19 '12 at 05:22
  • It would already be running. If not, it would start it up. – JosephGarrone Dec 19 '12 at 05:26
  • Thats what I'm trying to do. I have no idea how to get the HWND of Word. It just has to be the first instance of word. – JosephGarrone Dec 19 '12 at 05:28
  • I did something similiar using dde or ole containers a long time back, I created form that contained a word ole objects and tied into sink events like save and close. I am sure that the VS2010 word templates contain similiar control mechanisms. I can not answer about sizing as I have not used those projects. – Ross Bush Dec 19 '12 at 05:40
  • Here is one for you using FindWindow and SetWindowLong or SetWindowPos...http://forums.codeguru.com/showthread.php?3225-How-to-get-the-HWnd-of-Microsoft-Word . If you get a handle you "might" be able to setWindowPos() – Ross Bush Dec 19 '12 at 05:42
  • This one deals with window on top - Hope it helps:)http://forums.devx.com/showthread.php?169690-Open-always-on-top-Form-in-MS-Word-using-VBA-script – Ross Bush Dec 19 '12 at 05:51
  • Thanks for your responses, I now can get the Handle...I just don't know how to send a SetWindowPos...Updating question to include current code. – JosephGarrone Dec 19 '12 at 05:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21362/discussion-between-asryael-and-ltn) – JosephGarrone Dec 19 '12 at 05:54

1 Answers1

3

That is indeed possible, try this function:

[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref Rectangle rectangle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int WPARAM, int LPARAM);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int, Y, int cx, int cy, uint uFlags);

public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_NEXTWINDOW = 0xF040;
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
public static readonly IntPtr HWND_TOP = new IntPtr(0);
public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
public const UInt32 TOPMOST_FLAGS = 0x0002 | 0x0001;

Public void resisezeWindow(String procesname, int Width, int Height, Boolean bringtofront)
{
    foreach (Process proc in Process.GetProcesses())
    {
        IntPtr id = proc.MainWindowHandle;
        Rectangle rect = new Rectangle();
        GetWindowRect(id, ref rect);
        if (proc.MainWindowTitle.Contains(procesname))
        {
            PostMessage(proc.Handle, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
                    MoveWindow(id, 0, 0, Width, Height, true);
                    if(bringtofront) SetWindowPos(id, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
                    proc.Refresh();
        }
    }
}

If there is a problem, please notify me!

Quispie
  • 948
  • 16
  • 30
  • I am not quite sure I understand how I would use this to display Word and my application...Could you possibly show how you would do this using the arguments from my edit? If not, that's fine. – JosephGarrone Dec 19 '12 at 08:49
  • Open the taskmanager on the tab Applications. You will see a list of all the open windows. Set procesname on 'Microsoft Word' and it finds all the Word windows. – Quispie Dec 19 '12 at 09:31
  • I guess this will sort it for you, keep me posted! – Quispie Dec 19 '12 at 11:13
  • Mate, you are a bl**dy legend!!! I have a duplicate question of this (Not really termed correctly) if you go an post this on there, I'll mark as answer. One last question, how would I show the MS Word window if it is minimized to taskbar? Would it be `ShowWindow`? – JosephGarrone Dec 19 '12 at 11:23
  • The function as it is now, will do that already for you, set `bringtofront = true;` – Quispie Dec 19 '12 at 12:48
  • This causes Word to always remain TOP_MOST, should I not then call `SetWindowPos(id, HWND_TOP,......)` – JosephGarrone Dec 19 '12 at 22:38
  • The project I working on is using this code and had a focus problem. This was caused by the TOP_MOST flags. It not only put it on top of the Z index it sets the focus to this window too. You can change it to `HWND_NOTOPMOST = new IntPtr(-2);` or `HWND_TOP = new IntPtr(0);` or `HWND_BOTTOM = new IntPtr(1);` But I think in this case that HWND_TOP the better one is indeed. – Quispie Dec 20 '12 at 07:55
  • Thanks, I worked that out. Also found that the reason it wasn't being displayed from being minimised was due to me targeting `proc.Handle` instead of `proc.MainWindowHandle`. It's all working now! Thanks again for your help! – JosephGarrone Dec 20 '12 at 08:15