티스토리 뷰

프로그래밍/API

api 실습예제

문정환 2008. 5. 28. 11:35

#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass=TEXT("First");

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
                  ,LPSTR lpszCmdParam,int nCmdShow)
{
        HWND hWnd;
        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;
        WndClass.lpszClassName=lpszClass;
        WndClass.lpszMenuName=NULL;
        WndClass.style=CS_HREDRAW | CS_VREDRAW;
        RegisterClass(&WndClass);

        hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,CW_USEDEFAULT,320,240,
                NULL,(HMENU)NULL,hInstance,NULL);
        ShowWindow(hWnd,nCmdShow);
       
        while (GetMessage(&Message,NULL,0,0)) {
                TranslateMessage(&Message);
                DispatchMessage(&Message);
        }
        return (int)Message.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    static HWND hButton1, hButton2,hButton3;   
 static HANDLE hFile;
 DWORD dwWri, dwRead;
 static BOOL bTrue, bTrue2;
 char szText[]="조선대학교 컴퓨터공학과";
 char szBuf;
 switch (iMessage) {
  case WM_CREATE:
  hButton1=CreateWindow("BUTTON","파일생성",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,60,60,90,30,hWnd,(HMENU)1,NULL,NULL);
  hButton2=CreateWindow("BUTTON","파일읽기",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,60,90,90,30,hWnd,(HMENU)2,NULL,NULL);
  hButton3=CreateWindow("BUTTON","종료",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,60,120,90,30,hWnd,(HMENU)3,NULL,NULL);
  return 0;
  case WM_COMMAND:
  switch(LOWORD(wParam))
  {
  case 1:
   hFile=CreateFile("c:\\api.txt",GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
   if(hFile==INVALID_HANDLE_VALUE)
    MessageBox(hWnd,"파일생성 실패","경고",MB_OK);
   else
    MessageBox(hWnd,"파일생성 성공","경고",MB_OK);
   return 0;
  case 2:
  bTrue=WriteFile(hFile,szText,lstrlen(szText),&dwWri,NULL);
 
  if(bTrue==TRUE)
  {  MessageBox(hWnd,"파일읽기 성공","경고",MB_OK);
    SetFilePointer(hFile,0,NULL,FILE_BEGIN);
    bTrue2 = ReadFile(hFile,szBuf,lstrlen(szText),&dwRead,NULL);
    if(bTrue2==TRUE)
    {
     szBuf[dwRead]=0;
     MessageBox(hWnd,szBuf,"알림", NULL);
     SetEndOfFile(hFile);
     closeHandle(hFile);
    }
    else
    MessageBox(hWnd,"파일읽기 실패","알림",MB_OK);

    CloseHandle(hFile);
  }
   else
    MessageBox(hWnd,"파일쓰기 실패","경고",MB_OK);
   return 0;
  case 3:
   PostQuitMessage(0);
  }
  return 0;
        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }
        return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함