感觉MFC不属于程序员细究的东西,今实现基本界面避免日后遗忘。
源代码:
1 #include<windows.h> 2 #include<stdio.h> 3 char str[] = {'0'}; 4 char cmd[] = {'0'}; 5 //char hour[10], minute[10]; 6 int num; 7 int num_text_hour, num_text_minute; 8 //char num_str[]; 9 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); 10 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) 11 { 12 HWND hWnd; 13 MSG Msg; 14 WNDCLASS WndClass; 15 WndClass.style=CS_HREDRAW|CS_VREDRAW; 16 WndClass.lpfnWndProc=WndProc; 17 WndClass.cbClsExtra=0; 18 WndClass.cbWndExtra=0; 19 WndClass.hInstance=hInstance; 20 WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); 21 WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); 22 WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); 23 WndClass.lpszMenuName=NULL; 24 WndClass.lpszClassName="Hello Win"; //窗口类名 25 //注册窗口 26 if(!RegisterClass(&WndClass)) 27 { 28 MessageBox(NULL,"窗口注册失败!","Hello Win",0); 29 return 0; 30 } 31 //创建窗口 32 hWnd=CreateWindow("Hello Win", "定时关机--junmuzi", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 33 //显示窗口 34 ShowWindow(hWnd,nCmdShow); 35 //更新窗口 36 UpdateWindow(hWnd); 37 //进入消息循环:当从应用程序消息队列中捡取的消息是WM_QUIT时,则推出循环 38 while(GetMessage(&Msg,NULL,0,0)) 39 { 40 TranslateMessage(&Msg); //转换键盘消息 41 DispatchMessage(&Msg); //分发消息 42 } 43 return Msg.wParam; 44 } 45 46 47 LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) 48 { 49 HDC hDC; 50 PAINTSTRUCT Ps; 51 char strEdit_hour[10], strEdit_minute[10]; 52 static HWND hWndButton_ok, hWndButton_cancel, hWndEdit_hour, hWndEdit_minute; 53 switch(message) 54 { 55 case WM_CREATE: 56 hWndEdit_hour = CreateWindow("edit",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,10,60,100,25,hWnd,NULL,NULL,NULL); 57 hWndEdit_minute = CreateWindow("edit",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,180,60,100,25,hWnd,NULL,NULL,NULL); 58 hWndButton_ok = CreateWindow("button","确定",WS_CHILD|WS_VISIBLE|WS_BORDER, 340, 60, 100, 25, hWnd,NULL,NULL,NULL); 59 hWndButton_cancel = CreateWindow("button","取消定时关机",WS_CHILD|WS_VISIBLE|WS_BORDER, 460, 60, 100, 25, hWnd,NULL,NULL,NULL); 60 return 0; 61 case WM_COMMAND: 62 if(((HWND)lParam==hWndButton_ok)&&(HIWORD(wParam)==BN_CLICKED)) 63 //按下按键hWndButton_ok 64 { 65 num_text_hour = GetWindowText(hWndEdit_hour,strEdit_hour,10); //获取编辑框控件hour的内容 66 //sprintf(str,"The result is: %s",strEdit_hour); 67 //sprintf(hour, "%s", strEdit_hour); 68 if (num_text_hour == 0) 69 { 70 MessageBox(NULL,"小时不能为空!","错误信息:", MB_OK); 71 } 72 num_text_minute = GetWindowText(hWndEdit_minute,strEdit_minute,10); //获取编辑框控件minute的内容 73 if (num_text_minute == 0) 74 { 75 MessageBox(NULL,"分钟不能为空!","错误信息:", MB_OK); 76 } 77 if (!((atoi(strEdit_hour) >= 0) && (atoi(strEdit_minute) >= 0) && (atoi(strEdit_minute) <= 60))) 78 { 79 MessageBox(NULL,"非法输入(输入的小时必须大于等于0,输入的分钟必须大于等于0,且小于等于60)","错误信息:", MB_OK); 80 } 81 if ((num_text_hour != 0) && (num_text_minute != 0) && (atoi(strEdit_hour) >= 0) && (atoi(strEdit_minute) >= 0) && (atoi(strEdit_minute) <= 60)) 82 { 83 num = atoi(strEdit_hour) * 3600 + atoi(strEdit_minute) * 60; //把小时和分钟数转化为多少秒 84 //itoa(num, num_str, 10); 85 //sprintf(str,"The result is: %s",strEdit_minute); 86 //sprintf(minute, "%s", strEdit_minute); 87 //strcat(cmd, str_); 88 //sprintf(cmd, "shutdown -s -t %s %s %d", strEdit_hour, strEdit_minute, num); 89 sprintf(cmd, "shutdown -s -t %d", num); // 定时关机命令 90 sprintf(str, "电脑会在%s小时%s分钟后关机!!!", strEdit_hour, strEdit_minute); 91 system(cmd); //shutdown the computer. 92 InvalidateRect(hWnd,NULL,TRUE); 93 } 94 } 95 if(((HWND)lParam == hWndButton_cancel)&&(HIWORD(wParam) == BN_CLICKED)) 96 //按下按键hWndButton_cancel 97 { 98 sprintf(cmd, "shutdown -a"); //取消定时关机 99 sprintf(str, "电脑定时关机被取消!!!"); 100 system(cmd); //cancel ”shutdown the computer“. 101 InvalidateRect(hWnd,NULL,TRUE); 102 } 103 case WM_PAINT://设计编辑框 104 hDC=BeginPaint(hWnd,&Ps); 105 TextOut(hDC,10,10,"请输入你要设置的多长时间后关机(小时和分钟数):",48); 106 TextOut(hDC,120,60,"小时",4); 107 TextOut(hDC,290,60,"分钟",4); 108 //TextOut(hDC,10,90,str,strlen(str)); 109 TextOut(hDC,10,90,str,strlen(str)); 110 EndPaint(hWnd,&Ps); 111 return 0; 112 case WM_DESTROY: 113 PostQuitMessage(0); 114 return 0; 115 } 116 return DefWindowProc(hWnd,message,wParam,lParam); 117 } 118 119 120 121 //实现自动关机的程序