第一次照猫画虎的写了个win32程序,打算在程序中用system()执行外部命令,但好像有问题,不知道为什么。下面是代码,有点乱,请不要介意。
#include
#include
#include
#include
#include "resource.h"
const char szClassName[] = "myWindowClass";
NOTIFYICONDATA nid ;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE :
CreateWindow(TEXT("button"),TEXT("OK"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
100,120,100,30,hwnd,(HMENU)ID_BUTTON_OK,
((LPCREATESTRUCT)lParam)->hInstance,NULL);
CreateWindow(TEXT("edit"),TEXT(""),
WS_CHILD|WS_VISIBLE|ES_LEFT|ES_NUMBER,
125,70,50,30,
hwnd,(HMENU)ID_EDIT,((LPCREATESTRUCT)lParam)->hInstance,NULL);
break;
case WM_IAWENTRAY:
if(wParam == IDI_MYICON){
if(lParam == WM_LBUTTONDOWN){
ShowWindow(hwnd, SW_SHOWNORMAL);
printf("showWindows");
}
}
break;
case WM_SYSCOMMAND:
switch(wParam)
{
case SC_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
case SC_MINIMIZE:
ShowWindow(hwnd,SW_HIDE);
Shell_NotifyIcon(NIM_ADD,&nid);
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
break;
case WM_CLOSE :
DestroyWindow(hwnd);
break;
case WM_DESTROY :
Shell_NotifyIcon(NIM_DELETE,&nid);
PostQuitMessage(0);
break;
case WM_COMMAND :
switch(LOWORD(wParam)){
case ID_FILE_EXIT :
PostMessage(hwnd,WM_CLOSE,0,0);
break;
case ID_BUTTON_OK :
int length = GetWindowTextLength(GetDlgItem(hwnd, ID_EDIT));
char command[50] = "shutdown -s -t ";
if(length > 0) {
char message[100] = "系统将在" ;
char* buf = (char*)GlobalAlloc(GPTR, length + 1);
GetDlgItemText(hwnd,ID_EDIT,buf,length+1);
int time = atoi(buf);
time *= 60 ;
strcat(message,buf);
strcat(message,"分钟后关闭");
GlobalFree((HANDLE)buf);
itoa(time,buf,10);
strcat(command,buf);
GlobalFree((HANDLE)buf);
MessageBox(
NULL, TEXT(message), TEXT("提示"),
MB_OK | MB_ICONINFORMATION);
system(command); // 就是这里
SetWindowText((HWND)lParam,"Cancle");
PostMessage(hwnd,WM_SYSCOMMAND,SC_MINIMIZE,0);
}else{
MessageBox(
NULL, TEXT("请至少输入一些东西"), TEXT("警告"),
MB_OK | MB_ICONINFORMATION);
}
system(command);
break;
}
break;
default :
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd ;
MSG msg ;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(GetModuleHandle(NULL),IDC_ARROW);
wc.hbrBackground = (HBRUSH) COLOR_BACKGROUND ;
wc.hIconSm = LoadIcon(NULL,MAKEINTRESOURCE(IDI_MYICON));
wc.lpszClassName = szClassName ;
if(!RegisterClassEx(&wc)){
MessageBox(NULL,"some things happened in class","Error",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(0,szClassName,"ShutDown",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,320,240,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL){
MessageBox(NULL,"some things happened","Error",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hwnd;
nid.uID = IDI_MYICON;
nid.uCallbackMessage=WM_IAWENTRAY ;
nid.hIcon=LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
nid.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
_tcscpy(nid.szTip,TEXT("TimeWork正在工作!\r\n点击打开主界面"));
while(GetMessage(&msg, NULL, 0, 0) > 0){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。