windows 初体验

时间:2021-09-29 03:15:47

#include <windows.h>


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

int WINAPI WinMain1(HINSTANCE hInst, HINSTANCE, LPSTR szCmd, int nShow)
{
  WNDCLASS wndClass;
  TCHAR *ClassName = TEXT("MyClass");
  HWND hwnd;//用来记录窗口句柄
  MSG msg; //界说一个动静布局,用来存储获取的动静
  wndClass.cbClsExtra = 0;
  wndClass.cbWndExtra = 0;
  wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wndClass.lpfnWndProc = wndProc;
  wndClass.lpszClassName = ClassName; //类名
  wndClass.lpszMenuName = NULL; //暂时不要菜单
  wndClass.style = CS_VREDRAW | CS_HREDRAW;
  RegisterClass(&wndClass);//注册窗口
  //#define CreateWindowW(lpClassName, lpWindowName, dwStyle, x, y,\nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)
  hwnd = CreateWindow(
  ClassName/*窗口类*/,
  TEXT("Hello"/*窗口名*/),
  WS_OVERLAPPEDWINDOW/*窗口类型*/,
  CW_USEDEFAULT/*默认窗口位置 横坐标*/,
  CW_USEDEFAULT/*默认窗口位置 纵坐标*/,
  500/*窗口的宽*/,
  300/*窗口的高*/,
  NULL/*父窗口:没有父窗口,默认是桌面*/,
  NULL/*没有菜单*/,
  hInst/*措施实例句柄*/,
  NULL );


  ShowWindow(hwnd, nShow);//显示窗口
  UpdateWindow(hwnd);//刷新窗口


  while (GetMessage(&msg, NULL, 0, 0))//如果给窗口发送动静0 ,就退出循环结束措施
  {
    TranslateMessage(&msg);//翻译
    DispatchMessage(&msg);//把动静发送给措置惩罚惩罚动静的窗口措施
  }
  return 0;
}

LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lparam)
{
  switch (message)
  {
  case WM_DESTROY:
    PostQuitMessage(0);//给措施发送一条退出动静 退出值0
    return 0;
  default:
    return DefWindowProc(hwnd, message, wParam, lparam);//本身不措置惩罚惩罚,交给系统措置惩罚惩罚
  }
}