程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C語言實現界面(不通過MFC\避免遺忘),mfc

C語言實現界面(不通過MFC\避免遺忘),mfc

編輯:C++入門知識

C語言實現界面(不通過MFC\避免遺忘),mfc


感覺MFC不屬於程序員細究的東西,今實現基本界面避免日後遺忘。

源代碼:

#include<windows.h>
#include<stdio.h>
char str[] = {'0'};
char cmd[] = {'0'};
//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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved