读书人

绘图(直线曲线)事例

发布时间: 2012-09-03 09:48:39 作者: rapoo

绘图(直线,曲线)例子

画数学函数:

#include<windows.h>#include<math.h>#define NUM 1000#define TWOPI (2*3.14159)LRESULT CALLBACK WindowProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter);int WINAPI WinMain(  HINSTANCE hInstance,      // handle to current instance  HINSTANCE hPrevInstance,  // handle to previous instance  LPSTR lpCmdLine,          // command line  int nCmdShow              // show state){static TCHAR szAppName[]=TEXT("leidemingzi");HWND hwnd;MSG msg;WNDCLASS wndclass; wndclass.cbClsExtra=0;wndclass.cbWndExtra=0;wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.hCursor=LoadCursor(NULL,IDC_HELP);wndclass.hIcon=LoadIcon(NULL,IDI_EXCLAMATION);wndclass.hInstance=hInstance;wndclass.lpfnWndProc=WindowProc;wndclass.lpszClassName=szAppName;wndclass.lpszMenuName=NULL;wndclass.style=CS_HREDRAW|CS_VREDRAW;if(!RegisterClass(&wndclass)){MessageBox(NULL,TEXT("the program require windows NT"),szAppName,MB_YESNO);return 0;}hwnd=CreateWindow(  szAppName,  // registered class name  TEXT("this is title"), // window name  WS_OVERLAPPEDWINDOW,        // window style  CW_USEDEFAULT,                // horizontal position of window  CW_USEDEFAULT,                // vertical position of window  CW_USEDEFAULT,           // window width  CW_USEDEFAULT,          // window height  NULL,      // handle to parent or owner window  NULL,          // menu handle or child identifier  hInstance,  // handle to application instance  NULL       // window-creation data);ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK WindowProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter){static int cxClient,cyClient;HDC hdc;int i;PAINTSTRUCT ps;POINT apt[NUM];switch(uMsg){case WM_SIZE:cxClient=LOWORD(lParam);cyClient=HIWORD(lParam);return 0;case WM_PAINT:hdc=BeginPaint(hwnd,&ps);MoveToEx(hdc,0,cyClient/2,NULL);LineTo(hdc,cxClient,cyClient/2);for(i=0;i<NUM;++i){apt[i].x=i*cxClient/NUM;apt[i].y=(int)(cyClient/2*(1-sin(TWOPI*i/NUM)));//数学不会太差都应该可以算出来}Polyline(hdc,apt,NUM);//PolyBezier(hdc,apt,NUM);//Rectangle(hdc,100,100,500,400); //画一个矩形//RoundRect(hdc,100,100,500,400,100,75);//画一个圆角矩形倒数第二个参数=(xright-xleft)/4 倒数第一参数=(buttom-top)/4//Ellipse(hdc,100,100,500,400);//画一个椭圆//Arc(hdc,100,100,500,400,300,200,400,360);//画椭圆曲线//Chord(hdc,100,100,500,400,300,200,400,360);//画弓形//Pie(hdc,100,100,500,400,300,200,400,360);//画扇形/*Rectangle(hdc,cxClient/8,cyClient/8,7*cxClient/8,7*cyClient/8);//下面这一段代码画的是什么呢?调试下吧MoveToEx(hdc,0,0,NULL);LineTo(hdc,cxClient,cyClient);MoveToEx(hdc,0,cyClient,NULL);LineTo(hdc,cxClient,0);Ellipse(hdc,cxClient/8,cyClient/8,7*cxClient/8,7*cyClient/8);RoundRect(hdc,cxClient/4,cyClient/4,3*cxClient/4,3*cyClient/4,cxClient/4,cyClient/4);*/EndPaint(hwnd,&ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,uMsg,wParam,lParam);}

绘图(直线,曲线)事例

绘图(直线,曲线)事例

其他 什么矩形,圆矩形...自己把注释去掉,自己调试吧。

下面是一个贝赛样曲线:

#include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){     static TCHAR szAppName[] = TEXT ("Bezier") ;     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))     {          MessageBox (NULL, TEXT ("Program requires Windows NT!"),                       szAppName, MB_ICONERROR) ;          return 0 ;     }          hwnd = CreateWindow (szAppName, TEXT ("Bezier Splines"),                          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 ;}void DrawBezier (HDC hdc, POINT apt[]){     PolyBezier (hdc, apt, 4) ;          MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ;     LineTo   (hdc, apt[1].x, apt[1].y) ;          MoveToEx (hdc, apt[2].x, apt[2].y, NULL) ;     LineTo   (hdc, apt[3].x, apt[3].y) ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){     static POINT apt[4] ;     HDC          hdc ;     int          cxClient, cyClient ;     PAINTSTRUCT  ps ;          switch (message)     {     case WM_SIZE:          cxClient = LOWORD (lParam) ;          cyClient = HIWORD (lParam) ;                    apt[0].x = cxClient / 4 ;          apt[0].y = cyClient / 2 ;                    apt[1].x = cxClient / 2 ;          apt[1].y = cyClient / 4 ;                    apt[2].x =     cxClient / 2 ;          apt[2].y = 3 * cyClient / 4 ;                    apt[3].x = 3 * cxClient / 4 ;          apt[3].y =     cyClient / 2 ;                    return 0 ;     case WM_LBUTTONDOWN:     case WM_RBUTTONDOWN:     case WM_MOUSEMOVE:          if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)          {               hdc = GetDC (hwnd) ;                              SelectObject (hdc, GetStockObject (WHITE_PEN)) ;               DrawBezier (hdc, apt) ;                              if (wParam & MK_LBUTTON)               {                    apt[1].x = LOWORD (lParam) ;                    apt[1].y = HIWORD (lParam) ;               }                              if (wParam & MK_RBUTTON)               {                    apt[2].x = LOWORD (lParam) ;                    apt[2].y = HIWORD (lParam) ;               }                              SelectObject (hdc, GetStockObject (BLACK_PEN)) ;               DrawBezier (hdc, apt) ;               ReleaseDC (hwnd, hdc) ;          }          return 0 ;               case WM_PAINT:          InvalidateRect (hwnd, NULL, TRUE) ;                    hdc = BeginPaint (hwnd, &ps) ;                    DrawBezier (hdc, apt) ;                    EndPaint (hwnd, &ps) ;          return 0 ;               case WM_DESTROY:          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}

结果如下(鼠标左键右键可以控制弧度)

绘图(直线,曲线)事例

读书人网 >编程

热点推荐