简体   繁体   English

MFC-如何在矩形内绘制像素波

[英]MFC - How to draw pixel waves inside a rectangle

I am new to MFC trying to learn from the scratch. 我是MFC的新手,试图从头开始学习。

I am trying to create a small dialog based application in VS2012 - MFC. 我试图在VS2012-MFC中创建一个基于对话框的小应用程序。

On the dialog window i have drawn a rectangle (in OnPaint event) like this. 在对话框窗口中,我绘制了一个矩形(在OnPaint事件中),如下所示。

CPaintDC dc(this);
CRect background1(10,10,208,92);
dc.Rectangle(10,10,208,92);

Then i filled the bacground with some color. 然后我用一些颜色填满了免费。 Like this. 像这样。

   CBrush brush1;
   brush1.CreateSolidBrush(RGB(2,3,4));
   dc.FillRect(background1,&brush1);

Now i wanted to draw waves in form of pixels continuously inside the rectangle. 现在我想在矩形内连续绘制像素形式的波。

As of now what i have done is, 到目前为止,我所做的是

bool top = false;
    for(i=10,j=92;i<200 && j>10;)
    {
        pDC->SetPixel(i,j,NewColor);
        Sleep(10);

        if(!top)
            j-=1;
        else
            j+=1;
        if(j%4==0)
        {
            i+=1;

        }
        if(j==12)
            top=true;
        if(j==90)
            top = false;

    }

I am just drawing the pixels straightaway on the window, but within the dimensions where the rectangle lies. 我只是在窗口上直接绘制像素,但是在矩形所在的尺寸之内。 And the waves stop as it reaches the right dimension of the rect. 当波到达矩形的正确尺寸时,波停止。 But i feel thats not the right way to do it. 但是我觉得那不是正确的方法。

I want to draw the waves inside the rectangle and also continuosly, like when it reacahes the right end it should move left and it should be continuous. 我想在矩形内部连续绘制波浪,就像当它重新连接右端时,它应该向左移动并且应该是连续的。

Is there any proper way in MFC to draw something inside a rectangle (technically inside another object)? 在MFC中是否有任何适当的方法可以在矩形内(技术上在另一个对象内)绘制某些东西?

Please help me out. 请帮帮我。

I tried your code and just added some condition, so that if it reaches right side it should start moving toward left. 我尝试了您的代码,并添加了一些条件,因此,如果到达右侧,则应开始向左移动。

void CMyDlg::OnBnClickedOk()
{
    CWnd *cWnd = FromHandle(m_hWnd);
    CDC *pDC = cWnd->GetDC();
    bool top = false;
    bool right = false;
    int i,j;
    for(i=10,j=92;i<=200 && j>10;)
    {
        pDC->SetPixel(i,j,RGB(255,255,255));
        Sleep(10);

        if(!top)
            j-=1;
        else
            j+=1;
        if(j%4==0)
        {
            if(!right)
                i++;  // if reaches right side i--;
            else{
                i--;
            }
        }
        if(j==12)
            top=true;
        else if(j==90)
            top = false;
        if(i == 200)// make right = true if reaches right side
            right = true;
        else if(i == 10)// make false it reaches left side
            right = false;
    }
}

I am getting output something like this 我得到这样的输出
在此处输入图片说明

NOTE: this will cause infinite loop, you need to check condition where you have to stop printing pixels. 注意:这将导致无限循环,您需要检查必须停止打印像素的条件。

You are NOT looping with Sleep(10) in your WM_PAINT handler, are you? 您没有在WM_PAINT处理程序中与Sleep(10)循环,对吗?

MFC or not, you should separate your data processing from presentation. 是否使用MFC,您应该将数据处理与表示分离。 It appears you are trying to do an animation; 看来您正在尝试制作动画; the simplest way to do it is to have an off-screen (memory) DC and drawing your pixels on it. 最简单的方法是使用屏幕外(内存)DC,并在其上绘制像素。 At certain times (frame rate?), you would call parent's InvalidateRect()/UpdateWindow() pair. 在某些时间(帧速率?),您将调用父级的InvalidateRect()/ UpdateWindow()对。 Then in ON_PAINT, you would BitBlt() your prepared DC onto your preferred location in the dialog. 然后在ON_PAINT中,将准备好的DC BitBlt()放入对话框中的首选位置。

The ScrollWindow function can be used to scroll the existing graph to the left. ScrollWindow函数可用于将现有图形向左滚动。 Then you draw the new data to the right of the scrolled area. 然后,将新数据绘制到滚动区域的右侧。 The drawing will be much faster if you use the Polyline function instead of DrawPixel: Polyline draws an array of pixels. 如果您使用Polyline函数而不是DrawPixel,则绘制速度会更快:Polyline绘制像素数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM