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

📄 picruredemo.cpp

📁 c++从入门到精通
💻 CPP
字号:
#include <windows.h>
#include <math.h>


#define NUM  10000
#define Pi   3.1415926

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("正弦曲线") ;
     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 = szAppName ;
          
     if (!RegisterClass (&wndclass))
     {
          MessageBeep(0) ;
		  return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, 
						  TEXT ("利用Polyline函数绘制正弦曲线"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
						  CW_USEDEFAULT,
                          CW_USEDEFAULT,
						  CW_USEDEFAULT,
                          NULL,
						  NULL,
						  hInstance,
						  NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     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)
{
     static int  cxClient, cyClient ;
     HDC         hdc ;
     int         i ;
     static int  num=10000;
	 static int  flags=0;
	 BOOL b=TRUE;
	 PAINTSTRUCT ps ;
     POINT       apt [NUM] ;//POINT结构数组
    
	HPEN hP1,hP2; 
	COLORREF crColor=RGB(255,0,0);
	hP1=CreatePen     //创建新画笔
		(   
	     PS_SOLID,    //确定画笔的样式
		 0, 		  //画笔的宽度,取0表示一个象素宽
		 crColor      //画笔的颜色为红色
	    );
	hP2=CreatePen     //创建新画笔
		(  
		 PS_SOLID,    //确定画笔的样式
	 	 3, 		  //画笔的宽度,取3表示3个象素宽
		 RGB(0,0,255) //画笔的颜色为蓝色
		);
    //消息循环 
	switch (message)
     {
     case WM_SIZE:
          cxClient = LOWORD (lParam) ;//客户区的宽度
          cyClient = HIWORD (lParam) ;//客户区的高度
		  return 0 ;
          
     case WM_PAINT:
           hdc = BeginPaint (hwnd, &ps) ;
		  //画直线,x轴         
		  MoveToEx (hdc, 0,cyClient / 2, NULL) ;
          LineTo   (hdc, cxClient, cyClient / 2) ;
		  //选择画笔
		  if(flags==3||flags==2)
		     SelectObject(hdc,hP1);
		  else if(flags==1||flags==4)
			 SelectObject(hdc,hP2);
         // TextOut(hdc,cxClient-100,100,Text("num="+
          for (i = 0 ; i < num ; i++)
          {
               apt[i].x = i * cxClient / num ;
               apt[i].y = (int) (cyClient / 2 * (1 - sin (2*Pi * i / num))) ;
          }
          //依次用线段连接点apt
		  Polyline (hdc, apt, num) ;
		  
          return 0 ;
	 case WM_LBUTTONDOWN:
		 if(flags==0)
			  flags=1;
		 if(flags==1)
		 {
			 num=10;
			 flags++;
		 
		 }
		 else if(flags==2)
		 {
			 num=100;
			 flags++;
		 }
		 else if(flags==3)
		 {
			 num=1000;
			 flags++;
		 }
		 else if(flags==4)
		 {
			 num=10000;
			 flags=1;
		 }
		 InvalidateRect(hwnd,NULL,TRUE);
		 return 0;
          
     case WM_DESTROY:
		  //删除画笔
		  DeleteObject(hP1);
		  DeleteObject(hP2);
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

⌨️ 快捷键说明

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