본문 바로가기

프로그램언어/C++

컨트롤 윈도우 사용하기

CreateWindow()함수

HWND CreateWindow(

    LPCTSTR         lpClassName,               // 클래스 이름

    LPCTSTR         lpWindowName,           // 윈도우 제목 텍스트

    DWORD         dwStyle,                      // 생성할 윈도우의 스타일
    int                 x,                              // 원도우 왼쪽 위 모서리의 x좌표
    int                 y,                              // 원도우 위쪽 위 모서리의 y좌표
    int                 nWidth,                      // 윈도우 가로 길이
    int                 nHeight,                     // 윈도우 세로 길이

    HWND           hWndParent,               // 부모 윈도우 핸들

    HMENU          hMenu,                     // 윈도우 메뉴(컨트롤 ID)

    HINSTANCE     hInstance,                  // 윈도우를 생성할 인스턴스 핸들

    LPVOID           lpParam                    // WM_CREATE 메시지에 넘길 전달 인자
);

 

버튼 컨트롤 윈도우 생성하기

#include <windows.h>

#include <TCHAR.H>

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

HINSTANCE hInst;

 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)

{

    HWND hwnd;

    MSG msg;

    WNDCLASS WndClass;

    hInst = hInstance;

    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);

    hwnd = CreateWindow(_T("Window Class Name"), _T("Main Window Title"), 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;

}

 

#define IDC_BUTTON 100

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

    HDC hdc;

    HWND hButton;

 

    switch (iMsg)

    {

    case WM_CREATE:

        hButton = CreateWindow(_T("button"), _T("확인"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,

            200, 0, 100, 25, hwnd, (HMENU)IDC_BUTTON, hInst, NULL );

        return 0;

    case WM_COMMAND:

        switch (LOWORD(wParam))

        {

        case IDC_BUTTON:

            hdc = GetDC(hwnd);

            TextOut(hdc, 0, 100, _T("Hello World"), 11);

            ReleaseDC(hwnd, hdc);

            return 0;

        }

        return 0;

    case WM_DESTROY:

        PostQuitMessage(0);

        return 0;

    }

    return DefWindowProc(hwnd, iMsg, wParam, lParam);

}

실행 결과]

 

 

에디트 컨트롤 윈도우 생성하기

#define IDC_BUTTON 100

#define IDC_EDIT 101

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

    HDC hdc;

    static HWND hButton, hEdit;

    TCHAR str[100];

 

    switch (iMsg)

    {

    case WM_CREATE:

        hButton = CreateWindow(_T("button"), _T("확인"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,

            200, 0, 100, 25, hwnd, (HMENU)IDC_BUTTON, hInst, NULL );

        hEdit = CreateWindow(_T("edit"), _T("에디트 컨트롤"), WS_CHILD | WS_VISIBLE | WS_BORDER,

            0, 0, 200, 25, hwnd, (HMENU)IDC_EDIT, hInst, NULL );

        return 0;

    case WM_COMMAND:

        switch (LOWORD(wParam))

        {

        case IDC_BUTTON:

            GetDlgItemText(hwnd, IDC_EDIT, str, 100);

            hdc = GetDC(hwnd);

            TextOut(hdc, 0, 100, str, _tcslen(str));

            ReleaseDC(hwnd, hdc);

            return 0;

        }

        return 0;

    case WM_DESTROY:

        PostQuitMessage(0);

        return 0;

     }

    return DefWindowProc(hwnd, iMsg, wParam, lParam);

}

실행 결과]

 

콤보 박스 컨트롤 원도우 생성하기

#define IDC_COMBO 102

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

    HDC hdc;

    static HWND hButton, hEdit, hCombo;

    TCHAR str[100];

 

    switch (iMsg)

    {

    case WM_CREATE:

        hButton = CreateWindow(_T("button"), _T("확인"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,

            200, 0, 100, 25, hwnd, (HMENU)IDC_BUTTON, hInst, NULL );

        hEdit = CreateWindow(_T("edit"), _T("에디트 컨트롤"), WS_CHILD | WS_VISIBLE | WS_BORDER,

            0, 0, 200, 25, hwnd, (HMENU)IDC_EDIT, hInst, NULL );
        hCombo = CreateWindow(_T("combobox"), NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWN,
            0, 100, 200, 300, hwnd, (HMENU)IDC_COMBO, hInst, NULL );

        return 0;

    case WM_COMMAND:

        switch (LOWORD(wParam))

        {

        case IDC_BUTTON:

            GetDlgItemText(hwnd, IDC_EDIT, str, 100);

            if (_tcscmp(str, _T("")))

                SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)str);

            return 0;

        }

        return 0;

    case WM_DESTROY:

        PostQuitMessage(0);

        return 0;

     }

    return DefWindowProc(hwnd, iMsg, wParam, lParam);

}

실행 결과]

 

리치 에디트 컨트롤 윈도우 만들기

#include <windows.h>

#include <TCHAR.H>

#include <richedit.h>

#include "resource.h"

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

HINSTANCE hInst;

 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)

{

    HINSTANCE hRichedit;

    HWND hwnd;

    MSG msg;

    WNDCLASS WndClass;

    hInst = hInstance;

    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 = MAKEINTRESOURCE(IDR_MENU1);

    WndClass.lpszClassName = _T("Window Class Name");

    RegisterClass(&WndClass);

 

    hRichedit = LoadLibrary(_T("RICHED20.DLL"));

    hwnd = CreateWindow( _T("Window Class Name"), _T("Main Window Title"), 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);

    }

    FreeLibrary(hRichedit);

    return (int)msg.wParam;

}

#define IDC_RICHEDIT 100

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

    static HWND hRichedit;

    RECT rect;

    CHARFORMAT cf;

 

    switch (iMsg)

    {

    case WM_CREATE:

        GetClientRect(hwnd, &rect);

        hRichedit = CreateWindow(

            _T("RichEdit20W"),

            _T("우리는 민족 중흥의 역사적 사명을 띠고 이 땅에 태어났다."),

           WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |

           ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | WS_BORDER,

           100, 100, rect.right, rect.bottom, hwnd, (HMENU)IDC_RICHEDIT, hInst, NULL );

        return 0;

    case WM_SIZE:

        GetClientRect(hwnd, &rect);

        MoveWindow(hRichedit, 0, 0, rect.right, rect.bottom, TRUE);

        return 0;

    case WM_COMMAND:

        switch (LOWORD(wParam))

        {

        case ID_FONT:

            cf.cbSize = sizeof(CHARFORMAT);

            cf.dwMask = CFM_FACE | CFM_SIZE | CFM_COLOR;

            cf.yHeight = 300;

            cf.dwEffects = NULL;

            _tcscpy_s(cf.szFaceName, _T("궁서"));

            cf.crTextColor = RGB(100, 0, 0);

            SendMessage(hRichedit, EM_SETCHARFORMAT,

            (WPARAM)(UINT)SCF_SELECTION, (LPARAM)&cf);

           return 0;

        }

        return 0;

    case WM_DESTROY:

        PostQuitMessage(0);

        return 0;

    }

    return DefWindowProc(hwnd, iMsg, wParam, lParam);

}

 

실행 결과]

'프로그램언어 > C++' 카테고리의 다른 글

파일 입출력2  (0) 2021.01.11
파일 입출력1  (0) 2021.01.05
MID 프로그래밍  (0) 2021.01.03
대화상자4  (0) 2020.12.31
대화상자3  (0) 2020.12.29