박세범 2011. 9. 4. 17:07
I just started to study WinAPI... Actually, I didn't know how Windows applications work,
now I understand sort of(less than 1% though).
Below is what I've practiced.

Input, output, handling resources.


#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);
HINSTANCE g_hInst;
LPCTSTR lpszClass=TEXT("Menu");

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpszCmdParam, int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst = hInstance;            // ??
    HACCEL hAccel;

    hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(ID_TEST_MENU1));


   

    WndClass.cbClsExtra =0;
    WndClass.cbWndExtra = 0;
    WndClass.hbrBackground= CreateSolidBrush(RGB(0,0,255));
        //(HBRUSH)GetStockObject(BLACK_BRUSH);
    WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc = WndProc;
    WndClass.lpszClassName = lpszClass;
    WndClass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);

    hWnd = CreateWindow(lpszClass, TEXT("Sebeom's API"), WS_OVERLAPPEDWINDOW | WS_VSCROLL,
        CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL,(HMENU)NULL,hInstance,NULL);
    ShowWindow(hWnd,nCmdShow);

    while(GetMessage(&Message,NULL,0,0))
    {
        if(!TranslateAccelerator(hWnd,hAccel,&Message))
        {
            TranslateMessage(&Message);
            DispatchMessage(&Message);
        }

    }

    return (int)Message.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    //static TCHAR str[256];
    static int x=100, y=100;
    static BOOL bNowDraw = FALSE;
    static int len;
    static int index = 0;
    SYSTEMTIME st;
    static TCHAR sTime[128];
    static TCHAR *storage[256] = {TEXT("Hello"), TEXT("HIHIHIHI")};
    static TCHAR str[256];

    switch(iMessage)
    {
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case ID_TEST_MENU1:
                MessageBox(hWnd,TEXT("First menu has beeon chosen."), TEXT("Menu Demo"), MB_OK);
            case ID_TEST_MENU2:
                lstrcpy(str, TEXT("you chose Menu 2"));
        }

    case WM_CREATE:
        SetTimer(hWnd, 1, 2000, NULL);
        SendMessage(hWnd,WM_TIMER,1,0);
        return 0;
   
    case WM_TIMER:
//        GetLocalTime(&st);
//        wsprintf(sTime, TEXT("지금 시간은 %d:%d:%d입니다"), st.wDay, st.wHour, st.wMinute);
        lstrcpy(str,*(storage+index));
        index++;
        x += 100;
        y += 100;
        InvalidateRect(hWnd,NULL,TRUE);
        return 0;

    case WM_CHAR:
        if((TCHAR)wParam == VK_BACK)
        {
            MessageBox(hWnd,TEXT("haha"),TEXT("haha"),NULL);
           
        }
        return 0;

    case WM_LBUTTONDOWN:
        x= LOWORD(lParam);
        y= HIWORD(lParam);
        bNowDraw = TRUE;
   
        return 0;

    case WM_MOUSEMOVE:
        if(bNowDraw == TRUE)
        {
            hdc = GetDC(hWnd);
            MoveToEx(hdc, x, y, NULL);
            x= LOWORD(lParam);
            y= HIWORD(lParam);           
            LineTo(hdc, x, y);
            ReleaseDC(hWnd,hdc);
        }
        return 0;

    case WM_LBUTTONUP:
        bNowDraw = FALSE;
        return 0;

    case WM_PAINT:
        hdc = BeginPaint(hWnd,&ps);
        TextOut(hdc,x,y,str,lstrlen(str));        // to display the string that I will write down.
        EndPaint(hWnd,&ps);
        return 0;
       
    case WM_DESTROY:
        PostQuitMessage(0);
        KillTimer(hWnd,1);
        return 0;

    /*case WM_LBUTTONDOWN:
        hdc=GetDC(hWnd);
        TextOut(hdc,100,100,TEXT("Beautiful Korea"), 15);
        ReleaseDC(hWnd,hdc);
        return 0;
    }*/

/*
    case WM_PAINT:
        hdc = BeginPaint(hWnd,&ps);
//        DrawText(hdc,str,-1,&rt,DT_CENTER | DT_WORDBREAK);

//        MoveToEx(hdc,300,300,NULL);
//        LineTo(hdc,400,400);
        TextOut(hdc,100,100,str,lstrlen(str));
        EndPaint(hWnd,&ps);
        return 0;
//        MessageBox(hWnd,TEXT("그대는 찹쌀 떡"),TEXT("Jegal"),MB_OK);
        //TextOut(hdc,100,100,TEXT("Seeom"),5);
        //wsprintf("Hello %d", 10);
        //EndPaint(hWnd,&ps);

        */

    }

    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
    // it handles the rest of all cases like double click, quit stuff like that
}