ホームに戻る
 3、(コードサンプル)アイドル時間駆動型のメッセージ処理

#include <windows.h>

#define WINDOW_CLASS_NAME "WinClassName"
#define WINDOW_NAME "WinName"

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
  MSG msg;
  WNDCLASS wc;
  HWND hWnd;

  // Register Class
  if(hPrevInst == NULL)
  {
    ZeroMemory(&wc, sizeof(wc));
    wc.lpfnWndProc = (WNDPROC)WndProc;
    wc.hInstance = hInst;
    wc.hIcon = LoadIcon((HINSTANCE)NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
    wc.lpszClassName = WINDOW_CLASS_NAME;
    if(RegisterClass(&wc) == 0){
      return 0;
    }
  }
	
  // CreateWindow
  hWnd = CreateWindow(WINDOW_CLASS_NAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    (HWND)NULL, (HMENU)NULL, hInst, (LPVOID)NULL);
  if(hWnd == NULL){
    return 0;
  }

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

  // Message Loop
  while(1){
    if(PeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
    {
      if(msg.message == WM_QUIT)break;
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
    else{
      WaitMessage();
    }
  }

  return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
  switch(uMsg){
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
  }

  return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

inserted by FC2 system