상세 컨텐츠

본문 제목

VC++ API프로그램_1

C++,C#, ASP.NET

by 김일국 2018. 3. 2. 17:56

본문

비주얼C++ 방송대학 과목을 올해 마스터하기 위해 , VC++ 프로그램을 본격적으로 학습하기로 결심하였다.

올해 VC++을 사용하는 과목은 2가지 이다. (비주얼C++, 컴퓨터그래픽스:OpenGL 참조URL http://blog.daum.net/web_design/515 )

학습 첫번째로 API프로그램을 시작으로 MFC프로그램까지 학습용 프로그램을 실습해 보기로 하였다.

오늘으 그 첫번째로 API프로그램으로 매인윈도우, 차일드윈도우, 버튼컨트롤 과 메세지박스까지 실습해 보았다.

아래는 소스 HelloAPI.cpp파일내용

#include <windows.h>
#include <atlstr.h> //문자열 합치기위한 CString 사용을 위해
#define ID_OK_BTN 2000 //버튼컨트롤용
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);//차일드윈도우용
HINSTANCE g_hInst;//차일드윈도우용
LPCTSTR lpszClass = L"HelloAPI";
LPCTSTR ChildClassName = L"ChildWin";//차일드윈도우용

int APIENTRY WinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpszCmdParam,
 int nCmdShow)
{
 HWND hWnd;
 HWND hChildWnd;
 MSG Message;
 WNDCLASS WndClass;
 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 = lpszClass;
 WndClass.lpszMenuName = NULL;
 WndClass.style = CS_HREDRAW | CS_VREDRAW;
 RegisterClass(&WndClass);
 //차일드윈도우용 s
 WndClass.lpfnWndProc = ChildWndProc;
 WndClass.lpszClassName = ChildClassName;
 RegisterClass(&WndClass);
 //차일드윈도우용 e
 hWnd = CreateWindow(lpszClass,   //윈도우클래스 이름
  L"윈도우 프로그래밍",       //윈도우타이틀
  WS_OVERLAPPEDWINDOW | WS_VISIBLE,   //윈도우스타일
  200, 200,       //윈도우가 보일때 X Y좌표
  600, 600,       //윈도우의 폭과 높이    
  (HWND)NULL,       //부모윈도우 핸들
  (HMENU)NULL,      //윈도우가 가지는 메뉴핸들
  hInstance,       //인스턴스핸들
  NULL);        //여분의 데이터

 ShowWindow(hWnd, nCmdShow);

 while (GetMessage(&Message, 0, 0, 0)) {
  TranslateMessage(&Message);
  DispatchMessage(&Message);
 }
 return Message.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message,
 WPARAM wParam, LPARAM lParam)//메인윈도우 콜백함수
{
 LPCTSTR text = L"메인윈도우 생성";
 switch (Message)
 {
  case WM_PAINT:
  {
   PAINTSTRUCT ps;
   HDC hdc = BeginPaint(hWnd, &ps);
   TextOut(hdc, 100, 100, text, lstrlen(text));
   EndPaint(hWnd, &ps);
   return 0;
  }
  case WM_CREATE://차일드윈도우용
  {
   HWND hChildWnd = CreateWindow(ChildClassName, //윈도우클래스 이름
    L"차일드 윈도우",       //윈도우타이틀
    WS_OVERLAPPEDWINDOW | WS_CHILD,    //윈도우스타일
    150, 150,         //윈도우가 보일때 X Y좌표
    260, 200,         //윈도우의 폭과 높이    
    hWnd,          //부모 윈도우 핸들
    (HMENU)1000,        //차일드윈도우가 가지는 메뉴핸들
    g_hInst,         //인스턴스핸들
    (LPVOID)NULL);        //여분의 데이터
   if (!hChildWnd) return -1;
   ShowWindow(hChildWnd, SW_SHOW);
   //버튼컨트롤용 s 컨트롤은 콜백함수가 필요없고, 운영체제가 자동생성해 준다.
   HWND hBtnChildWnd = CreateWindow(L"button", //윈도우클래스 이름
    L"지역대학",       //윈도우타이틀
    WS_CHILD | WS_VISIBLE,    //윈도우스타일+ShowWindow 액션포함
    20, 400,         //윈도우가 보일때 X Y좌표
    100, 30,         //윈도우의 폭과 높이    
    hWnd,          //부모 윈도우 핸들
    (HMENU) ID_OK_BTN,       //버튼윈도우가 가지는 메뉴핸들
    g_hInst,         //인스턴스핸들
    (LPVOID)NULL);        //여분의 데이터
   //버튼컨트롤용 e
   return 0;
  }
  case WM_COMMAND:
  {
   if (LOWORD(wParam) == ID_OK_BTN)
   {
    CString sId, title; //문자와 숫자 합치기
    sId.Format(_T("%i"), ID_OK_BTN);//int형을 문자형으로 변환
    title = L"지역대학_버튼ID는 " + sId;//문자열 합치기
    MessageBox(hWnd, L"[지역대학] 버튼이 클릭되었습니다.", title, MB_OK);
   }
   return 0;
  }
  case WM_DESTROY:
   PostQuitMessage(0);
   return 0;
 }
 return(DefWindowProc(hWnd, Message, wParam, lParam));
}

LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT iMessage,
 WPARAM wParam, LPARAM lParam)//차일드윈도우용 콜백프로시저
{
 LPCTSTR text = L"차일드윈도우";
 switch (iMessage)
 {
  case WM_PAINT:
  {
   PAINTSTRUCT ps;
   HDC hdc = BeginPaint(hWnd, &ps);
   TextOut(hdc, 10, 10, text, lstrlen(text));
   EndPaint(hWnd, &ps);
   return 0;
  }
 }
 return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}

관련글 더보기

댓글 영역