#include <windows.h>
#include <math.h>
#include <stdio.h>
#include"resource.h"
#define wintopX 30
#define wintopY 50
#define winHigh 400
#define winWide 300
HINSTANCE hInst;
//求一元二次方程的根,函数返回根的个数
int GetRoot(float a,float b,float c,double *root)
{
double delta,deltasqrt;
delta=b*b-4*a*c;
//In the Select project configuration(s) to modify dialog box that appears, click the check boxes for all Release versions
if(delta<0.0)return 0;
deltasqrt=sqrt(delta);
if(a!=0.0)
{
root[0]=(-b+deltasqrt)/(2.0*a);
root[1]=(-b-deltasqrt)/(2.0*a);
}
else
if(b!=0.0)root[0]=root[1]=-c/b;
else return 0;
if(root[0]==root[1])return 1;
else return 2;
}
char strResultDown[80],strResultTop[30],strtop[30],strdown[80];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
char wname[10]="HelloWin";
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC)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 = wname;
wndclass.hIconSm = wndclass.hIcon;
hInst=hInstance;
if(!RegisterClassEx(&wndclass))
{MessageBox(NULL,"窗口注册失败!","HelloWin",0);
return 0;
}
//__________________________________________
hwnd = CreateWindow(wname, "计算二元一次方程的小东西---NBSKY",
WS_SYSMENU,wintopX, wintopY,
winWide, winHigh,
NULL, NULL, hInstance, NULL);
//_______________________________________
if(!hwnd)
{MessageBox(NULL,"窗口生成失败!","HelloWin",0);
return 0;
}
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)
{
PAINTSTRUCT ps;
HDC hdc;
int i,j;
static HWND hwndButton,hwndEdit[3],ClearButton;
char strEdit[80];
float a[3];
for(i=0;i<3;i++)
{
a[i]=0;
}
//初始显示
if(strResultTop[0]==NULL)
{
strcpy(strResultTop,"您欲求解的方程为:");
sprintf(strResultDown,"%f*x*x+%f* x+%f=0",a[0],a[1],a[2]);
}
//************END****
double root[2];
switch (message)
{ case WM_CREATE:
hwndEdit[0]=CreateWindow("edit",NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER,
20, 70, 70, 25, hwnd, NULL, NULL, NULL);
hwndEdit[1]=CreateWindow("edit",NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER,
110, 70, 70, 25, hwnd, NULL, NULL, NULL);
hwndEdit[2]=CreateWindow("edit",NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER,
200, 70, 70, 25, hwnd, NULL, NULL, NULL);
hwndButton=CreateWindow("button","计算结果",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
40, 250, 80, 25, hwnd, NULL, NULL, NULL);
ClearButton=CreateWindow("button","复位",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
160, 250, 80, 25, hwnd, NULL, NULL, NULL);
return 0;
case WM_COMMAND:
//*******显示提示及纠错*********************************************
if(((HWND)lParam==hwndEdit[0]||(HWND)lParam==hwndEdit[1]||(HWND)lParam==hwndEdit[2])
&&(HIWORD(wParam)==EN_UPDATE))
{
//清除上次结果
strcpy(strdown," ");
strcpy(strtop," ");
for(i=0;i<3;i++)
{
j=0;
GetWindowText(hwndEdit[i],strEdit,80);
while(strEdit[j]!=NULL)
{
if((strEdit[j]<'0'||strEdit[j]>'9')&&strEdit[0]!='-'&&strEdit[j]!='.')
{
MessageBox(hwnd,"只能输入0-9的数字和负号及小数点","警告",MB_OK);
strEdit[j]=NULL;
SetWindowText(hwndEdit[i],strEdit);
break;
}
j++;
}
a[i]=(float)atof(strEdit);
}
strcpy(strResultTop,"您欲求解的方程为:");
sprintf(strResultDown,"%f*x*x+%f* x+%f=0",a[0],a[1],a[2]);
InvalidateRect(hwnd,NULL,TRUE);
}
//*************复位操作**************************
if(((HWND)lParam==ClearButton)&&(HIWORD(wParam)==BN_CLICKED))
{
for(i=0;i<3;i++)
{
SetWindowText(hwndEdit[i],NULL);
a[i]=0;
}
strcpy(strdown," ");
strcpy(strtop," ");
InvalidateRect(hwnd,NULL,TRUE);
}
//**********************end
if(((HWND)lParam==hwndButton)&&(HIWORD(wParam)==BN_CLICKED))
{
for(i=0;i<3;i++)
{
GetWindowText(hwndEdit[i],strEdit,80);
a[i]=(float)atof(strEdit);
}
int n=GetRoot(a[0],a[1],a[2],root);
if(n<1)strcpy(strtop,"输入错误,方程无根!请重新输入...");
else
{
strcpy(strtop,"方程的解为:");
sprintf(strdown,"x1=%f , x2=%f",root[0],root[1]);
}
InvalidateRect(hwnd,NULL,TRUE);
}
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//___________划线框架与图片____________________
HPEN newPen;
newPen=CreatePen(PS_DOT, 1, RGB(0, 0, 0));
//oldPen=SelectObject(hdc, GetStockObject(NULL_PEN));//保存当前画笔,为何报错???
RoundRect(hdc,5,5,290,360,8,8);//画框
HBITMAP hBitmap; //位图句柄
HDC hDC;
//将图象调入内存
hBitmap= LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1));
hDC=CreateCompatibleDC(hdc);
SelectObject( hDC, hBitmap);
BitBlt(hdc, 45, 300, 300, 150, hDC, 0, 0, SRCAND);//可以透明贴图
DeleteDC(hDC);
DeleteObject(hBitmap);
//
SelectObject(hdc, newPen);
MoveToEx(hdc,15,180 , NULL);
LineTo(hdc, 280, 180);
MoveToEx(hdc,15,240 , NULL);
LineTo(hdc, 280, 240);
MoveToEx(hdc,15,280 , NULL);
LineTo(hdc, 280, 280);
DeleteObject(newPen);
//______________end___________________
TextOut(hdc,20,20,"请输入一元二次方程的系数:",25);
SetTextColor(hdc,RGB(000,000,000));
TextOut(hdc,20,51,"a",1);
TextOut(hdc,110,51,"b",1);
TextOut(hdc,200,51,"c",1);
TextOut(hdc,10,150,strResultDown,strlen(strResultDown));
TextOut(hdc,10,120,strResultTop,strlen(strResultTop));
//设置文本颜色(报错信息 )
SetTextColor(hdc,RGB(220,20,60));
TextOut(hdc,10,190,strtop,strlen(strtop));
TextOut(hdc,50,220,strdown,strlen(strdown));
//还原色彩和字体
SetTextColor(hdc,RGB(000,000,000));
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}