6 Replies - 282 Views - Last Post: 30 October 2017 - 05:49 PM Rate Topic: -----

#1 jb559atw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-October 17

Using movewindow() doesnt move window all the way to 0,0?

Posted 30 October 2017 - 11:26 AM

I recently decided to learn C# after using C++ for years. So I was experimenting trying to get the hang of doing stuff from C++. Well I wanted my console app to start at the top left of the desktop so that it wouldn't hang off the screen and have to be manually moved. Since C# doesn't let you use WinAPI functions directly I looked up how to import the DLLs to use the native code. The movewindow() function was in User32.dll so I put that in a separate class. I got all that working and got the window to move. The problem was that when I used 0,0 as where to move the top left of the window to it didn't quite get there. It appears to move it all the way up but not quite all the way left.

Any help or ideas are appreciated. And thanks in advance for your time.

I tried attaching a screenshot but it keeps saying the server returned an error during upload.

Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            User32.RECT WindowRect = new User32.RECT();
            IntPtr ConsoleWindowHandle = Kernel32.GetConsoleWindow();

            User32.GetWindowRect(ConsoleWindowHandle, out WindowRect);

            Int32 WindowWidth = WindowRect.right - WindowRect.left;
            Int32 WindowHeight = WindowRect.bottom - WindowRect.top;

            User32.MoveWindow(ConsoleWindowHandle, 0, 0, WindowWidth, WindowHeight, false);

            Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
            Console.SetBufferSize(Console.LargestWindowWidth, Console.LargestWindowHeight);

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.SetCursorPosition(5, 10);
            Console.Write("Hello World!");
            Console.ReadKey();
        }
    }
}




Kernel32.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    public static class Kernel32
    {
        [DllImport("Kernel32.dll")]
        public static extern IntPtr GetConsoleWindow(); 
    }
}



User32.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    public static class User32
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct RECT
        {
            public Int32 left;
            public Int32 top;
            public Int32 right;
            public Int32 bottom;
        }

        [DllImport("User32.dll")]
        public static extern Boolean GetWindowRect(IntPtr hWnd, out RECT lpRect);

        [DllImport("User32.dll")]
        public static extern Boolean MoveWindow(IntPtr hWnd, Int32 X, Int32 Y, Int32 nWidth, Int32 nHeight, Boolean bRepaint);

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Using movewindow() doesnt move window all the way to 0,0?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5896
  • View blog
  • Posts: 20,132
  • Joined: 05-May 12

Re: Using movewindow() doesnt move window all the way to 0,0?

Posted 30 October 2017 - 12:17 PM

Yes, there is a forum bug that is preventing direct uploads. If you have access to a picture hosting site like imagur, you can upload the picture there, and insert a picture link here.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5896
  • View blog
  • Posts: 20,132
  • Joined: 05-May 12

Re: Using movewindow() doesnt move window all the way to 0,0?

Posted 30 October 2017 - 12:33 PM

If you run Spy++, you'll see the the window is at 0,0. I suspect what is happening is that there is invisible non-client (and invisible) padding around the console window on Windows 10. If you tried this on Windows 7, or older versions of the OS that actually drew full chrome around the console windows, I think you'll find that the window placement will look correct.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5896
  • View blog
  • Posts: 20,132
  • Joined: 05-May 12

Re: Using movewindow() doesnt move window all the way to 0,0?

Posted 30 October 2017 - 01:01 PM

A quick "hack" would be to determine what is the client area dimensions as opposed to the window dimensions, and then offset by that much more.
Was This Post Helpful? 0
  • +
  • -

#5 jb559atw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-October 17

Re: Using movewindow() doesnt move window all the way to 0,0?

Posted 30 October 2017 - 01:52 PM

@Skydiver:
1: I don't use any of those picture sites, but it sounds like you have a good idea of what it looks like.
2: That there is some invisible padding might be it. After the WindowMove I had it output the values of WindowRect.left and WindowRect.top and both came back as 0. So as far as the program was concerned it was at 0,0. Next I'll add the ShowWindow() function to the User32 class and then have the window maximize and then see what it says the left value is. I'm guessing it will still say 0 but I'll see. I had even tried using SetWindowPos() with the flag set to NO_SIZE and that was even worse. It was even more to the right and lower as well.
3: I had went back and tried -10 as the x value and it moved it over but just slightly off the edge. So once I get the ShowWindow() done and test it when its maximized to see what I should use rather than -10. I don't want to just eyeball it. But as far as using the client area, isn't that just the output area without the frame or title bar? And isn't that relative to the actual window? So how would knowing its size help me in adjusting the position of the actual window?

And thanks for your help and ideas so far.
Was This Post Helpful? 0
  • +
  • -

#6 jb559atw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-October 17

Re: Using movewindow() doesnt move window all the way to 0,0?

Posted 30 October 2017 - 02:02 PM

@Skydiver
Ok when its maximized left and top both comeback as -8. I wonder why the screen origin is -8 rather than 0? When I tried to do this before with C++ it was all fine. But for some reason through C# its acting weird. Well its nothing new. I'm used to writing code that when you look at it your like yep this is what it should do and then for some weird reason it doesn't do it lol. Well now we at least know about this weird thing.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5896
  • View blog
  • Posts: 20,132
  • Joined: 05-May 12

Re: Using movewindow() doesnt move window all the way to 0,0?

Posted 30 October 2017 - 05:49 PM

The client rectangle contains has the top left corner and the bottom right corner. Obviously subtracting the top left from the bottom right gives you the area as you surmised, but the coordinates of the corners give you more information.

You will find that the top left corner has an x coordinate of 8. As you discovered, you needed -8 for the left edge of the console client area to line up with the left edge of the screen.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1