//---------------------------------------
// Win32 API Programming
//---------------------------------------

#include <windows.h>

// 함수 선언
void InitInstance(HINSTANCE hInstance, int nCmdShow);
void InitApplication(HINSTANCE hInstance);
int MessageLoop();
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// WinMain 함수
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 어플리케이션 초기화
InitApplication(hInstance);

// 인스턴스 초기화
InitInstance(hInstance, nCmdShow);

// 메시지 루프
return MessageLoop();
}

// 어플리케이션 초기화: 윈도우 클래스 등록
void InitApplication(HINSTANCE hInstance)
{
WNDCLASS wc; //구조체를 만듦.
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc; // 윈도우 프로시저 설정. 필수!! 윈도우 프로시저의 위치를 알려주는것임.
wc.hInstance = hInstance; // 프로그램 핸들
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "ExampleClass"; // 등록할 윈도우클래스명 (전체에 대한 타이틀?)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;

RegisterClass(&wc); //win32 API 함수 중요!!
}

// 인스턴스 초기화: 윈도우 생성
void InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hWnd = CreateWindow("ExampleClass",  // 등록된 윈도우 클래스 (윈도우를 만들고)
"Caption : Win32 API",
WS_OVERLAPPEDWINDOW, // Style : 시스템 메뉴, ...
CW_USEDEFAULT, CW_USEDEFAULT, // x,y
720, 540, // xSize, ySize
NULL, NULL, hInstance, NULL);

ShowWindow(hWnd, nCmdShow); // SW_SHOW, SW_SHOWMAXIMIZED (만든 윈도우를 화면에 출력)
UpdateWindow(hWnd); //갱신
}

// 메시지 루프
int MessageLoop()
{
MSG msg;
while (GetMessage(&msg, NULL, NULL, NULL))
{
DispatchMessage(&msg); //넘겨줌
}
return msg.wParam;
}

// 윈도우 프로시저: 메시지 처리
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hDC;
switch (message) {

case WM_CREATE:

MessageBox(hWnd, "Hello", "test", MB_OK);

break;

case WM_LBUTTONDOWN:

hDC = GetDC(hWnd); //O/S한테 허락을 받고.
TextOut(hDC, LOWORD(lParam), HIWORD(lParam), "Loading....", 10); //클릭한 좌표에 메세지출력.

ReleaseDC(hWnd, hDC); //다 쓰면 되돌려줌.
break;

case WM_DESTROY:             // 창(Window) 닫힘.

PostQuitMessage(0);  // WM_QUIT 메시지 전송 -> 프로그램 종료

break;

}



return DefWindowProc(hWnd, message, wParam, lParam); //기본적으로 윈도우에서 조정해줌.

}






이게 예제입니다.


이걸 보고 연습하려고 똑같이 코딩해봤는데









#include <Windows.h>


void InitInstance(HINSTANCE hInstance, int nCmdShow);

void InitApplication(HINSTANCE hInstance);

int MessageLoop();

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



int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

InitApplication(hInstance);


InitInstance(hInstance, nCmdShow);


return MessageLoop();

}



void InitApplication(HINSTANCE hInstance)

{

WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;

wc.lpfnWndProc = WndProc;

wc.hInstance = hInstance;

wc.hIcon = LoadIcon(NULL, IDC_ARROW);

wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

wc.lpszMenuName = NULL;

wc.lpszClassName = "TestClass";

wc.cbClsExtra = 0;

wc.cbWndExtra = 0;


RegisterClass(&wc);

}



void InitInstance(HINSTANCE hInstance, int nCmdShow)

{

HWND hWnd;


hWnd = CreateWindow("TestClass",

"Test Window",

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT,

720, 540,

NULL, NULL, hInstance, NULL);


ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

}



int MessageLoop()

{

MSG msg;

while (GetMessage(&msg, NULL, NULL, NULL))

{

DispatchMessage(&msg);

}

return msg.wParam;

}



LRESULT CALLBACK WndProc(HWND hWnd, UINT message,

WPARAM wParam, LPARAM lParam)

{

HDC hDC;

switch (message) {


case WM_CREATE:

MessageBox(hWnd, "Hello", "test", MB_YESNO);

break;

case WM_LBUTTONDOWN:

hDC = GetDC(hWnd);

TextOut(hDC, LOWORD(lParam), HIWORD(lParam), "Loading...", 10);

ReleaseDC(hWnd, hDC);

case WM_DESTROY:

PostQuitMessage(0);

break;

}



return DefWindowProc(hWnd, message, wParam, lParam);

}







하나도 안틀리고 고대로 베꼈는데 위에꺼(예제)는 잘 되고

제꺼는 오류가 나네요;;;;;


최초빌드때 성공이라고만 나오고 윈도우창은 출력이 안돼요.

그렇다고 ShowWindow쪽에 잘못쓴것도 없는것같은데 음;;;


그리고 더 이상한점은 그 이후에 실행시키려고 하면 "test.exe를 쓰기용으로 열 수 없습니다" 라면서 오류가 나와요. 

찾아보니까 이 오류는 이미 프로그램이 실행중이면 나오는 오류라고 하는데요.

첫 빌드때 성공뜨고 아무것도 안나오는것 보니까 인터페이스로 출력되지않고 백그라운드 뒤에서 지혼자 돌아가고있는거 같은데;;


대체 왜그럴까요 ShowWindow에서 뭔가 잘못된걸까요????