본문 바로가기

Windows/Windows API

프로퍼티 이용하기 (SetProp)

#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK ChildProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="WndProp";

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
         ,LPSTR lpszCmdParam,int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst=hInstance;
    
    WndClass.cbClsExtra=0;
    WndClass.cbWndExtra=0;
    WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc=(WNDPROC)WndProc;
    WndClass.lpszClassName=lpszClass;
    WndClass.lpszMenuName=NULL;
    WndClass.style=CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);

    WndClass.hCursor=LoadCursor(NULL,IDC_CROSS);
    WndClass.lpfnWndProc=(WNDPROC)ChildProc;
    WndClass.lpszClassName="ChildCls";
    WndClass.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
    RegisterClass(&WndClass);

    hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
        NULL,(HMENU)NULL,hInstance,NULL);
    ShowWindow(hWnd,nCmdShow);
    
    while(GetMessage(&Message,0,0,0)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return Message.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    int x,y;
    switch(iMessage) {
    case WM_CREATE:
        // Create child window
        for (x=0;x<300;x+=100)
            for (y=0;y<300;y+=100) {
                CreateWindow("ChildCls",NULL,WS_CHILD | WS_VISIBLE,
                    x,y,100,100,hWnd,(HMENU)NULL,g_hInst,NULL);
            }
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}

LRESULT CALLBACK ChildProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    int Prop;
    switch(iMessage) {
    case WM_CREATE:
        /*
        The SetProp function adds a new entry or changes an existing entry in
        the property list of the specified window. The function adds a new entry
        to the list if the specified character string does not exist already in
        the list. The new entry contains the string and the handle. Otherwise,
        the function replaces the string's current handle with the specified handle.
        */
        SetProp(hWnd,"PROP_STAT",(HANDLE)0);
        return 0;
    case WM_LBUTTONDOWN:
        Prop=(int)GetProp(hWnd,"PROP_STAT");
        SetProp(hWnd,"PROP_STAT",(HANDLE)!Prop);
        InvalidateRect(hWnd,NULL,TRUE);
        return 0;
    case WM_PAINT:
        hdc=BeginPaint(hWnd, &ps);
        Prop=(int)GetProp(hWnd,"PROP_STAT");
        if (Prop)
            Ellipse(hdc,0,0,100,100);
        else
            Rectangle(hdc,0,0,100,100);
        EndPaint(hWnd, &ps);
        return 0;
    case WM_DESTROY:
        // 반드시 지울때 삭제 해야된다.
        RemoveProp(hWnd,"PROP_STAT");
        return 0;
    }
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}

'Windows > Windows API' 카테고리의 다른 글

윈도우DC (GetWindowDC)  (0) 2013.10.01
GDI PLUS  (0) 2013.10.01
WndClassEx.hIcon  (0) 2013.10.01
윈도우 크기, 사이즈 조정 (WM_SIZE)  (0) 2013.10.01
닫기 메세지 (WM_CLOSE)  (0) 2013.10.01