Windows/Windows API

마우스 오버 이벤트 (Mouse Over)

aucd29 2013. 10. 1. 18:50

#include <windows.h>
#include <string>

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
        HWND hWnd;
        MSG msg;
        WNDCLASS wc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
        wc.hInstance = hInstance;
        wc.lpfnWndProc = WndProc;
        wc.lpszClassName = "study";
        wc.lpszMenuName = NULL;
        wc.style = CS_HREDRAW | CS_VREDRAW;

        RegisterClass(&wc);

        hWnd = CreateWindow("study",
                "Mouseover Test",
                WS_OVERLAPPEDWINDOW,
                0,
                0,
                300,
                300,
                NULL,
                NULL,
                hInstance,
                NULL);

        ShowWindow(hWnd,nCmdShow);
        UpdateWindow(hWnd);

        while(GetMessage(&msg,0,0,0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }

        return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
        static RECT R, r[4];
        HBRUSH brush;
        HDC hdc;
        PAINTSTRUCT ps;
        COLORREF cor[4] = {RGB(255,0,0), RGB(0,255,0), RGB(0,0,255), RGB(128,128,0)};
        int i,sel;
        static int cursel;
        POINT pt;
        switch(uMsg)
        {
        case WM_CREATE:
                break;

        case WM_PAINT:
                hdc = BeginPaint(hWnd,&ps);
                GetClientRect(hWnd,&R);
                R.left += 10;
                R.top += 10;
                R.right -= 10;
                R.bottom -= 10;


                SetRect(&r[0],R.left,R.top,R.right/2,R.bottom/2);
                SetRect(&r[1],R.right/2,R.top,R.right,R.bottom/2);
                SetRect(&r[2],R.left,R.bottom/2,R.right/2,R.bottom);
                SetRect(&r[3],R.right/2,R.bottom/2,R.right,R.bottom);

                for(i=0;i<4;i++)
                {
                        brush = CreateSolidBrush(cor[i]);
                        SelectObject(hdc,brush);
                        Rectangle(hdc,r[i].left,r[i].top,r[i].right,r[i].bottom);
                }
                
                EndPaint(hWnd,&ps);
                break;
                
        case WM_MOUSEMOVE:
                pt.x = LOWORD(lParam);
                pt.y = HIWORD(lParam);

                for(i=0;i<4;i++)
                {
                        if(PtInRect(&r[i],pt))
                        {
                                sel = i;
                                break;
                        }
                }
                if(sel != i){

                        return DefWindowProc(hWnd,uMsg,wParam,lParam);
                }
                if(cursel != sel) {
                        InvalidateRect(hWnd,&r[cursel],TRUE);
                        hdc = GetDC(hWnd);
                        SelectObject(hdc,CreateSolidBrush(RGB(255,255,255)));
                        Rectangle(hdc,r[sel].left,r[sel].top,r[sel].right,r[sel].bottom);
                        ReleaseDC(hWnd,hdc);
                }
                cursel = sel;
                
                break;


        case WM_DESTROY:
                PostQuitMessage(0);
                break;

        }
        return DefWindowProc(hWnd,uMsg,wParam,lParam);
}