2013년 2월 18일 월요일

[c++/win32api]메모장

// MM4.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;        // current instance
TCHAR szTitle[MAX_LOADSTRING];        // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];        // The title bar text

// Foward declarations of functions included in this code module:
ATOM    MyRegisterClass(HINSTANCE hInstance);
BOOL    InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  // TODO: Place code here.
 MSG msg;
 HACCEL hAccelTable;

 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 LoadString(hInstance, IDC_MM4, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);

 // Perform application initialization:
 if (!InitInstance (hInstance, nCmdShow)) 
 {
  return FALSE;
 }

 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MM4);

 // Main message loop:
 while (GetMessage(&msg, NULL, 0, 0)) 
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }

 return msg.wParam;

 
// 메시지 루프
 while (GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg); // 키보드 메시지를 번역
  DispatchMessage(&msg);  // 메시지를 해당 윈도우 프로시저로 보냅니다.
 }
 return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX); 

 wcex.style   = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc = (WNDPROC)WndProc;
 wcex.cbClsExtra  = 0;
 wcex.cbWndExtra  = 0;
 wcex.hInstance  = hInstance;
 wcex.hIcon   = LoadIcon(hInstance, (LPCTSTR)IDI_MM4);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = (LPCSTR)IDC_MM4;
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm  = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

 return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }
SLWA pSetLayeredWindowAttributes = NULL;  // 함수포인터 선언, 초기화.
 HINSTANCE hmodUSER32 = LoadLibrary("USER32.DLL"); // 인스턴스 얻음.
 pSetLayeredWindowAttributes=(SLWA)GetProcAddress(hmodUSER32,"SetLayeredWindowAttributes");    
 //함수포인터 얻음.
  SetWindowLong(hWnd, GWL_EXSTYLE,GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
 pSetLayeredWindowAttributes(hWnd, 0, (255 * 50) / 100, LWA_ALPHA);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
 
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
 static char str[100];
 
 static int count, yPos;
 static SIZE size;
 HDC hdc;
 PAINTSTRUCT ps;
 switch(iMessage) {
 
 case WM_CREATE:
  count = 0;
  yPos = 0;
  CreateCaret(hWnd, NULL, 5, 15);
  ShowCaret(hWnd);
  break;
 case WM_PAINT: 
  /*
  hdc = BeginPaint(hWnd, &ps);
  TextOut(hdc,0,0,"helloWorld",10);
  EndPaint(hWnd, &ps);
  */
  hdc = BeginPaint(hWnd, &ps);
  GetTextExtentPoint(hdc,str, strlen(str), &size);
  TextOut(hdc,0,yPos,str,strlen(str));
  SetCaretPos(size.cx, 0);
  EndPaint(hWnd, &ps);
  break;
 /*
 case WM_KEYDOWN:
  hdc = GetDC(hWnd);
  TextOut(hdc,100,0,"KeyDown",7);
  ReleaseDC(hWnd, hdc);
  break;
 */
 /* 
 case WM_KEYUP:
  hdc = GetDC(hWnd);
  TextOut(hdc,200,0,"KeyUP",5);
  ReleaseDC(hWnd, hdc);
  break;
 */
 case WM_CHAR:
  
  if(wParam == VK_BACK) //backspace key
   count--;
  else if(wParam == VK_RETURN) //enter key
  {
   count = 0;
   yPos +=20;
  }
  else 
   str[count++] = wParam;
  
  str[count] = '\0';
  /*
  hdc = GetDC(hWnd);
  TextOut(hdc,300,0,str,strlen(str));
  ReleaseDC(hWnd, hdc);
  */
  InvalidateRgn(hWnd, NULL, TRUE);
  break;
 case WM_DESTROY:
  HideCaret(hWnd);
  DestroyCaret();
  PostQuitMessage(0);
  return 0;
 }
 return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}

 

댓글 없음:

댓글 쓰기