My simple API

来源:互联网 发布:宏毅网络 编辑:程序博客网 时间:2024/05/10 12:38

#include <windows.h>
/***********************************************************************/
LRESULT CALLBACK WinProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    switch( uMsg )
 {
 case WM_KEYDOWN://击键消息
     switch( wParam )
     {
     case VK_ESCAPE:
     PostMessage(hwnd, WM_CLOSE, 0, 0);//给窗口发送WM_CLOSE消息
     break;
     }
     return 0; //处理完一个消息后返回0
    case WM_CLOSE: //准备退出
  DestroyWindow( hwnd ); //释放窗口
  return 0;
    case WM_RBUTTONDOWN:
     MessageBox(hwnd,"鼠标右键按下了!","Mouse",MB_OK);
     return 0;
    case WM_DESTROY: //如果窗口被人释放…
     PostQuitMessage( 0 ); //给窗口发送WM_QUIT消息
 return 0;
 }
 //调用缺省消息处理过程
 return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
/***********************************************************************/
HWND InitWindow(HINSTANCE hinst, int nCmdShow)
{
  HWND                hwnd; // the window handle we obtain will be put here
  WNDCLASSEX          wc;

  // set up and register window class
  memset(&wc, 0, sizeof(wc));
  wc.cbSize = sizeof(wc);
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = WinProc; // change this to NULL and crash!
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hinst;
  wc.hIcon = NULL;
  wc.hIconSm = NULL;
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = CreateSolidBrush(RGB(100,0,0));    //CreateSolidBrush (RGB(100, 0, 0));
  wc.lpszMenuName = NULL;
  wc.lpszClassName = "MyCoolWindow";
  RegisterClassEx(&wc);

  // create a window that's 200 pixels wide, 100 tall
  hwnd = CreateWindowEx(0, "MyCoolWindow", "My First Window",
     WS_OVERLAPPEDWINDOW, 50, 50, 200, 100, NULL, NULL, hinst, NULL);

  if (!hwnd) {
    ::MessageBox(NULL, "CreateWindow failed!", "Ch1p1_HackedWindowProc", MB_ICONSTOP);
    exit(-1);
  }

  ShowWindow(hwnd, nCmdShow);
  return(hwnd);
}

/***********************************************************************/
int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  HWND hwnd = InitWindow(hInstance, nCmdShow);
    MSG msg;
 //进入消息循环:
 for(;;)
 {
  if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  {
   if ( msg.message==WM_QUIT) break;
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }
 return msg.wParam;
}