怎样在对话框上实现画两个函数图形。比如Sin()和Cos()函数图形。
Sin()和Cos()函数可以通过控件实现。图形只在一定区域有效,
还可以把这两个区域的背景色设成不一样。我是菜鸟,,,
请说的详细些。谢谢大哥大姐了!

解决方案 »

  1.   

    取个适当的步长,将N个点连接起来即可
    如果你不介意一点一点的化也可以#include <windows.h>
    #include <math.h>
    LRESULT CALLBACK WindowProc(HWND hwnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam
    );
    #define NUM 1000
    POINT   apt [NUM];#define TWOPI (2 * 3.14159)
    int WINAPI WinMain(HINSTANCE hInstance,
       HINSTANCE hPrevInstance,
       LPSTR lpCmdLine,
       int   nShowCmd
       )
    {
    WNDCLASS wc;
    wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WindowProc;
    wc.lpszClassName = TEXT("FloodFill");
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW|CS_VREDRAW; if(!RegisterClass(&wc))
    {
    MessageBox(NULL,TEXT("注册窗口类失败!"),TEXT("你好!"),MB_OK);
    return 0;
    } HWND hwnd = CreateWindow( TEXT("FloodFill"),
                          TEXT("正弦曲线"),
      WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT,
      CW_USEDEFAULT,
      CW_USEDEFAULT,
      CW_USEDEFAULT,
      NULL,
      NULL,
      hInstance,
      NULL
      );
    ShowWindow(hwnd,nShowCmd);
    UpdateWindow(hwnd); MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    } return msg.wParam;
    }LRESULT CALLBACK WindowProc(HWND hwnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam
    )
    {    PAINTSTRUCT ps;
       HDC hdc;
        static int  cxClient, cyClient ; switch(message)
    {
    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 (int 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) ; EndPaint(hwnd,&ps);
    }
    return 0;
    case WM_DESTROY:
    DestroyWindow(hwnd);
    PostQuitMessage(0);
    return 0;
        default:
    break; }
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
      

  2.   

    设置对话框背景色放在CDialog::OnCtlColor里面
      

  3.   

    不好意思哈。。是我问题没说清楚,我问的不是具体函数图形的实现,而是在对话框的两个特定区域显示,区域的背景暂时定为黑色吧。
    能不能用PICTURE控件设定这两个特定区域,,并改变这两个特定区域背景,图形就在这两个特定区域分别显示,而不在其他区域显示。。
    或者用其他方法(位图)。。
    下面是我做的一部分,其他的不知道怎么弄。
    // DrawDlgDlg.cpp : implementation file
    //#include "stdafx.h"
    #include "DrawDlg.h"
    #include "DrawDlgDlg.h"#include "math.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDrawDlgDlg dialogCDrawDlgDlg::CDrawDlgDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CDrawDlgDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CDrawDlgDlg)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CDrawDlgDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDrawDlgDlg)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CDrawDlgDlg, CDialog)
    //{{AFX_MSG_MAP(CDrawDlgDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDrawDlgDlg message handlersBOOL CDrawDlgDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CDrawDlgDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CDrawDlgDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CDrawDlgDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }void CDrawDlgDlg::OnButton1() //余弦图形
    {
    // TODO: Add your control notification handler code here for (int i=0;i<400;i++)
    {
            CClientDC dc(this);
        dc.MoveTo(300+i,100+100*cos(50*2*3.14*i));
    dc.LineTo(300+(i+1),100+100*cos(50*2*3.14*(i+1)));
    }}void CDrawDlgDlg::OnButton2() //正弦图形
    {
    // TODO: Add your control notification handler code here
    for (int i=0;i<400;i++)
    {
            CClientDC dc(this);
        dc.MoveTo(300+i,400+100*sin(50*2*3.14*i));
    dc.LineTo(300+(i+1),400+100*sin(50*2*3.14*(i+1)));
    }
    }
      

  4.   

    应该把绘图的过程放到onpaint里面去,不然刷新就没了,
    OnButton函数里面只要调用invalidate刷新窗口和传变量给onpaint,根据变量绘制相应的图形
      

  5.   

    我的确遇到过这个问题。。谢谢zoulie的提示,