dev c++实现图形界面定时关机(windows)


//shutdown_windows.c
#include<windows.h>
#include<stdio.h>
char str[];
char cmd[]; 
//char hour[10], minute[10];
int num;
int num_text_hour, num_text_minute;
//char num_str[]; 
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{
    HWND hWnd;
    MSG Msg;
    WNDCLASS WndClass;
    WndClass.style=CS_HREDRAW|CS_VREDRAW;
    WndClass.lpfnWndProc=WndProc;
    WndClass.cbClsExtra=0;
    WndClass.cbWndExtra=0;
    WndClass.hInstance=hInstance;
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.lpszMenuName=NULL;
    WndClass.lpszClassName="Hello Win";   //窗口类名
    //注册窗口
    if(!RegisterClass(&WndClass))
    {
        MessageBox(NULL,"窗口注册失败!","Hello Win",0);
        return 0;
    }
    //创建窗口
    hWnd=CreateWindow("Hello Win", "定时关机--junmuzi", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    //显示窗口
    ShowWindow(hWnd,nCmdShow);   
    //更新窗口
    UpdateWindow(hWnd);
    //进入消息循环:当从应用程序消息队列中捡取的消息是WM_QUIT时,则推出循环
    while(GetMessage(&Msg,NULL,0,0))
    {
      TranslateMessage(&Msg);  //转换键盘消息
      DispatchMessage(&Msg);   //分发消息
    }
    return Msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    HDC hDC;
    PAINTSTRUCT Ps;
    char strEdit_hour[10], strEdit_minute[10];
    static HWND hWndButton_ok, hWndButton_cancel, hWndEdit_hour, hWndEdit_minute;
    switch(message)
    {
      case WM_CREATE:
           hWndEdit_hour = CreateWindow("edit",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,10,60,100,25,hWnd,NULL,NULL,NULL);
           hWndEdit_minute = CreateWindow("edit",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,180,60,100,25,hWnd,NULL,NULL,NULL);
           hWndButton_ok = CreateWindow("button","确定",WS_CHILD|WS_VISIBLE|WS_BORDER, 340, 60, 100, 25, hWnd,NULL,NULL,NULL);
           hWndButton_cancel = CreateWindow("button","取消定时关机",WS_CHILD|WS_VISIBLE|WS_BORDER, 460, 60, 100, 25, hWnd,NULL,NULL,NULL);
           return 0;
      case WM_COMMAND:
           if(((HWND)lParam==hWndButton_ok)&&(HIWORD(wParam)==BN_CLICKED))
           //按下按键hWndButton_ok
           {   
                num_text_hour = GetWindowText(hWndEdit_hour,strEdit_hour,10);     //获取编辑框控件hour的内容  
                //sprintf(str,"The result is: %s",strEdit_hour);
                //sprintf(hour, "%s", strEdit_hour); 
                if (num_text_hour == 0) 
                {
                    MessageBox(NULL,"小时数不能为空!","错误信息:", MB_OK);
                } 
                num_text_minute = GetWindowText(hWndEdit_minute,strEdit_minute,10);     //获取编辑框控件minute的内容
                if (num_text_minute == 0) 
                {
                    MessageBox(NULL,"分钟数不能为空!","错误信息:", MB_OK);
                } 
                if ((num_text_hour != 0) && (num_text_minute != 0))
                { 
                    num = atoi(strEdit_hour) * 3600 + atoi(strEdit_minute) * 60;    //把小时和分钟数转化为多少秒 
                    //itoa(num, num_str, 10); 
                    //sprintf(str,"The result is: %s",strEdit_minute);
                    //sprintf(minute, "%s", strEdit_minute); 
                    //strcat(cmd, str_); 
                    //sprintf(cmd, "shutdown -s -t %s %s %d", strEdit_hour, strEdit_minute, num);
                    sprintf(cmd, "shutdown -s -t %d", num);    // 
                    sprintf(str, "电脑会在%s小时%s分钟后关机!!!", strEdit_hour, strEdit_minute);
                    system(cmd);     //shutdown the computer. 
                    InvalidateRect(hWnd,NULL,TRUE);
                } 
           }
           if(((HWND)lParam == hWndButton_cancel)&&(HIWORD(wParam) == BN_CLICKED))
           //按下按键hWndButton_cancel
           {   
                sprintf(cmd, "shutdown -a");    // 
                sprintf(str, "电脑定时关机被取消!!!");
                system(cmd);     //shutdown the computer. 
                InvalidateRect(hWnd,NULL,TRUE);
           } 
      case WM_PAINT://设计编辑框
           hDC=BeginPaint(hWnd,&Ps);
           TextOut(hDC,10,10,"请输入你要设置的多长时间后关机(小时和分钟数):",48);
           TextOut(hDC,120,60,"小时",4);
           TextOut(hDC,290,60,"分钟",4);
           //TextOut(hDC,10,90,str,strlen(str));
           TextOut(hDC,10,90,str,strlen(str));
           EndPaint(hWnd,&Ps);
           return 0;
      case WM_DESTROY:
           PostQuitMessage(0);
           return 0;
    }
    return DefWindowProc(hWnd,message,wParam,lParam);
}










  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值