티스토리 뷰

System Programming

[system programming] __declspec(thread)

열혈허슬러 상추님 2019.07.05 00:01
190703-1
 

교육을 받으면서 노트필기 했던 내용을 날것 그대로 업로드합니다.


WS_CHILD : 부모 내에서만 동작 
WS_OVERLAPPEDWINDOW : 겹쳐지는 윈도우
    hwndChild = CreateWindow(_T("ChildWindow"),
        _T("Child Window !!"),
        WS_OVERLAPPEDWINDOW|WS_CHILD,
        100,
        100,
        300,
        300,
        hwndParent,
        (HMENU)NULL, //저번 코드 수정 NULL로 주어야 한다.
        g_hInst,
        NULL
    );
 
__declspec(thread) static int count = 0;
 
 
#include <windows.h>
#include <TCHAR.H>
 
DWORD __stdcall DrawThread(LPVOID pParam);
DWORD __stdcall UIThread(LPVOID pParam);
 
 
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg,
    WPARAM wParam, LPARAM lParam);
 
LRESULT CALLBACK ChildProc(HWND hwnd, UINT iMsg,
    WPARAM wParam, LPARAM lParam);
 
HINSTANCE g_hInst;
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpszCmdLine, int nCmdShow)
{
    g_hInst = hInstance;
 
    HWND hwnd;
    MSG      msg;
    WNDCLASS WndClass;
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    WndClass.lpfnWndProc = WndProc;
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hInstance = hInstance;
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.lpszMenuName = NULL;
    WndClass.lpszClassName = _T("Window Class Name");
    RegisterClass(&WndClass);
 
    WndClass.lpfnWndProc = ChildProc;
    WndClass.lpszClassName = _T("ChildWindow");
    RegisterClass(&WndClass);
 
    hwnd = CreateWindow(_T("Window Class Name"),
        _T("Window Title Name"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg,
    WPARAM wParam, LPARAM lParam)
{
    static HANDLE hThread;
    static DWORD threadID;
    switch (iMsg)
    {
    case WM_CREATE:
        hThread = CreateThread(NULL, 0, DrawThread, hwnd, 0, &threadID);
 
        break;
    case WM_LBUTTONDOWN:
        CloseHandle(CreateThread(NULL, 0, UIThread, hwnd, 0, &threadID));
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        CloseHandle(hThread);
        break;
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
DWORD __stdcall DrawThread(LPVOID pParam) { // Worker 쓰레드 라고한다.
    HWND hwnd = (HWND)pParam;
    HDC hdc = GetDC(hwnd);
 
    for (int i = 0; i < 100; i++)
    {
        Rectangle(hdc, 100 + i, 100, 200 + i, 200);
        Sleep(20);
    }
 
    ReleaseDC(hwnd, hdc);
 
    return 0;
}
LRESULT CALLBACK ChildProc(HWND hwnd, UINT iMsg,
    WPARAM wParam, LPARAM lParam)
{
    __declspec(thread) static int count = 0;
    TCHAR buf[100];
    HDC hdc;
    switch (iMsg)
    {
    case WM_CREATE:
        SetTimer(hwnd, 10, 500, NULL);
        break;
    case WM_TIMER:
        wsprintf(buf, "count:%d", ++count);
        SetWindowText(hwnd, buf);
        break;
    case WM_LBUTTONDOWN:
        hdc = GetDC(hwnd);
        Rectangle(hdc, LOWORD(lParam), HIWORD(lParam), LOWORD(lParam) + 50, HIWORD(lParam) + 50);
        ReleaseDC(hwnd, hdc);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
DWORD __stdcall UIThread(LPVOID pParam) { // UI 쓰레드 라고 한다.
 
    HWND hwndParent=(HWND)pParam;
    HWND hwndChild;
    MSG      msg;
 
 
    hwndChild = CreateWindow(_T("ChildWindow"),
        _T("Child Window !!"),
        WS_OVERLAPPEDWINDOW| WS_POPUPWINDOW |WS_CHILD,
        100,
        100,
        300,
        300,
        hwndParent,
        (HMENU)NULL,
        g_hInst,
        NULL
    );
    ShowWindow(hwndChild, SW_SHOW);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
 
    return 0;
}
 
 
 
댓글
댓글쓰기 폼
네이버 이웃추가
«   2019/10   »
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
글 보관함