0

I only know a little about c# and most of my code is off google. The thing I'm trying is moving a window, I got this working by using SetWindowPos. However, I want the window partically outside the screen, but Microsoft Windows doesn't allow it. The code below is what I got so far. Of course I searched on google for the answer, and found the answer, the only thing I don't know is how to put this into my program.

Could somebody explain me how I would put this into my application?

Google answer: Move a window partially off the top of the screen

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowMover
{

    static class Logic
    {

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

        public static void Main()
        {
            const short SWP_NOSIZE = 1;
            const short SWP_NOZORDER = 0X4;
            const int SWP_NOACTIVATE = 0x0010;

            Process[] processes = Process.GetProcesses("DeviceEmulator");
            foreach (var process in processes)
            {
                if (process.ProcessName == "DeviceEmulator")
                {
                    var handle = process.MainWindowHandle;
                    SetWindowPos(handle, 0, 0, 0, -100, -100, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
                }
            }
        }
    }
}
Community
  • 1
  • 1
  • Your code doesn't make sense, you are trying to give the window a negative size. That's of course not possible. And then also specify SWP_NOSIZE to tell SetWindowPos() that it should not change the size. Change the position instead. Don't pass (0, 0), that puts it in the upper left corner of the screen, by definition not partially off the screen. – Hans Passant Feb 22 '13 at 02:05
  • Thanks, that's at least one mystery solved! – user2097402 Feb 22 '13 at 10:19

1 Answers1

0
  1. Move the constants to class scope and add public modifier for them.

  2. Remove Main() from the Logic class

  3. Save it in a .cs file

  4. Add the file to your project.

  5. Suppose your main form class was Form1.cs, add a line at the top

    using WindowMover;

  6. Double click Form1 in form designer, it would add a Load event handler for you and enters code editing mode

  7. Call SetWindowPos with this.Handle, and latter parameters are depend on your requirement

Ken Kin
  • 4,503
  • 3
  • 38
  • 76