win32 api 创建窗口的一个简单例子

这篇博客通过一个简单的例子介绍了如何利用Win32 API在Windows XP操作系统下创建窗口,并添加按钮控件。文章涵盖了窗口类的注册、窗口的创建以及消息处理函数的实现,包括按钮的点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*使用ascci编码, vc6.0调试, xp os*/

 

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值