c/C++ 【Win32】 创建一个 Button

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
 
创建一个 Button
HWND  hwndButton
hwndButton = CreateWindow(
    " BUTTON",   // predefined class    不区分大小写
    "OK",       // button text
     WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // styles     注意 如果样式写错了 Button 将不会正常显示

    // Size and position values are given explicitly, because
    // the CW_USEDEFAULT constant gives zero values for buttons.
    10,         // starting x position
    10,         // starting y position
    100,        // button width
    100,        // button height
    hwnd,       // parent window
    NULL,       // No menu
    (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
    NULL);      // pointer not needed
================================================
  1. #include <windows.h>  
  2.   
  3. //expected constructor, destructor, or type conversion before  
  4. // LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);  
  5.  LRESULT CALLBACK WndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);  
  6.   
  7. int WINAPI  WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  
  8.         PSTR szCmdLine, int iCmdShow){  
  9.   
  10.     static TCHAR szAppName[] = TEXT("hello");  
  11.     HWND hwnd;  
  12.     MSG msg;  
  13.     WNDCLASS wndclass;  
  14.   
  15.     wndclass.style = CS_HREDRAW | CS_VREDRAW;  
  16.     wndclass.lpfnWndProc = WndProc;  
  17.     wndclass.cbClsExtra = 0;  
  18.     wndclass.cbWndExtra = 0;  
  19.     wndclass.hInstance = hInstance;  
  20.     // 加载 图标供程序使用 IDI-图示ID  
  21.     wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);  
  22.     // 加载 鼠标光标供程序使用  IDC-游标ID  
  23.     wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);  
  24.     //GetStockObject 取得一个图形对象(此例中是取得窗口背景的画刷对象)  
  25.     wndclass.hbrBackground =  (HBRUSH)GetStockObject(WHITE_BRUSH);  
  26.     wndclass.lpszMenuName = NULL;  
  27.     wndclass.lpszClassName = szAppName;  
  28.   
  29.     // 窗口依照某一窗口类别建立,窗口类别用以标识处理窗口消息的窗口消息处理程序。  
  30.     // 注册窗口  
  31.     if(!RegisterClass(&wndclass)){  
  32.         MessageBox(NULL, TEXT("This program requires Windows NT!"),  
  33.                 szAppName,MB_ICONERROR);  
  34.         return 0;  
  35.     }  
  36.     // 根据窗口类别 WndClass 建立一个窗口  
  37.     hwnd = CreateWindow(szAppName,  // window class name  
  38.             TEXT("Hello world"),    // window caption  
  39.             WS_OVERLAPPEDWINDOW,    // window style  
  40.             CW_USEDEFAULT,  
  41.             CW_USEDEFAULT,  
  42.             CW_USEDEFAULT,  
  43.             CW_USEDEFAULT,  
  44.             NULL,   // parent window handle  
  45.             NULL,   // window menu handle  
  46.             hInstance,//program instance handle  
  47.             NULL);  // creation parameters  
  48.     // 在屏幕上显示窗口  
  49.     ShowWindow(hwnd,iCmdShow);  
  50.     // 指示窗口自我更新  
  51.     UpdateWindow(hwnd);  
  52.     // 从消息队列中取得消息  
  53.     while(GetMessage(&msg,NULL,0,0)){  
  54.         TranslateMessage(&msg);  
  55.         DispatchMessage(&msg);  
  56.     }  
  57.     return msg.wParam;  
  58. }  
  59.   
  60. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
  61.   
  62. {  
  63.     HWND hwndButton;  
  64.     HDC  hdc ;  
  65.     PAINTSTRUCT ps ;  
  66.     RECT rect ;  
  67.     char* str;  
  68.     switch (message)  
  69.     {  
  70.     case WM_CREATE:  
  71.     //PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;  
  72.         // 创建一个按钮  
  73.         hwndButton = CreateWindow(  
  74.             "BUTTON",   // predefined class  
  75.             "OK",       // button text  
  76.             WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // styles  
  77.   
  78.             // Size and position values are given explicitly, because  
  79.             // the CW_USEDEFAULT constant gives zero values for buttons.  
  80.             10,         // starting x position  
  81.             10,         // starting y position  
  82.             100,        // button width  
  83.             100,        // button height  
  84.             hwnd,       // parent window  
  85.             NULL,       // No menu  
  86.             (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),  
  87.             NULL);      // pointer not needed  
  88.          //MoveWindow(hwndSmaller, 100,100,  100, 100, TRUE) ;  
  89.         return 0 ;  
  90.     case   WM_PAINT:  
  91.         // 绘制窗口  
  92.         hdc = BeginPaint (hwnd, &ps) ;  
  93.         // 取得窗口显示区域的大小  
  94.         GetClientRect (hwnd, &rect) ;  
  95.         str = "hello world!!!";  
  96.         // 显示字符串  
  97.         // DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,  
  98.         //    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;  
  99.           
  100.         // 输出字符串  
  101.         TextOut(hdc,20,20, str,strlen(str));  
  102.   
  103.   
  104.         EndPaint (hwnd, &ps) ;  
  105.         return 0 ;  
  106.     case   WM_DESTROY:  
  107.         // 再消息队列中插入一个[退出程序]消息  
  108.         PostQuitMessage (0) ;  
  109.         return 0 ;  
  110.     }  
  111.     // 执行内定的消息处理  
  112.   return DefWindowProc (hwnd, message, wParam, lParam) ;  
  113. }  
