-
Static control
Code:
#include <windows.h>
const char szTitle[] = "Hello";
const char szClass[] = "HelloApp";
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam);
INT WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
LPSTR lpCmdLine, int nShowCmd)
{
HWND hwnd, static_hwnd;
MSG msg ;
WNDCLASSEX Wnd;
Wnd.cbClsExtra = 0;
Wnd.cbSize = sizeof(WNDCLASSEX);
Wnd.cbWndExtra = 0;
Wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
Wnd.hCursor = LoadCursor (NULL, IDC_ARROW);
Wnd.hIcon = LoadIcon (NULL, IDI_APPLICATION);
Wnd.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
Wnd.hInstance = hInst;
Wnd.lpfnWndProc = WndProc;
Wnd.lpszClassName = szClass;
Wnd.lpszMenuName =NULL;
Wnd.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&Wnd);
hwnd = CreateWindow (szClass, szTitle,
WS_BORDER | WS_SYSMENU,
0, 0, 200,500,NULL,NULL,hInst,NULL);
static_hwnd = CreateWindow ( "Static", "hi", SS_SIMPLE,
0, 0, 100,200,hwnd,NULL,hInst,NULL);
if (!hwnd) return 0;
if (!static_hwnd) MessageBox(hwnd, "CREATE STATIC FAILED","FAILED",NULL);
ShowWindow(static_hwnd,SW_SHOWNORMAL);
UpdateWindow(static_hwnd);
ShowWindow(hwnd, SW_SHOWMINIMIZED);
UpdateWindow(hwnd);
while ( GetMessage(&msg, NULL, 0 , 0 ) )
{
TranslateMessage(&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam )
{
switch (msg)
{
case WM_QUIT:
PostQuitMessage(0);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
How come when I compile and execute the program, the STATIC Control appears not on the main window but rather as a separate Window?????
-
Re: Static control
You must pass WS_CHILD as the window style of the child window.
Also, you should create all child windows of a particular window in that window's WM_CREATE response.