• win32窗口添加按钮及按钮消息相应,动态显示时间


    1、首先建立一个win32项目

    2、建立晚工程之后找到回调函数WndProc(HWND, UINT, WPARAM, LPARAM)的实现部分

    在case语句中WM_CREATE三个CreateWindow分别创建按钮和显示时间的lable,建议创建lable的句柄hLadle 设为全局变量方便用。其中用于放时间的数组sTimeBuf也建议设为

    全局的数组。

      1 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
      2 {
      3     int wmId, wmEvent;
      4     PAINTSTRUCT ps;
      5     HDC hdc;
      6     HWND hwndButtonCintinue ;
      7     HWND hwndButtonExit ;
      8 
      9     switch (message)
     10     {
     11     case WM_CREATE:
     12         {
     13             hwndButtonCintinue=CreateWindow(TEXT("button"),//必须为:button    
     14                 TEXT("继续导入"),//按钮上显示的字符    
     15                 WS_CHILD | WS_VISIBLE,  
     16                 100,240,100,50,  //按钮在界面上出现的位置
     17                 hWnd,(HMENU)IDC_BUTTON_CONTINUE,  //设置按钮IDIDC_BUTTON_CONTINUE = 131自己定义ID
     18                 ((LPCREATESTRUCT)lParam)->hInstance,NULL); 
     19 
     20             hwndButtonExit=CreateWindow(TEXT("button"),//必须为:button    
     21                 TEXT("退出"),//按钮上显示的字符    
     22                 WS_CHILD | WS_VISIBLE,  
     23                 400,240,100,50,  //按钮在界面上出现的位置
     24                 hWnd,(HMENU)IDC_BUTTON_EXIT,  //设置按钮ID IDC_BUTTON_EXIT =132自己定义ID
     25                 ((LPCREATESTRUCT)lParam)->hInstance,NULL); 
     26             
     27             //获取系统时间并显示到界面上
     28             SYSTEMTIME time;  
     29             GetLocalTime(&time);
     30             sprintf_s(sTimeBuf, "%d:%d:%d", time.wHour, time.wMinute, time.wSecond);
     31             SetTimer(hWnd, 22, 1000, NULL);//设定时器
     32 
     33             WCHAR *swTimeBuf = charToWchar(sTimeBuf);
     34             hLadle = CreateWindow(TEXT("static"), 
     35                 swTimeBuf, 
     36                 WS_CHILD | WS_VISIBLE | WS_TABSTOP, 
     37                 0, 48, 57, 16, 
     38                 hWnd, 
     39                 HMENU(21), 
     40                 ((LPCREATESTRUCT)lParam)->hInstance, NULL);
     41         }
     42         break;
     43     case WM_TIMER:
     44         {
     45             switch(wParam)
     46             {
     47             case 22:
     48                 {
     49                     SYSTEMTIME time; 
     50                     GetLocalTime(&time);
     51                     sprintf_s(sTimeBuf, "%d:%d:%d", time.wHour, time.wMinute, time.wSecond);
     52                     break;
     53                 } 
     54             default:
     55                 break;
     56             } 
     57             WCHAR *swTimeBuf = charToWchar(sTimeBuf);
     58             SetWindowText(hLadle, swTimeBuf);
     59         }
     60         break;
     61     case WM_COMMAND:
     62         wmId    = LOWORD(wParam);
     63         wmEvent = HIWORD(wParam);
     64         // 分析菜单选择:
     65         switch (wmId)
     66         {
     67         case IDC_BUTTON_CONTINUE://继续导入按钮的响应
     68             {
     69                 //MessageBox(NULL,L"继续导入",L"提示",NULL);
     70                 HWND hDialog = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hWnd, About_Dialog2);
     71 
     72                 //初始化窗口标题栏,并显示窗口
     73                 SetWindowText(hDialog,L"我的对话框啊");  
     74                 ShowWindow(hDialog,SW_SHOWNA);  
     75                 UpdateWindow(hDialog);
     76                 break;
     77             }
     78         case IDC_BUTTON_EXIT://退出按钮的响应
     79             {
     80                 //MessageBox(hWnd,L"退出按钮",L"提示",NULL);
     81                 DestroyWindow(hWnd);
     82                 break;
     83             }
     84         case IDM_ABOUT:
     85             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
     86             break;
     87         case IDM_EXIT:
     88             DestroyWindow(hWnd);
     89             break;
     90         default:
     91             return DefWindowProc(hWnd, message, wParam, lParam);
     92         }
     93         break;
     94     case WM_PAINT:
     95         hdc = BeginPaint(hWnd, &ps);
     96         // TODO: 在此添加任意绘图代码...
     97         EndPaint(hWnd, &ps);
     98         break;
     99     case WM_DESTROY:
    100         PostQuitMessage(0);
    101         break;
    102     default:
    103         return DefWindowProc(hWnd, message, wParam, lParam);
    104     }
    105     return 0;
    106 }

    将char型转换成wchar型

     1 //功     能:char字符串转化为wchar字符串
     2 WCHAR * charToWchar(const char *s)
     3 {
     4     int w_nlen=MultiByteToWideChar(CP_ACP,0,s,-1,NULL,0);
     5     WCHAR *ret;
     6     ret=(WCHAR*) malloc(sizeof(WCHAR)*w_nlen);
     7     memset(ret,0,sizeof(ret));
     8     MultiByteToWideChar(CP_ACP,0,s,-1,ret,w_nlen);
     9     return ret;
    10 }

    在继续导入按钮点击之后会弹出对话框,可以在系统的基础上直接改IDD_DIALOG1,也可以自己新建一个,我是复制了原来的,然后产生了新的IDIDD_DIALOG2,这个ID号在资源文件中更改,在Reaource.h中。CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hWnd, About_Dialog2);

     1 // “关于”对话框2框的消息处理程序。
     2 INT_PTR CALLBACK About_Dialog2(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
     3 {
     4     UNREFERENCED_PARAMETER(lParam);
     5     int dwmId;
     6     int ret_num;
     7     switch (message)
     8     {
     9     case WM_INITDIALOG:
    10         return (INT_PTR)TRUE;
    11 
    12     case WM_COMMAND:
    13         dwmId  = LOWORD(wParam);
    14         switch (dwmId)
    15         {
    16         case IDOK:
    17             {
    18               ret_num = GetDlgItemText(hDlg,IDC_EDIT1,sDialogBuf,sizeof(sDialogBuf) );
    19               if (strlen((char *)sDialogBuf) <= 0 )
    20               {
    21                   MessageBox(NULL,L"输入为空请重新输入",L"提示",NULL);
    22               }
    23               else
    24               {
    25                   MessageBox(NULL,sDialogBuf,L"提示",NULL);
    26                   EndDialog(hDlg, LOWORD(wParam));
    27                   return (INT_PTR)TRUE;
    28               }
    29             }
    30             break;
    31         case IDCANCEL:
    32             {
    33                 EndDialog(hDlg, LOWORD(wParam));
    34                 return (INT_PTR)TRUE;
    35             }
    36             break;
    37         default:
    38             break;
    39         }
    40     }
    41     return (INT_PTR)FALSE;
    42 }

  • 相关阅读:
    20145229吴姗珊《信息安全系统设计基础》第3周学习总结
    20145229《信息安全系统设计基础》第2周学习总结
    20145207 myeclipse测试
    20145207 ms11_050漏洞攻击
    20145207 ms08_067攻击实验
    20145207《Java程序设计》实验一(Java开发环境的熟悉)实验报告
    20145207《Java程序设计》第7周学习总结
    20145207李祉昂《网络对抗技术》恶意代码分析
    20145207《Java程序设计》第6周学习总结
    20145207《网络对抗》免杀原理与实践
  • 原文地址:https://www.cnblogs.com/LYF-LIUDAO/p/7027902.html
Copyright © 2020-2023  润新知