读书人

按钮移动有关问题

发布时间: 2013-01-06 15:44:47 作者: rapoo

按钮移动问题
我现在有7个按钮,怎么判断是哪个按钮被点击呢?然后让他开始运动,其他的按钮也跟着运动呢。
CMyButton* m_button1;
CMyButton* m_button2;
CMyButton* m_button3;
CMyButton* m_button4;
CMyButton* m_button5;
CMyButton* m_button6;
在鼠标点击事件里写什么呀!
1.鼠标单击事件。
void CImageButtonDlg::OnButton1Click()
{
int button[]={ m_button1,m_button2,m_button3,m_button4,m_button5}
在鼠标点击事件里写。
switch(button)
{
case "m_button1"
SetTimer(1234,1000,NULL);
i=0;
break;
case "m_button2"
SetTimer(1234,1000,NULL);
i=1;
break;
……
}
在这里写对吗?
}
2.ontimer事件。
void CImageButtonDlg::OnTimer(UINT nIDEvent)
{

for(;i<7;i++)
{
for(int j=0,j<i;j++)
{
button[i]->SetWindowPos(NULL,points[i].x,points[i].y,110,130,SWP_NOCOPYBITS);
}
}

在这里写对吗?
}

typedef struct{

int x;
int y;
}point_t;


point_t points[]={
{43, 320},
{73, 290},
{103, 260},
{133, 230},
{163, 210},
{182, 198},
{222, 189},
{262, 180},
{302, 171},
{340, 162},
{345, 159},
{375, 157},
{405, 155},
{435, 153},
{465, 151},
{480, 149},
{515, 155},
{550, 161},
{585, 167},
{600, 170},
{620, 173},
{616, 198},
{612, 223},
{608, 248},
{604, 273},
{600, 310},
{566, 317},
{531, 324},
{500, 331},
{470, 338},
{440, 345},
{410, 352},
{390, 359},
{360, 366},
{330, 373},
{290, 375},
{255, 380},
{230, 372},
{210, 366},
{190, 360},
{170, 354},
{150, 348},
{130, 342},
{110, 336},
{90, 324},
{43, 320},
};
[解决办法]
添加单击响应函数啊
在函数里,移动按钮,获得按钮的指针或句柄,MoveWindow
[解决办法]
给你提供一段参考代码:


参考:http://blog.csdn.net/visualeleven/article/details/7177775

1)新建基于CButton的CNewButton。

2)为CNewButton添加成员变量并在构造函数中初始化。
//鼠标左键是否按下的标记
bool m_flag;
//保存当前鼠标的坐标
CPoint m_point;

CNewButton::CNewButton()
: m_flag(false)
, m_point(0)
{
}

3)给CNewButton重载PreTranslateMessage,过滤WM_LBUTTONDOWN 、WM_LBUTTONUP和WM_MOUSEMOVE消息。
BOOL CNewButton::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
switch(pMsg->message)
{
case WM_LBUTTONDOWN:
{
m_point = pMsg->pt;
m_flag = TRUE;
}
break;
case WM_LBUTTONUP:
{
m_flag = FALSE;
}


break;
case WM_MOUSEMOVE:
{
if(m_flag)
{
int cx = pMsg->pt.x - m_point.x;
int cy = pMsg->pt.y - m_point.y;

CRect rc;
GetWindowRect(&rc);
GetParent()->ScreenToClient(&rc);
int nWidth = rc.Width();
int nHeight = rc.Height();

rc.left += cx;
rc.top += cy;
rc.right = rc.left + nWidth;
rc.bottom = rc.top + nHeight;

MoveWindow(rc);

m_point = pMsg->pt;
}
}
break;
default:
break;
}
return CButton::PreTranslateMessage(pMsg);
}

读书人网 >C++

热点推荐