⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex_wintext.cpp

📁 郑阿齐VC教材
💻 CPP
字号:
#include <windows.h>
#include <math.h>
#include <stdio.h>

// 求一元二次方程的根,函数返回根的个数
int GetRoot(float a, float b, float c, double *root)
{
	double delta, deltasqrt;

	delta = b*b - 4.0 * a * c;
	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 str[80];

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);		// 窗口过程
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine, int nCmdShow)
{

	 // 计算方程的根
	 float a = 2.0, b = 6.0, c = 3.0;
	 double root[2];
	 int n = GetRoot(a, b, c, root);
	 if (n<1)
		strcpy(str, "方程无根!") ;
	 else 
		sprintf(str,"方程的解为:%f, %f", root[0], root[1]);

     HWND       	hwnd ;	// 窗口句柄
     MSG        	msg ;	// 消息
     WNDCLASS	wndclass ;	// 窗口类
     wndclass.style  			= CS_HREDRAW | CS_VREDRAW ;	
     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 	= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  	= NULL ;
     wndclass.lpszClassName 	= "HelloWin";		// 窗口类名
     if (!RegisterClass (&wndclass))				// 注册窗口
     {
          MessageBox (NULL, "窗口注册失败!", "HelloWin", 0) ;
          return 0 ;
     }
     // 创建窗口
     hwnd = CreateWindow ("HelloWin",                 		// 窗口类名
                          "我的窗口",					// 窗口标题
                          WS_OVERLAPPEDWINDOW,    	// 窗口样式
                          CW_USEDEFAULT,            	// 窗口最初的 x 位置
                          CW_USEDEFAULT,             	// 窗口最初的 y 位置
                          CW_USEDEFAULT,             	// 窗口最初的 x 大小
                          CW_USEDEFAULT,             	// 窗口最初的 y 大小
                          NULL,                       	// 父窗口句柄
                          NULL,                       	// 窗口菜单句柄
                          hInstance,                  	// 应用程序实例句柄
                          NULL) ;                     	// 创建窗口的参数
     ShowWindow (hwnd, nCmdShow) ;			// 显示窗口				
     UpdateWindow (hwnd) ;					// 更新窗口,包括窗口的客户区
     // 进入消息循环:当从应用程序消息队列中检取的消息是WM_QUIT时,则退出循环。
     while (GetMessage (&msg, NULL, 0, 0))
     {
		  	TranslateMessage (&msg) ;		// 转换某些键盘消息
         	DispatchMessage (&msg) ;		// 将消息发送给窗口过程,这里是WndProc
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (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);
		  					return 0 ;          
		 case WM_DESTROY:	// 当窗口关闭时产生的消息
			  				PostQuitMessage (0) ;
			  				return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ; // 执行默认的消息处理
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -