Windows 프로그래밍을 할때는 WinMain을 이용해서 사용한다.

 하지만, C언어에서 나오는 WinMain이 아닌 Main 함수를 이용해서 호출하는데.

 만약, WinMain을 사용하지 않는다는 가정하게 한다면, 다음과 같은 함수가 필요하다.


 GetModuleHandle[각주:1]


 참조 : https://bobobobo.wordpress.com/2008/02/03/getting-the-hwnd-and-hinstance-of-the-console-window/


 여기서, 나오는 Console을 연결하는 소스에서 창을 띄우는 부분에 대한 소스가 있다.


 가장 중요한 것은 Console API라는 것이 필요하다.


 이것의 장점은 콘솔창이 뜨기 때문에, 해당 콘솔창에서 디버그 효과를 얻을 수 있게 된다.


 예제 소스

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
 
#include <windows.h>
 
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
 
WNDCLASS WndClass;
 
int numbers = 0;
 
int main()
{
    HINSTANCE hInstance;
    HWND hWnd;
    MSG Message;
    
    hInstance = GetModuleHandle(NULL);
    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="ApiBase";
    WndClass.lpszMenuName=NULL;
    WndClass.style=CS_HREDRAW|CS_VREDRAW;
 
    //윈도우 클래스 생성.
    RegisterClass(&WndClass);
    
    //윈도우 객체 생성.
    hWnd = CreateWindow("ApiBase",
                        "Test",
                        WS_OVERLAPPEDWINDOW,
                        10,// X
                        100,// Y
                        400,// Width
                        400,// Height
                        NULL,
                        (HMENU)NULL,
                        hInstance,
                        NULL);
    
    //윈도우 창 띄우기.
    ShowWindow(hWnd,1);
    
    while(GetMessage(&Message,0,0,0))
    {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    
    return Message.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    long dwStyle;
    
    switch(iMessage)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_PAINT:
            hdc = GetDC(hWnd);
            TextOut(hdc, 10050"Hello World",11);
            ReleaseDC(hWnd, hdc);
            break;
    }
    return DefWindowProc(hWnd,iMessage,wParam, lParam);
}
 
cs


  1. https://msdn.microsoft.com/ko-kr/library/windows/desktop/ms683199(v=vs.85).aspx(2016-06-12) [본문으로]
Posted by JunkMam
,