2011-04-10 75 views
4

如何在運行時單擊按鈕時調整全局hwnd變量的大小?在運行時在c + + Windows API調整窗口大小?

或者只是在運行時調整窗口大小的任何方式。 即

HWND hwnd; //global 
int buttonid = 250; // an id for a button 
//also global 


int WINAPI wWinMain(/*blah blah blah */) { 


//blah blah blah 

hwnd = CreateWindowEx(
    0, 
    L"WindowClass", 
    L"Window", 
    WS_OVERLAPPEDWINDOW, 
    CW_USEDEFAULT, CW_USEDEFAULT, 
    300, 275, 
    NULL, 
    NULL, 
    hInstance, 
    NULL 
    ); 

    HWND mybutton = CreateWindow(
    L"BUTTON", 
    L"Button", 
    WS_VISIBLE | WS_CHILD | WS_TABSTOP, 
    14, 13, 
    250, 200, 
    hwnd, 
    (HMENU)buttonid, 
    hInstance, 
    NULL 
    ); 

//blah blah blah 

} 


LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lparam) { 

switch(uMsg) { 

case WM_COMMAND: 
if(buttonid==wParam) { 
//this is where i want the code for resizing hwnd so when you click the 
//button it resizes the window 
} 



} 

} 

回答

8

MoveWindowSetWindowPos(但如果你想要做的不僅僅是調整它的大小,後者是更有益的)。

在這兩種情況下,您不僅可以指定左上角的位置,還可以指定右下角的位置,因此如果按原樣離開左上角並將底部正確的,你調整窗口的大小,而不是「移動」它。

8
SetWindowPos(yourhwnd,0,0,0,newWidth,newHeight,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE); 

或者,如果你想移動和調整,你可以使用舊的MoveWindow功能