#include <windows.h>//expected constructor, destructor, or type conversion before// LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK WndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  PSTR szCmdLine, int iCmdShow){ static TCHAR szAppName[] = TEXT("hello"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; // 加载 图标供程序使用 IDI-图示ID wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); // 加载 鼠标光标供程序使用  IDC-游标ID wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); //GetStockObject 取得一个图形对象(此例中是取得窗口背景的画刷对象) wndclass.hbrBackground =  (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; // 窗口依照某一窗口类别建立,窗口类别用以标识处理窗口消息的窗口消息处理程序。 // 注册窗口 if(!RegisterClass(&wndclass)){  MessageBox(NULL, TEXT("This program requires Windows NT!"),    szAppName,MB_ICONERROR);  return 0; } // 根据窗口类别 WndClass 建立一个窗口 hwnd = CreateWindow(szAppName, // window class name   TEXT("Hello world"), // window caption   WS_OVERLAPPEDWINDOW, // window style   CW_USEDEFAULT,   CW_USEDEFAULT,   CW_USEDEFAULT,   CW_USEDEFAULT,   NULL, // parent window handle   NULL, // window menu handle   hInstance,//program instance handle   NULL); // creation parameters // 在屏幕上显示窗口 ShowWindow(hwnd,iCmdShow); // 指示窗口自我更新 UpdateWindow(hwnd); // 从消息队列中取得消息 while(GetMessage(&msg,NULL,0,0)){  TranslateMessage(&msg);  DispatchMessage(&msg); } return msg.wParam;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ HWND hwndButton;    HDC  hdc ;    PAINTSTRUCT ps ;    RECT rect ;    char* str;    switch (message)    {    case WM_CREATE:    //PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;     // 创建一个按钮     hwndButton = CreateWindow(         "BUTTON",   // predefined class         "OK",       // button text         WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // styles         // Size and position values are given explicitly, because         // the CW_USEDEFAULT constant gives zero values for buttons.         10,         // starting x position         10,         // starting y position         100,        // button width         100,        // button height         hwnd,       // parent window         NULL,       // No menu         (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),         NULL);      // pointer not needed      //MoveWindow(hwndSmaller, 100,100,  100, 100, TRUE) ;        return 0 ;    case   WM_PAINT:  // 绘制窗口  hdc = BeginPaint (hwnd, &ps) ;  // 取得窗口显示区域的大小  GetClientRect (hwnd, &rect) ;  str = "hello world!!!";  // 显示字符串  // DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,  //    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;    // 输出字符串  TextOut(hdc,20,20, str,strlen(str));  EndPaint (hwnd, &ps) ;  return 0 ;    case   WM_DESTROY:  // 再消息队列中插入一个[退出程序]消息  PostQuitMessage (0) ;  return 0 ;    }    // 执行内定的消息处理  return DefWindowProc (hwnd, message, wParam, lParam) ;}
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值