现在刚开始写遇到这样一个问题.我在屏幕上随机绘制方块.不管ONTIMER时间设置的多小.绘置的图都非常慢.
效率极低.这是不是说明用GDI绘图下落速度是不可以设置非常快的.直接看代码吧.贴出部分代码
int TertisBox::boxes[7][4][4] = {\
{{0,0,0,0},\
    {1,1,1,1},\
{0,0,0,0},\
{0,0,0,0}},\
//
{{0,0,0,0},\
{0,1,1,0},\
{0,1,1,0},\
{0,0,0,0}},\
//
{{0,0,0,0},\
    {1,1,0,0},\
{0,1,1,0},\
{0,0,0,0}},\
//
{{0,0,0,0},\
    {0,0,1,1},\
{0,1,1,0},\
{0,0,0,0}},\
//
{{0,0,1,0},\
    {0,1,1,1},\
{0,0,0,0},\
{0,0,0,0}},\
//
{{0,0,0,0},\
    {1,1,1,0},\
{0,0,1,0},\
{0,0,0,0}},\
//
{{0,0,0,0},\
    {0,1,1,1},\
{0,1,0,0},\
{0,0,0,0}}\
};//开始.
void CMainFrame::OnMenuStart()
{
m_wndView.SetTimer(1,200,NULL);
// TODO: 在此添加命令处理程序代码
}
void TertisBox::PaintBox(CClientDC* dc)
{
CPen pen(PS_SOLID,1,RGB(0,255,0));
CBrush brush(RGB(255,0,0));
CPen wPen(PS_SOLID,1,RGB(255,255,255));
CBrush wBrush(RGB(255,255,255)); int offset=5;
for(int j=0;j<4;j++)
for(int i=0;i<4;i++)
{
if(box[j][i]==1)
{
dc->SelectObject(&pen);
dc->SelectObject(&brush);
dc->Rectangle(i*20+5,j*20+5,(i+1)*20+5,(j+1)*20+5);
}
else
{
dc->SelectObject(&wPen);
dc->SelectObject(&wBrush);
dc->Rectangle(i*20+5,j*20+5,(i+1)*20+5,(j+1)*20+5);
}
}
}
void CChildView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
srand(time(NULL));
int n=rand() % 7;
TertisBox box( n );
box.PaintBox(&dc);
CString str;
m_n++;
str.Format(_T("第 %d 个方块"),m_n);
this->GetParent()->SetWindowTextW(str);
CWnd::OnTimer(nIDEvent);
}当执行的时候发现这样一个问题.标题栏的数字狂涨.但是窗口中的方块不紧不慢的会制.大约1秒变一次方块.
程序也不会出错.
绘制的方块和上面的数字也不同步.请问这应该怎么改进.

解决方案 »

  1.   


    //开始.
    void CMainFrame::OnMenuStart()
    {
    srand(time(NULL));
    m_wndView.SetTimer(1,200,NULL);    // TODO: 在此添加命令处理程序代码
    }void CChildView::OnTimer(UINT_PTR nIDEvent)
    {
        // TODO: 在此添加消息处理程序代码和/或调用默认值
        CClientDC dc(this);
        srand(time(NULL)); 你把这一句挪到 OnMenuStart里面去试试
        int n=rand() % 7;
        TertisBox box( n );
        box.PaintBox(&dc);
        CString str;
        m_n++;
        str.Format(_T("第 %d 个方块"),m_n);
        this->GetParent()->SetWindowTextW(str);
        CWnd::OnTimer(nIDEvent);
    }
      

  2.   

       xuexile
       楼主你好 我初学MFC 试着写扫雷俄罗斯方块之类的小游戏-ing
       主能给我学习哈你写的俄罗斯方块么 我想要"俄罗斯方块"全部的相关文件
       若可以 请发我邮箱[email protected] 不胜感激 谢谢