加法器小改进成支持加减运算,遇到一个问题, 在 WM_COMMAND 的处理中实现不了改变运算符号,不知道是 BS_AUTOCHECKBOX 的用法错误了,还是其他原因,请大家指教,代码如下:
#include <windows.h>
#include <tchar.h>
#include <stdlib.h>
#include <stdio.h>
HWND hwin;
HWND hedt1;
HWND hedt2;
HWND hbtn;
HWND hresult;
HWND hsta1;
HWND hsta2;
HWND hbtn_op;
int op=0;LRESULT CALLBACK MyWinProc(HWND hwnd,UINT uMsg,WPARAM wParam, LPARAM lParam)
{
int r;
double x;
double y;
char buf[30];
WORD w;
HWND htmp;
LRESULT res; switch (uMsg)
{
case WM_SYSCOMMAND:
w=(UINT)wParam;
if(w==SC_MINIMIZE || w==SC_MAXIMIZE )
{
return 0;
}
case WM_COMMAND:
htmp =(HWND)lParam;
if( htmp == hbtn_op) //改变运算符号
{
res = SendMessage( hbtn_op,BM_GETSTATE,0,0);
if( res == BST_CHECKED )
{
op =0;
SetWindowText(hsta1,"+");
}
if( res == BST_UNCHECKED )
{
op =1;
SetWindowText(hsta1,"-");
}
return 0;
}
if( htmp == hbtn )
{
memset(buf,0,30);
GetWindowText(hedt1,buf,29);
x=atof(buf);
if( x==0 )
{
MessageBox(hwnd,"输入的数据有误!","错误",MB_OK);
return 0;
}
memset(buf,0,30);
GetWindowText(hedt2,buf,29);
y=atof(buf);
if( y==0 )
{
MessageBox(hwnd,"输入的数据有误!","错误",MB_OK);
return 0;
}
switch (op)
{
case 0:
x+=y;
break;
case 1:
x=y;
break;
}
sprintf(buf,"%g",x);
SetWindowText(hresult,buf);
return 0; } if( htmp==hedt1||htmp==hedt2 )//有输入清空结果
{
SetWindowText(hresult,"");
return 0;
}
break;
case WM_CLOSE:
r=MessageBox(hwnd,"是否退出","退出",MB_YESNO);
if(r==IDYES)
{
DestroyWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hins, HINSTANCE hpre,LPSTR cmd,int showtype)
{
WNDCLASSEX wnd;
MSG msg;
BOOL bRet; wnd.cbClsExtra=0;
wnd.cbSize=sizeof(wnd);
wnd.cbWndExtra=0;
wnd.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
wnd.hCursor=0;
wnd.hIcon=0;
wnd.hIconSm=0;
wnd.hInstance=GetModuleHandle(0);
wnd.lpfnWndProc= MyWinProc;
wnd.lpszClassName="mywin";
wnd.lpszMenuName=0;
wnd.style=CS_HREDRAW|CS_VREDRAW; RegisterClassEx(&wnd);
hwin=CreateWindowEx(WS_EX_CLIENTEDGE,"mywin","加法器",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
0,0,400,300,0,0,hins,0);
hedt1=CreateWindowEx(0,_T("EDIT"),_T(""),WS_CHILD|WS_VISIBLE,
50,60,100,20,hwin,0,hins,0);
hedt2=CreateWindowEx(0,_T("EDIT"),_T(""),WS_CHILD|WS_VISIBLE,
200,60,100,20,hwin,0,hins,0);
hbtn=CreateWindowEx(0,_T("BUTTON"),_T("计算结果"),WS_CHILD|WS_VISIBLE,
120,90,100,30,hwin,0,hins,0);
hsta1=CreateWindowEx(0,_T("STATIC"),_T("+"),WS_CHILD|WS_VISIBLE,
170,60,10,20,hwin,0,hins,0);
hresult=CreateWindowEx(0,_T("STATIC"),_T(""),WS_CHILD|WS_VISIBLE,
150,140,200,20,hwin,0,hins,0);
hsta2=CreateWindowEx(0,_T("STATIC"),_T("结果是:"),WS_CHILD|WS_VISIBLE,
50,140,70,30,hwin,0,hins,0);
hbtn_op=CreateWindowEx(0,_T("BUTTON"),_T("加/减 选择"),WS_CHILD|WS_VISIBLE | BS_AUTOCHECKBOX ,
120,1,120,30,hwin,0,hins,0);
SendMessage( hbtn_op,BM_SETCHECK,BST_CHECKED,0);
while( ( bRet=GetMessage(&msg,0,0,0) ) !=0 )
{
if( bRet==-1)
{
return -1;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
#include <windows.h>
#include <tchar.h>
#include <stdlib.h>
#include <stdio.h>
HWND hwin;
HWND hedt1;
HWND hedt2;
HWND hbtn;
HWND hresult;
HWND hsta1;
HWND hsta2;
HWND hbtn_op;
int op=0;LRESULT CALLBACK MyWinProc(HWND hwnd,UINT uMsg,WPARAM wParam, LPARAM lParam)
{
int r;
double x;
double y;
char buf[30];
WORD w;
HWND htmp;
LRESULT res; switch (uMsg)
{
case WM_SYSCOMMAND:
w=(UINT)wParam;
if(w==SC_MINIMIZE || w==SC_MAXIMIZE )
{
return 0;
}
case WM_COMMAND:
htmp =(HWND)lParam;
if( htmp == hbtn_op) //改变运算符号
{
res = SendMessage( hbtn_op,BM_GETSTATE,0,0);
if( res == BST_CHECKED )
{
op =0;
SetWindowText(hsta1,"+");
}
if( res == BST_UNCHECKED )
{
op =1;
SetWindowText(hsta1,"-");
}
return 0;
}
if( htmp == hbtn )
{
memset(buf,0,30);
GetWindowText(hedt1,buf,29);
x=atof(buf);
if( x==0 )
{
MessageBox(hwnd,"输入的数据有误!","错误",MB_OK);
return 0;
}
memset(buf,0,30);
GetWindowText(hedt2,buf,29);
y=atof(buf);
if( y==0 )
{
MessageBox(hwnd,"输入的数据有误!","错误",MB_OK);
return 0;
}
switch (op)
{
case 0:
x+=y;
break;
case 1:
x=y;
break;
}
sprintf(buf,"%g",x);
SetWindowText(hresult,buf);
return 0; } if( htmp==hedt1||htmp==hedt2 )//有输入清空结果
{
SetWindowText(hresult,"");
return 0;
}
break;
case WM_CLOSE:
r=MessageBox(hwnd,"是否退出","退出",MB_YESNO);
if(r==IDYES)
{
DestroyWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hins, HINSTANCE hpre,LPSTR cmd,int showtype)
{
WNDCLASSEX wnd;
MSG msg;
BOOL bRet; wnd.cbClsExtra=0;
wnd.cbSize=sizeof(wnd);
wnd.cbWndExtra=0;
wnd.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
wnd.hCursor=0;
wnd.hIcon=0;
wnd.hIconSm=0;
wnd.hInstance=GetModuleHandle(0);
wnd.lpfnWndProc= MyWinProc;
wnd.lpszClassName="mywin";
wnd.lpszMenuName=0;
wnd.style=CS_HREDRAW|CS_VREDRAW; RegisterClassEx(&wnd);
hwin=CreateWindowEx(WS_EX_CLIENTEDGE,"mywin","加法器",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
0,0,400,300,0,0,hins,0);
hedt1=CreateWindowEx(0,_T("EDIT"),_T(""),WS_CHILD|WS_VISIBLE,
50,60,100,20,hwin,0,hins,0);
hedt2=CreateWindowEx(0,_T("EDIT"),_T(""),WS_CHILD|WS_VISIBLE,
200,60,100,20,hwin,0,hins,0);
hbtn=CreateWindowEx(0,_T("BUTTON"),_T("计算结果"),WS_CHILD|WS_VISIBLE,
120,90,100,30,hwin,0,hins,0);
hsta1=CreateWindowEx(0,_T("STATIC"),_T("+"),WS_CHILD|WS_VISIBLE,
170,60,10,20,hwin,0,hins,0);
hresult=CreateWindowEx(0,_T("STATIC"),_T(""),WS_CHILD|WS_VISIBLE,
150,140,200,20,hwin,0,hins,0);
hsta2=CreateWindowEx(0,_T("STATIC"),_T("结果是:"),WS_CHILD|WS_VISIBLE,
50,140,70,30,hwin,0,hins,0);
hbtn_op=CreateWindowEx(0,_T("BUTTON"),_T("加/减 选择"),WS_CHILD|WS_VISIBLE | BS_AUTOCHECKBOX ,
120,1,120,30,hwin,0,hins,0);
SendMessage( hbtn_op,BM_SETCHECK,BST_CHECKED,0);
while( ( bRet=GetMessage(&msg,0,0,0) ) !=0 )
{
if( bRet==-1)
{
return -1;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
x=y;
break;
你要减法,怎么是 x=y ?
-->
res = SendMessage( hbtn_op,BM_GETCHECK,0,0);switch (op)
{
case 0:
x+=y;
break;
case 1:
x=y;
break;
}
中的case 1:修改为
case 1:
x -= y;
break;