Put that piece of code at the top. Probably below your <windows.h>
Then in your WM_SIZE message, call it like this:
Code:
case WM_SIZE:
{
MakeBottomRight( hwndButton );
return 0 ;
}
For reference, this is my complete code.
Code:
/*------------------------------------------------------------------------------
Demonstrates putting a button at the bottom right of a window.
Author: Dante Shamest
Date: 11th October 2005
------------------------------------------------------------------------------*/
#include <windows.h>
HWND hwndMain ;
HWND hwndButton ;
/*
* Places a control at the bottom right of it's parent.
*/
void MakeBottomRight( HWND button )
{
RECT rcClient, rcButton ;
HWND hwndParent ;
int newButtonX, newButtonY ;
if ( !IsWindow(button) )
return ;
hwndParent = GetParent(button);
if ( !IsWindow(hwndParent) )
return ;
GetClientRect( hwndParent, &rcClient );
GetClientRect( button, &rcButton );
newButtonX = rcClient.right - rcButton.right ;
newButtonY = rcClient.bottom - rcButton.bottom ;
SetWindowPos( button, NULL, newButtonX, newButtonY, 0, 0,
SWP_NOSIZE|SWP_NOZORDER );
}
LRESULT CALLBACK WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
switch(msg)
{
case WM_CREATE:
{
hwndButton = CreateWindow( "BUTTON", "My Button", WS_CHILD|WS_VISIBLE,
10, 10, 200, 22, hwnd, 0, GetModuleHandle(0),
0 );
return 0;
}
case WM_SIZE:
{
MakeBottomRight( hwndButton );
return 0 ;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
default: return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR args, int nShow )
{
WNDCLASS wc = {0};
MSG msg ;
wc.lpszClassName = TEXT( "ButtonFun" );
wc.hInstance = hInst ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
hwndMain = CreateWindow( wc.lpszClassName,TEXT("Button At Lower Right"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
0,0,300,200,0,0,hInst,0);
while( GetMessage(&msg,0,0,0) > 0 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}