#include <Windows.h> #include <cmath> #include<cstdio> using namespace std; int GetRoot(float a,float b,float c,double *root) { double delta,deltasqrt; delta=b*b-4*a*c; if(delta<0)return 0; deltasqrt=sqrt(delta); if(a!=0){ root[0]=(-b+deltasqrt)/2.0*a; root[1]=(-b-deltasqrt)/2.0*a; } else{ if(b!=0)root[0]=root[1]=-c/b; else return 0; } if(root[0]==root[1])return 1; else return 2; } char str[80]; //窗口过程 LRESULT CALLBACK WndPro(HWND,UINT,WPARAM,LPARAM); //WinMain主函数 int WINAPI WinMain ( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd ) { float a=2.0,b=6.0,c=3.0; double root[2]; int n=GetRoot (a,b,c,root); if(n<1)strcpy_s(str,"方程无根"); else sprintf_s (str,80,"方程的解为:%f,%f",root[0],root[1]); HWND hwnd; MSG msg; WNDCLASS wndclass; //窗口类 wndclass.style=CS_HREDRAW|CS_VREDRAW; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance=hInstance; wndclass.lpszMenuName=NULL; wndclass.lpszClassName="HelloWin"; //窗口类名 wndclass.lpfnWndProc=WndPro; if(!RegisterClass(&wndclass)) { MessageBox(NULL,"窗口注册失败","HelloWin",0); return 0; } hwnd=CreateWindow("HelloWin","MY Windows",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_NORMAL); UpdateWindow(hwnd); while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndPro(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch (message) { case WM_CREATE: return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); TextOut(hdc,10,10,str,strlen(str)); EndPaint(hwnd,&ps); break; case WM_DESTROY: PostQuitMessage(0); return 0; default: DefWindowProc(hwnd,message,wParam,lParam); break; } }