ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C언어 공부 되새김 Win32Api구현해보기 2편 화면출력함수처리
    C언어 공부 되새김 Win32api로 구현해보기 2019. 10. 16. 20:53

    컴퓨터 프로그래밍 C언어 공부를 콘솔로 공부하고 윈도우 프로그래밍으로 구현하는데 

     

    콘솔로 공부하던 소스를 윈도우 프로그래밍 기본코드를 제외하고 함수형식으로 콘솔 소스와

     

    비슷하게 구현해보려고 합니다.

     

    콘솔은 쉽고 윈도우는 어렵다가 아니고 윈도우 프로그래밍을 시작부터 접하면서 친숙해졌으면 합니다.

     

    구현은 무료 통합개발 환경인 Dev-C++ 4.9.9.2로 구현합니다.

     

    이번엔 지난 편에 다루었던 하면출력부분을 콘솔처럼 함수에서 비슷하게 구성하겠습니다.

     

    콘솔화면은 가로 80칸 세로 23칸으로 구성되어 있으나

     

    윈도우는 칸수구분이 아니라 기본적으로 픽셀 구성이라 적용되는 폰트에 따라

     

    같은 수의 글자라도 길이가 다를 수 있습니다.

     

    윈도우 프로그램을 가로 640 세로 480으로 고정하고 크기를 조절하지 못하도록 고정하고 다루도록 하겠습니다.

     

     윈도우 생성시 윈도우 타입을 아래와 같이 수정하고 크기를 확정해 주면 됩니다.

        WS_CAPTION | WS_SYSMENU, /* default window */
        640,                 /* The programs width */
        480,                 /* and height in pixels */

     

     길이가 다른 예

     

    WIN32API는 프로그램 실행시 윈도우생성하면 윈도우 메시지인 WM_PAINT 메시지가 발생됩니다.

     

    이를 이용하여 상기 메시지 수신시 함수 처리식으로 main함수부분을 떼 놓는 방식으로 콘솔 main 함수처럼 이용할 수 있습니다.

     

    소스 상단부에 함수 선언해주고 윈도우 기본 소스코딩이 완료된 밑부분에 함수를 main처럼 구성하였습니다.

     

    #include <windows.h>

     

    /*  Declare Windows procedure  */

    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

     

    /*  Make the class name into a global variable  */

    char szClassName[ ] = "myadmonWindowsApp";

     

    void Study_Main(HDC hdc); //main 함수화 선언

     

    int WINAPI WinMain (HINSTANCE hThisInstance,

                        HINSTANCE hPrevInstance,

                        LPSTR lpszArgument,

                        int nFunsterStil)

     

    {

        HWND hwnd;               /* This is the handle for our window */

        MSG messages;            /* Here messages to the application are saved */

        WNDCLASSEX wincl;        /* Data structure for the windowclass */

     

        /* The Window structure */

        wincl.hInstance = hThisInstance;

        wincl.lpszClassName = szClassName;

        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */

        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */

        wincl.cbSize = sizeof (WNDCLASSEX);

     

        /* Use default icon and mouse-pointer */

        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);

        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);

        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);

        wincl.lpszMenuName = NULL;                 /* No menu */

        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */

        wincl.cbWndExtra = 0;                      /* structure or the window instance */

        /* Use Windows's default color as the background of the window */

        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

     

        /* Register the window class, and if it fails quit the program */

        if (!RegisterClassEx (&wincl))

            return 0;

     

        /* The class is registered, let's create the program*/

        hwnd = CreateWindowEx (

               0,                   /* Extended possibilites for variation */

               szClassName,         /* Classname */

               "myadmon Windows App",       /* Title Text */

               WS_CAPTION | WS_SYSMENU, /* default window */

               CW_USEDEFAULT,       /* Windows decides the position */

               CW_USEDEFAULT,       /* where the window ends up on the screen */

               640,                 /* The programs width */

               480,                 /* and height in pixels */

               HWND_DESKTOP,        /* The window is a child-window to desktop */

               NULL,                /* No menu */

               hThisInstance,       /* Program Instance handler */

               NULL                 /* No Window Creation data */

               );

     

        /* Make the window visible on the screen */

        ShowWindow (hwnd, nFunsterStil);

     

        /* Run the message loop. It will run until GetMessage() returns 0 */

        while (GetMessage (&messages, NULL, 0, 0))

        {

            /* Translate virtual-key messages into character messages */

            TranslateMessage(&messages);

            /* Send message to WindowProcedure */

            DispatchMessage(&messages);

        }

     

        /* The program return-value is 0 - The value that PostQuitMessage() gave */

        return messages.wParam;

    }

     

     

    /*  This function is called by the Windows function DispatchMessage()  */

     

    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

    {

        HDC hdc;

        PAINTSTRUCT ps;

       

        switch (message)                  /* handle the messages */

        {

     

            case WM_PAINT:

                hdc=BeginPaint(hwnd,&ps);

     

                Study_Main(hdc); //함수 호출

     

                EndPaint(hwnd,&ps);

                             break;

     

            case WM_DESTROY:

                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */

                break;

            default:                      /* for messages that we don't deal with */

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

        }

     

        return 0;

    }

     

    void Study_Main(HDC hdc){ //콘솔 main함수 처럼 사용

        

         TextOut(hdc,0,0,"C 언어 시작하기",15);

         TextOut(hdc,0,20,"01234567890123456789012345678901234567890123456789012345678901234567890123456789",80);    

         TextOut(hdc,0,40,"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",78);         

    }

     

    콘솔 main함수 처럼 사용할 Study_Main함수 생성하였습니다.

     

    화면에 왼쪽으로 별 삼각형을 출력하는 예입니다.

    *
    **
    ***
    ****
    *****
    ******
    *******

     

    ▷ 소스 비교

    Console 실행화면
    int main(int argc, char *argv[])
    {
      printf("*\n"); 
      printf("**\n");
      printf("***\n");
      printf("****\n");
      printf("*****\n");
      printf("******\n");
      printf("*******\n");
      
      system("PAUSE");
      return 0;
    }
    Windows 실행화면
    void Study_Main(HDC hdc){
         
         TextOut(hdc,0,0,"*",1);
         TextOut(hdc,0,20,"**",2);
         TextOut(hdc,0,40,"***",3);
         TextOut(hdc,0,60,"****",4);
         TextOut(hdc,0,80,"*****",5);
         TextOut(hdc,0,100,"******",6);
         TextOut(hdc,0,120,"*******",7);

    }

     

    소스와 출력화면이 비슷해 졌습니다.

     

    다음편에 다른방법으로 화면 출력 구현해 보겠습니다.

     

    오늘은 이만...

     

     

Designed by Tistory.