본문 바로가기
코드/C++

버튼 클릭 시 메세지 출력

by bongin 2023. 5. 16.
728x90
반응형

컴파일은 cl box.cpp user32.lib

또는

코드 상단 #pragma comment(lib, "user32") 추가 후 cl box.cpp

error LNK2019

참고 : https://kaspyx.tistory.com/23

chatgpt에서 c++로 네모 버튼 눌리면 hello 출력하는 프로그램 만들어 줘

 

//#pragma comment(lib,"user32")
//#pragma comment(lib,"gdi32")

#include <windows.h>

// 윈도우 프로시저 함수
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_COMMAND:
            // 버튼이 눌렸는지 확인
            if (LOWORD(wParam) == 1)
            {
                // "Hello" 출력
                MessageBox(hWnd, "Hello", "Button Clicked", MB_OK);
            }
            break;
        case WM_DESTROY:
            // 윈도우가 파괴되었음을 알림
            PostQuitMessage(0);
            break;
        default:
            // 기본 윈도우 프로시저 함수 호출
            return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
}

// WinMain 함수
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 윈도우 클래스 구조체 정의
    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;                // 윈도우 프로시저 함수 등록
    wc.hInstance = hInstance;                // 인스턴스 핸들 등록
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); // 커서 등록
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // 배경색 지정
    wc.lpszClassName = "MyWindowClass";       // 윈도우 클래스 이름 지정

    // 윈도우 클래스 등록
    if (!RegisterClass(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // 윈도우 생성
    HWND hWnd = CreateWindow("MyWindowClass", "Hello World!", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, NULL, NULL, hInstance, NULL);

    if (hWnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // 버튼 생성
    CreateWindow("BUTTON", "Click Me!", WS_VISIBLE | WS_CHILD, 100, 100, 100, 30, hWnd, (HMENU)1, hInstance, NULL);

    // 윈도우 출력
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    // 메시지 루프
    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}
728x90
반응형

'코드 > C++' 카테고리의 다른 글

128TB를 16진수로 변환  (0) 2023.07.07
6개의 버튼을 가진 윈도우 생성 (LNK2019, LNK1120)  (0) 2023.06.20
Chapter04_Game programming in c++  (0) 2022.09.17
Chapter03_Game programming in c++  (0) 2022.09.09
개발 일지  (0) 2022.07.28

댓글