天天看點

win32 api 建立視窗的一個簡單例子

#include "stdafx.h"

#include "resource.h"

#define  BUFSIZ 126

TCHAR szWindowClass[BUFSIZ];

HINSTANCE hInst = NULL;

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

ATOM MyRegisterClass(HINSTANCE hInstance)

{

 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);

 wcex.style   = CS_HREDRAW | CS_VREDRAW;

 wcex.lpfnWndProc = WndProc;

 wcex.cbClsExtra  = 0;

 wcex.cbWndExtra  = 0;

 wcex.hInstance  = hInstance;

 wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON2));

 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);

 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 18);

 wcex.lpszMenuName = NULL;

 wcex.lpszClassName = szWindowClass;

 wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON1));

 return RegisterClassEx(&wcex);

}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

 HWND hWnd;

 hInst = hInstance;

    hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, szWindowClass, "aa",

  WS_OVERLAPPED|WS_MINIMIZE|WS_SYSMENU|WS_CAPTION|WS_MINIMIZEBOX,

  50,//CW_USEDEFAULT,

  50,//0,

  385,//CW_USEDEFAULT,

  292,//0,

  NULL, NULL, hInstance, NULL);

 if (!hWnd)

 {

  return FALSE;

 }

 ShowWindow(hWnd, nCmdShow);

 UpdateWindow(hWnd);

 return TRUE;

}

int APIENTRY WinMain(HINSTANCE hInstance,

                     HINSTANCE hPrevInstance,

                     LPSTR     lpCmdLine,

                     int       nCmdShow)

{

 // set window class name

 strncpy(szWindowClass, "abc", BUFSIZ);

 // register window class

 MyRegisterClass(hInstance);

 // create window and show it

 InitInstance(hInstance, nCmdShow);

 // enter massage loop

 MSG  msg;

 while (GetMessage(&msg, NULL, 0, 0))

 {

  //if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

  {

   TranslateMessage(&msg);

   DispatchMessage(&msg);

  }

 }

 return 0;

}

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

{

 int  nCtrlId = LOWORD(wParam);

 switch (msg)

 {

 case WM_CREATE:

  CreateWindow("button", "選擇一", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,

   20, 30, 70, 24, hWnd, (HMENU)1010, ((LPCREATESTRUCT)lParam)->hInstance, NULL );

  CreateWindow("button", "選擇二", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,

   100, 30, 70, 24, hWnd, (HMENU)1020, ((LPCREATESTRUCT)lParam)->hInstance, NULL );

  CreateWindow("button", "選擇三", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,

   180, 30, 70, 24, hWnd, (HMENU)1030, ((LPCREATESTRUCT)lParam)->hInstance, NULL );

  CreateWindow("button", "請選擇", WS_CHILD | WS_VISIBLE | BS_GROUPBOX,

   10, 10, 260, 60, hWnd, (HMENU)1040, ((LPCREATESTRUCT)lParam)->hInstance, NULL );

  CreateWindow("button", "點選我..", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,

   280, 30, 70, 24, hWnd, (HMENU)1050, ((LPCREATESTRUCT)lParam)->hInstance, NULL );

  break;

 case WM_COMMAND:

  switch (nCtrlId)

  {

  case 1010:

   MessageBox(hWnd, "radio one", "aa", MB_OK);

   break;

  case 1050:

   MessageBox(hWnd, "clicked me", "note", MB_OK);

   break;

  }

  break;

 case WM_CLOSE:

  PostQuitMessage(0);

  break;

 default:

  return DefWindowProc(hWnd, msg, wParam, lParam);

 }

 return 0L;

}

繼續閱讀