Jump to content

set window placement of current app window


Recommended Posts

Hi. I'm programming in c++ win32 and I need some code that will take the current window app that is on the screen and set placement of it to certain x, y coordinates on the screen. (please no MFC)

Thanks and please be specific.

Link to comment
Share on other sites


Thanks. Use setwindowpos , you mean?

I am using this now:

It gives me errors saying:

C:\Program Files\Microsoft Visual Studio\MyProjects\get pixel win32\getpixel7.cpp(18) : error C2059: syntax error : 'constant'

C:\Program Files\Microsoft Visual Studio\MyProjects\get pixel win32\getpixel7.cpp(19) : error C2059: syntax error : 'constant'

Error executing cl.exe.

Here is the code:

const char* caption = "Poker";
HWND hwnd = FindWindow(NULL, caption);


BOOL SetWindowPos( HWND hWnd,
   HWND HWND_TOP,
   100,
   100,
   2,
   2,
   UINT SWP_NOSIZE
);

How do you get the handle? Am I doing it right?

Link to comment
Share on other sites

If you didn't keep track of the HWND variable when you created the window, using CreateWindow or CreateWindowEx, then yes you can use FindWindow, but that is a mediocre way of doing it. Better to track the variable like so:

hwnd = CreateWindowEx(NULL, ClsName, WndName, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,
 CW_USEDEFAULT, WndWid, WndHgt, NULL, NULL, Instance, (void*) this)

This way, you always have the TOP window handle in case you need it... If you did it this way, use the variable you used instead.

BOOL SetWindowPos( HWND hWnd,

HWND HWND_TOP,

100,

100,

2,

2,

UINT SWP_NOSIZE

);

You're not using the function properly...

First, the 'BOOL' word lets you know that the 'SetWindowPos' function will return a true or false when it has actually been called. You don't need to write this when calling it.

Second, When you call a function, you don't include the DATA TYPE declaration... so, you can remove the HWND in front of the first & second parameters, along with removing the 'UINT' from the final parameter.

Your final result should resemble something of this nature:

SetWindowPos(hwnd, HWND_TOP, 100, 100, 2, 2, SWP_NOSIZE);

However, you may want to review those numbers, because your function will create the window offset by 100 vertically & Horizontally, but it would only have a width & height of 2 pixels.... Is that what you wanted?

Link to comment
Share on other sites

Thanks for the reply. The SWP_NOSIZE should make it ignore the height and width numbers (the 2's). This child window that I am trying to move wasn't created by me. It is an application.

Right now, I'm having trouble getting a handle on the child window. The hwnd or whatever. Can you help?

Basically, all I need to do is get a handle on the current child window on the screen just below my console program and move it over to the left and up. Thanks.

Link to comment
Share on other sites

It's a child window? And you didn't create the Window... you're just trying to manipulate it?

You need to use 'FindWindow' along with 'FindWindowEx'...

Use 'FindWindow' to retrieve the HWND of the TOP WINDOW or PARENT window. You will need to know the className or WindowName. The preferred method is the className, as it is unique..., but if you didn't make the window, you won't know that. SO use the PARENT Window Name instead.

Once you have the PARENT HWND, use that variable within 'FindWindowEx' to locate the child Window. Once again, you're going to need either the Child's Class name or Window name. Same rules apply as the PARENT.

Then try setting the Window Position using 'SetWindowPos'.

HWND Phwnd, chwnd;
Phwnd = FindWindow(NULL,"CLASSNAME", "WINDOWNAME");

chwnd = FindWindowEx(Phwnd, NULL, "CLASSNAME", "WINDOWNAME");

SetWindowPos(chwnd, HWND_TOP, 100, 100, 2, 2, SWP_NOSIZE);

Link to comment
Share on other sites

Thanks for the help Hearts. I finally got it working. It feels good to see your program work. I did a double take when I saw the window move.

I'm new to programming as you may have guessed so it feels good when something works especially when it is windows programming.

Actually, I'm not sure I would have gotten without your help. thanks. I like setwindowpos better than movewindow.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...