如何使用SetTimer,KillTimer函数 [英] How to use SetTimer, KillTimer functions

查看:192
本文介绍了如何使用SetTimer,KillTimer函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual C ++,我在对话框中插入了三个按钮.当我仅单击一个按钮时,按钮会移动并更改其位置.为此,我使用了Movewindow功能,但问题是按钮在移动时我看不到他们的动作.我不知道如何以及在哪里使用settimer和killtimer,所以我可以查看他们的动作.这里是代码,我不知道如何使用代码标签,所以我将其粘贴在这里:


I am working with visual c++ and i have inserted three buttons on dialogue.When i click only one of the button the buttons move and change their position.For this purpose i have used Movewindow function but problem is that while buttons are moving i cannot see their movement.I donot know how and where to use settimer and killtimer so i can view their movement.Here is the code,i donot konw how to use code tag so i am pasting it here:


void CNadiaDlg::OnButton1() 
{
Move();     //on button1 click call move function
   }


void CNadiaDlg::Move() 
{	   
int x2;
srand(time(NULL));
x2=1+rand()%4;
if(x2==1)
{
m_butn1.MoveWindow(30,280,130,60,true);
Sleep(200);
m_butn2.MoveWindow(170,280,130,60,true);
Sleep(200);
m_butn3.MoveWindow(170,10,130,60,true);
Sleep(200);
m_butn2.MoveWindow(30,115,130,60,true);
Sleep(200);
m_butn1.MoveWindow(320,115,130,60,true);
Sleep(200);
m_butn3.MoveWindow(170,115,130,60,true);
}
else if(x2==2)
{
/////////////////////////////////////////////////////////////////////////////////
m_butn1.MoveWindow(30,280,130,60,true);
Sleep(200);
m_butn2.MoveWindow(170,280,130,60,true);
Sleep(200);
m_butn3.MoveWindow(170,10,130,60,true);
Sleep(200);
m_butn3.MoveWindow(30,115,130,60,true);
Sleep(200);
m_butn2.MoveWindow(320,115,130,60,true);
Sleep(200);
m_butn1.MoveWindow(170,115,130,60,true);
}
else if(x2==3)
{
/////////////////////////////////////////////////////////////////////////////////
m_butn1.MoveWindow(30,280,130,60,true);
Sleep(200);
m_butn2.MoveWindow(170,280,130,60,true);
Sleep(200);
m_butn3.MoveWindow(170,10,130,60,true);
Sleep(200);
m_butn1.MoveWindow(30,115,130,60,true);
Sleep(200);
m_butn2.MoveWindow(320,115,130,60,true);
Sleep(200);
m_butn3.MoveWindow(170,115,130,60,true);
}


按钮在随机移动,但是我看不到它们的移动.


Buttons are moving randomly but i cannot see their movement.

推荐答案

SetTimerKillTimer(以及OnTimer)方法在文档中都有很好的解释,请查看:" CWnd :: SetTimer ".

基本上,您必须:
  • 为按钮位置的{x,y}坐标随机选择较小的增量(例如{dx,dy}).
  • 设置一个合理的"计时器(例如100 ms)
  • 内部OnTimer消息处理程序按照较小的增量移动按钮(即移至新位置" {x=x+dx, y=y+dy}).
  • 经过一定的迭代次数(例如25)将增量{dx,dy}替换为随机选择的新增量.
SetTimer, KillTimer (together with OnTimer) methods are well explained in the documentation, have a look at: "CWnd::SetTimer".

Basically you have to:
  • Choose randomly small increments (say {dx,dy}) for the {x,y} coordinates of buttons position.
  • Set a ''reasonable'' timer (say 100 ms)
  • Inside OnTimer message handler move the button according to the small increments (i.e. to the ''new position'' {x=x+dx, y=y+dy}).
  • after a certain number of iterations (say 25) replace the increments {dx,dy} with random choosen new ones.


文档 [ ^ ]


请参阅我对您对此问题的补充问题的回答.
See my answer to your supplementary question on this subject.


这篇关于如何使用SetTimer,KillTimer函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