Chinaunix首页 | 论坛 | 博客
  • 博客访问: 514853
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-02-07 13:11:36

请参考《windows程序设计》- 图形基础 - BEZIER.C.
看起来非常简单不是么?
头文件:

#ifndef BEZIER_H
#define BEZIER_H

class CMyApp : public CWinApp {
    virtual BOOL InitInstance();
};

class CMainWindow : public CFrameWnd {
public:
    CMainWindow();
protected:
    int cxClient, cyClient;
    POINT ppt[4];
    void DrawBezier();
    afx_msg void OnSize(UINT, int, int);
    afx_msg void OnPaint();
    afx_msg void OnMouseMove(UINT, CPoint);
    // afx_msg void OnLButtonDown(UINT, CPoint);

    DECLARE_MESSAGE_MAP()
};
#endif

实现:

#include <afxwin.h>
#include "bezier.h"

CMyApp myApp;

BOOL CMyApp::InitInstance() {
    m_pMainWnd = new CMainWindow;
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
    ON_WM_SIZE()
    ON_WM_PAINT()
    ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

CMainWindow::CMainWindow() {
    Create(NULL, _T("贝塞尔曲线"));
}

void CMainWindow::OnSize(UINT uType,
                         int cx,
                         int cy) {
                             cxClient = cx;
                             cyClient = cy;
                             ppt[0].x = cxClient / 4;
                             ppt[0].y = cyClient / 2;
                             ppt[1].x = cxClient / 2;
                             ppt[1].y = cyClient / 4;
                             ppt[2].x = cxClient / 2;
                             ppt[2].y = 3 * cyClient / 4;
                             ppt[3].x = 3 * cxClient / 4;
                             ppt[3].y = cyClient / 2;
}

void CMainWindow::DrawBezier() {
    CClientDC dc(this);
    dc.SetROP2(R2_NOT);
    dc.PolyBezier(ppt, 4);
    dc.MoveTo(ppt[0]);
    dc.LineTo(ppt[1]);
    dc.MoveTo(ppt[2]);
    dc.LineTo(ppt[3]);
}


void CMainWindow::OnMouseMove(UINT nFlags, CPoint point) {
    if(nFlags & MK_LBUTTON) {
        DrawBezier();
        ppt[1].x = point.x;
        ppt[1].y = point.y;
        DrawBezier();
    }
    if(nFlags & MK_RBUTTON) {
        DrawBezier();
        ppt[2].x = point.x;
        ppt[2].y = point.y;
        DrawBezier();
    }
}

void CMainWindow::OnPaint() {
    CPaintDC dc(this);
    DrawBezier();
}


阅读(4000) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~