SanBen
SanBen

Reputation: 2798

Place window relative to parent

Ive got a simple modless dialog which I would like to place at the bottom right corner of the parent window. This is my first time working with windows forms in C (so please be patient if I don't directly understand).

I've tried positoning the window with SetWindowPos, but to no avail as the coordinates x and y are relative to the top left screen corner.

//hWnd is the parent window
hwndStatusBox = CreateDialog(hInst, MAKEINTRESOURCE(IDD_STATUSBOX), 
                             hWnd, svnStatusBoxProc); 

SetWindowPos(hwndStatusBox,NULL, 100, 100, 0, 0, 
             SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER);

Am I missing a function or do I have to calculate the offset myself?

I don't want to use the coordinates defined in the resource, i'd like to solve it programatically.

Upvotes: 1

Views: 2218

Answers (2)

Skizz
Skizz

Reputation: 71060

There is a function to do this:

ClientToScreen

which converts a client coordinate to a screen coordinate. For example:-

message_box_position = {ParentWidth - MessageBoxWidth, ParentHeight - MessageBoxHeight}
ClientToScreen (parent_window_handle, &message_box_position)
SetWindowPos (message_box_handle, messahe_box_position)

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 612794

For a top-level window, i.e. not a child window, the co-ordinates are indeed relative to the screen.

You will need to read the co-ordinates of the owning window, work out what offset you need, add on the offset and finally set the co-ordinates for your dialog, relative to the screen origin. Or you can call ClientToScreen, passing hWnd, and get the system to do it for you.

Upvotes: 3

Related Questions