编写的windows小程序运行后一闪而过,帮忙解决一下
写了一个小程序,可是运行时居然一闪而过,大家帮忙解决一下。
EditBoxDemo.c:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "EditBoxDemo.h "
HWND hEditItem,hEditResult,
hButtonAdd,hButtonReset,hButtonCancel,
hStatic1,hStatic2;
HINSTANCE hInst;
char lpszAddItem[] = " ";
char lpszResult[] = " ";
char *stop;
long nAddItem,nResult;
int nMax;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInst,
LPSTR lpszCmdLine,
int nCmdShow
)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[] = "编辑框 ";
char lpszTitle[] = "编辑框示例-加法器 ";
wndclass.style = 0;
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 = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszClassName;
if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return 0;
}
hwnd = CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
switch(message)
{
case WM_CREATE:
hStatic1 = CreateWindow
( "STATIC ",
"加数: ",
WS_CHILD|WS_VISIBLE,
40,20,
50,20,
hwnd,
(HMENU)IDS_1,
hInst,
NULL);
hStatic2 = CreateWindow
( "STATIC ",
"结果: ",
WS_CHILD|WS_VISIBLE,
40,70,
50,20,
hwnd,
(HMENU)IDS_2,
hInst,
NULL);
hEditItem = CreateWindow
( "EDIT ",
NULL,
WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER,
130,20,
80,20,
hwnd,
(HMENU)IDE_ADDITEM,
hInst,
NULL);
hEditResult= CreateWindow
( "EDIT ",
NULL,
WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER|ES_READONLY,
130,70,
80,20,
hwnd,
(HMENU)IDE_RESULT,
hInst,
NULL);
hButtonAdd= CreateWindow
( "BUTTON ",
"+ ",
WS_CHILD|WS_VISIBLE,
20,120,
70,20,
hwnd,
(HMENU)IDB_ADD,
hInst,
NULL);
hButtonReset= CreateWindow
( "BUTTON ",
"重新开始 ",
WS_CHILD|WS_VISIBLE,
100,120,
70,20,
hwnd,
(HMENU)IDB_RESET,
hInst,
NULL);
hButtonCancel= CreateWindow
( "BUTTON ",
"退出 ",
WS_CHILD|WS_VISIBLE,
180,120,
70,20,
hwnd,
(HMENU)IDB_CANCEL,
hInst,
NULL);
SetWindowText(hEditResult, "0 ");
break;
case WM_SETFOCUS:
SetFocus(hEditItem);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_ADD:
nMax = GetWindowTextLength(hEditItem)+1;
GetWindowText(hEditItem,lpszAddItem,nMax);
nAddItem = strtol(lpszAddItem,&stop,10);
nResult +=nAddItem;
_ltoa(nResult,lpszResult,10);
SetWindowText(hEditResult,lpszResult);
SetWindowText(hEditItem, " ");
SendMessage(hwnd,WM_SETFOCUS,0,0L);
break;
case IDB_RESET:
SetWindowText(hEditItem, " ");
SetWindowText(hEditResult, "0 ");
SendMessage(hwnd,WM_SETFOCUS,0,0L);
nResult = 0;
nAddItem =0;
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
//system( "pause ");
return 0;
}
EditBoxDemo.h:
#define IDS_1 101
#define IDS_2 102
#define IDE_RESULT 103
#define IDE_ADDITEM 104
#define IDB_RESET 105
#define IDB_CANCEL 106
#define IDB_ADD 107
[解决办法]
在 case WM_DESTROY: 前面添加一个break语句就OK了
VC6.0测试通过。
在嵌套的swich语句中经常会少些一个break,以后你可要
注意点!!1
[解决办法]
break;
case WM_DESTROY:
...