16,548
社区成员




#include <windows.h>
#include "resource.h"
#define ID_TIMER1 1
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,
WPARAM wParam,LPARAM lParam);
INT_PTR CALLBACK CloseWin_Proc(HWND hwnd,UINT message,
WPARAM wParam,LPARAM lParam);
HINSTANCE hInst=0;
HWND hbut1=0;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=DLGWINDOWEXTRA;
wndclass.hbrBackground=(HBRUSH)COLOR_WINDOWFRAME;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=TEXT("ALLINONE");
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
hInst=hInstance;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,
TEXT("必须运行在Window NT 下!"),
TEXT("错误"),MB_OK);
return 0;
}
hwnd=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_MAIN),
NULL,NULL);
ShowWindow(hwnd,iCmdShow);
while(GetMessage(&msg,NULL,NULL,NULL))
{
if(hbut1==NULL||!IsDialogMessage(hbut1,&msg))
{
DispatchMessage(&msg);
TranslateMessage(&msg);
}
}
return (int)msg.lParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,
WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON1://创建关机对话框
hbut1=CreateDialog(hInst,MAKEINTRESOURCE(IDD_BUTTON1),hwnd,CloseWin_Proc);
ShowWindow(hwnd,SW_HIDE);
return 0;
}
break;
case WM_CTLCOLORSTATIC:
return (LRESULT)GetStockObject(WHITE_BRUSH);
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
INT_PTR CALLBACK CloseWin_Proc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HWND hwndP=GetParent(hwnd);
SYSTEMTIME st;
static TCHAR time[50];
static int ci;
static int hour,minutes;
static BOOL close=FALSE;
switch(message)
{
case WM_INITDIALOG:
//填充combox列表
SetTimer(hwnd,ID_TIMER1,1000,NULL);
for(ci=0;ci<24;ci++)
{
if(ci<=9)
{
wsprintf(time,TEXT("0%d"),ci);
}
else
wsprintf(time,TEXT("%d"),ci);
SendMessage(GetDlgItem(hwnd,IDC_COMBO1),
CB_ADDSTRING,0,LPARAM(time));
}
for(ci=0;ci<=60;ci++)
{
if(ci<=9)
wsprintf(time,TEXT("0%d"),ci);
else
wsprintf(time,TEXT("%d"),ci);
SendMessage(GetDlgItem(hwnd,IDC_COMBO2),CB_ADDSTRING,
0,LPARAM(time));
}
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
//COMBOX的时间选择
case IDC_COMBO1:
if(HIWORD(wParam)==LBN_SELCHANGE)
hour=SendMessage((HWND)lParam,CB_GETCURSEL,0,0);
return TRUE;
case IDC_COMBO2:
if(HIWORD(wParam)==LBN_SELCHANGE)
minutes=SendMessage((HWND)lParam,CB_GETCURSEL,0,0);
return TRUE;
case IDC_OK://隐藏窗口,显示主窗口
close=TRUE;
ShowWindow(hwnd,SW_HIDE);
ShowWindow(hwndP,SW_SHOW);
return TRUE;
case IDC_CANCLE:
SendMessage(hwnd,WM_CLOSE,0,0);
return TRUE;
}
return FALSE;
case WM_TIMER:
GetLocalTime(&st);//获得系统时间
GetTimeFormat(NULL,
TIME_FORCE24HOURFORMAT,
&st,
TEXT("HH':'mm':'ss"),
time+wsprintf(time,TEXT(" %d-%d-%d "),st.wYear,st.wMonth,st.wDay),
50);
SetWindowText(GetDlgItem(hwnd,IDC_TIME),time);//更新时间显示
if(close && (st.wHour==hour) && (st.wMinute==minutes))//关机条件
{
//运行一下就会发现,这个消息框一直出..怎么回事
MessageBox(hwnd,TEXT("WM_TIMER 关机消息发送!"),TEXT("提示"),MB_OK);
//是否成功
//ExitWindowsEx(EWX_SHUTDOWN|EWX_POWEROFF,0);//关机函数
//SendMessage(hwnd,WM_CLOSE,0,0);
SendMessage(hwndP,WM_DESTROY,0,0);//退出程序
}
return TRUE;
case WM_CLOSE:
close=FALSE;
ShowWindow(hwndP,SW_SHOW);
hbut1=NULL;
KillTimer(hwnd,ID_TIMER1);
DestroyWindow(hwnd);
return TRUE;
}
return FALSE;
}
if(close && (st.wHour==hour) && (st.wMinute==minutes))//关机条件
{
KillTimer(ID_TIMER1); //添加一句,关闭定时器
//运行一下就会发现,这个消息框一直出..怎么回事
MessageBox(hwnd,TEXT("WM_TIMER 关机消息发送!"),TEXT("提示"),MB_OK);
//是否成功
//ExitWindowsEx(EWX_SHUTDOWN|EWX_POWEROFF,0);//关机函数
//SendMessage(hwnd,WM_CLOSE,0,0);
SendMessage(hwndP,WM_DESTROY,0,0);//退出程序
}