如何让button随着父窗口的变化而变化 win32
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
算好按钮长宽与窗口客户区长宽 的比例系数 当窗口调整时 重新获取窗口 宽高 与比例系数相乘计算出此时按钮变化到多大的长宽,用这个算出来的按钮的新的长宽值 去调整按钮的窗口大小
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
//wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("button resize when window resize"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
400,//CW_USEDEFAULT, // initial x size
300,//CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hBtn;
RECT rt;
static float btnw_div_clientw;/////
static float btnh_div_clienth;/////
int w;
int h;
switch (message)
{
case WM_CREATE:
GetClientRect(hwnd, &rt);/////
hBtn = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("BUTTON"), TEXT("按钮"),
WS_CHILDWINDOW, (rt.right-rt.left-100)/2, (rt.bottom-rt.top-60)/2, 100, 60,
hwnd, NULL, NULL, NULL);
ShowWindow(hBtn, SW_SHOW);
btnw_div_clientw = 100*1.0/(rt.right-rt.left);/////
btnh_div_clienth = 60*1.0/(rt.bottom-rt.top);/////
return 0 ;
case WM_SIZE:
GetClientRect(hwnd, &rt);////////
w = (int)((rt.right-rt.left)*btnw_div_clientw);////////
h = (int)((rt.bottom-rt.top)*btnh_div_clienth);///////
MoveWindow(hBtn,(rt.right-rt.left-w)/2, (rt.bottom-rt.top-h)/2, w, h,TRUE); ////
return 0;
case WM_DESTROY:
DestroyWindow(hBtn);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}