WIN32 HELLOWORLD

#include <Windows.h>

#pragma comment(lib,"WINMM.lib") // used by PlaySound()

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
 static TCHAR szAppName[]=TEXT("hello win32!");
 HWND hwnd;
 MSG msg;
 WNDCLASS wndclass;

 //initialize wndclass struct
 wndclass.style = CS_HREDRAW | CS_HREDRAW;
 wndclass.cbClsExtra = 0;
 wndclass.cbWndExtra = 0;
 wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); //HGDIOBJ实际上是NEAR的void*。WIN32不区分NEAR和LONG
 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 wndclass.hInstance = hInstance; //passed by WinMain
 wndclass.lpfnWndProc = WndProc;  // 重要! lpfnWndProc指向窗口的消息处理函数WndProc
 wndclass.lpszClassName = szAppName;
 wndclass.lpszMenuName = NULL;
 
 if (!RegisterClass(&wndclass))
 {
  MessageBox(NULL,TEXT("RegisterClass(&wndclass) failed!"),szAppName,MB_ICONERROR);
  return 0;
 }

 hwnd = CreateWindow(szAppName,TEXT("hello win32 program"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
 
 ShowWindow(hwnd, nShowCmd);
 UpdateWindow(hwnd);

 while (GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return msg.wParam;
}

 

// 窗口消息处理函数 WndProc

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps;
 RECT rect;

 switch (message)
 {
 case WM_CREATE:
  PlaySound(TEXT("Windows XP 启动.wav"),NULL,SND_FILENAME|SND_ASYNC);
  return 0;
 case WM_PAINT:
  hdc = BeginPaint(hwnd,&ps);
  GetClientRect(hwnd,&rect);
  DrawText(hdc,TEXT("Hello WIN32!"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  EndPaint(hwnd,&ps);
  return 0;
 case WM_DESTROY:
  PostQuitMessage(0);
  return 0;
 }

 return DefWindowProc(hwnd,message,wParam,lParam);
}

你可能感兴趣的:(helloworld)