dev c++实现windows shutdown图形化界面(源代码如下)

#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 (!((atoi(strEdit_hour) >= 0) && (atoi(strEdit_minute) >= 0) && (atoi(strEdit_minute) <= 60)))
                {
                    MessageBox(NULL,"非法输入(输入的小时必须大于等于0,输入的分钟必须大于等于0,且小于等于60)","错误信息:", MB_OK);
                } 
                if ((num_text_hour != 0) && (num_text_minute != 0) && (atoi(strEdit_hour) >= 0) && (atoi(strEdit_minute) >= 0) && (atoi(strEdit_minute) <= 60))
                { 
                    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);     //cancel ”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
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Dev-C++是一个集成开发环境(IDE),它可以用于编写和运行C和C++程序。Dev-C++本身并不支持图形化界面,但可以通过使用EGE库来实现图形化界面。 EGE(Easy Graphics Engine)是一个简单易用的图形库,它可以在Dev-C++实现图形化界面。要在Dev-C++中使用EGE库进行图形编程,需要进行以下步骤: 1. 下载EGE库文件:从EGE官方网站(https://xege.org/)下载EGE库文件。 2. 复制库文件:将EGE库文件中的libgraphics64.a文件复制到Dev-C++安装目录下的MinGW32\lib\gcc\mingw32\9.2.0目录下(根据你的版本可能会有所不同)。 3. 创建一个新的Dev-C++项目:打开Dev-C++,点击菜单栏的"File",然后选择"New",再选择"Project"。在弹出的对话框中选择"C++ Project",然后点击"OK"。 4. 配置项目属性:在项目窗口中,右键点击项目名称,选择"Project Options"。在弹出的对话框中,选择"Parameters"选项卡,在"Linker"字段中添加以下参数:-lgraphics64 -luuid -lmsimg32 -lgdi32 -limm32 -lole32 -loleaut32。 5. 编写图形化界面代码:在项目窗口中,双击打开main.cpp文件。在文件中编写图形化界面的代码,例如: ```cpp #include <graphics.h> int main() { // 初始化为640*480大小 initgraph(640, 480); // 绘制图形 circle(320, 240, 100); // 等待用户按键 getch(); // 关闭图形界面 closegraph(); return 0; } ``` 6. 编译和运行程序:点击菜单栏的"Execute",然后选择"Compile & Run"。Dev-C++将会编译并运行你的程序,显示图形化界面。 请注意,以上步骤是基于使用EGE库来实现图形化界面的。如果你想使用其他图形库,可能需要进行不同的配置和操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值