这里是我在对话框类中的写的代码,但是不能工作。感觉是得到的坐标不准或者其它原因?
希望大家能够给予答案。HBRUSH CampusNewsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
switch(pWnd->GetDlgCtrlID())
{
case IDC_NEWS1:
if (m_cursorRect == 1)
{
pDC->SetTextColor(RGB(255,0,0));//设为红色
}
break;
case IDC_NEWS2:
if (m_cursorRect == 2)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
break;
case IDC_NEWS3:
if (m_cursorRect == 3)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
break;
case IDC_NEWS4:
if (m_cursorRect == 4)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
break;
case IDC_NEWS5:
if (m_cursorRect == 5)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
break;
}
return hbr;
}void CampusNewsDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CRect rc1,rc2,rc3,rc4,rc5;
CString newsText;
GetDlgItem(IDC_NEWS1)->GetWindowRect(&rc1);//这里是得到屏幕坐标系的坐标
GetDlgItem(IDC_NEWS2)->GetWindowRect(&rc2);
GetDlgItem(IDC_NEWS3)->GetWindowRect(&rc3);
GetDlgItem(IDC_NEWS4)->GetWindowRect(&rc4);
GetDlgItem(IDC_NEWS5)->GetWindowRect(&rc5);
ScreenToClient(&rc1);//这里是变为这个对话框客户区的坐标
ScreenToClient(&rc2);
ScreenToClient(&rc3);
ScreenToClient(&rc4);
ScreenToClient(&rc5);
if (rc1.PtInRect(point))
{
m_cursorRect = 1;
GetDlgItem(IDC_NEWS1)->RedrawWindow();//重新绘制这个IDC_NEWS1的静态文本控件
}
else if(rc2.PtInRect(point))
{
m_cursorRect = 2;
GetDlgItem(IDC_NEWS2)->RedrawWindow();
}
else if(rc3.PtInRect(point))
{
m_cursorRect = 3;
GetDlgItem(IDC_NEWS3)->RedrawWindow();
}
else if(rc4.PtInRect(point))
{
m_cursorRect = 4;
GetDlgItem(IDC_NEWS4)->RedrawWindow();
}
else if(rc5.PtInRect(point))
{
m_cursorRect = 5;
GetDlgItem(IDC_NEWS5)->RedrawWindow();
} CDialog::OnMouseMove(nFlags, point);
}
希望大家能够给予答案。HBRUSH CampusNewsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
switch(pWnd->GetDlgCtrlID())
{
case IDC_NEWS1:
if (m_cursorRect == 1)
{
pDC->SetTextColor(RGB(255,0,0));//设为红色
}
break;
case IDC_NEWS2:
if (m_cursorRect == 2)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
break;
case IDC_NEWS3:
if (m_cursorRect == 3)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
break;
case IDC_NEWS4:
if (m_cursorRect == 4)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
break;
case IDC_NEWS5:
if (m_cursorRect == 5)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
break;
}
return hbr;
}void CampusNewsDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CRect rc1,rc2,rc3,rc4,rc5;
CString newsText;
GetDlgItem(IDC_NEWS1)->GetWindowRect(&rc1);//这里是得到屏幕坐标系的坐标
GetDlgItem(IDC_NEWS2)->GetWindowRect(&rc2);
GetDlgItem(IDC_NEWS3)->GetWindowRect(&rc3);
GetDlgItem(IDC_NEWS4)->GetWindowRect(&rc4);
GetDlgItem(IDC_NEWS5)->GetWindowRect(&rc5);
ScreenToClient(&rc1);//这里是变为这个对话框客户区的坐标
ScreenToClient(&rc2);
ScreenToClient(&rc3);
ScreenToClient(&rc4);
ScreenToClient(&rc5);
if (rc1.PtInRect(point))
{
m_cursorRect = 1;
GetDlgItem(IDC_NEWS1)->RedrawWindow();//重新绘制这个IDC_NEWS1的静态文本控件
}
else if(rc2.PtInRect(point))
{
m_cursorRect = 2;
GetDlgItem(IDC_NEWS2)->RedrawWindow();
}
else if(rc3.PtInRect(point))
{
m_cursorRect = 3;
GetDlgItem(IDC_NEWS3)->RedrawWindow();
}
else if(rc4.PtInRect(point))
{
m_cursorRect = 4;
GetDlgItem(IDC_NEWS4)->RedrawWindow();
}
else if(rc5.PtInRect(point))
{
m_cursorRect = 5;
GetDlgItem(IDC_NEWS5)->RedrawWindow();
} CDialog::OnMouseMove(nFlags, point);
}
pstatic->ModifyStyle(0x0,SS_NOTIFY);
你要在mousemove里获得static控件后,将其样式改为ss_NOTIFY,因为static控件本身不支持这个事件的
此外选上cstatic的notify属性
直接在CMyStatic::OnMouseMove里判断就是了,根本就不需要计算什么位置
然后你在设计对话框的时候选中“通知父窗口”,或者运行时加上 SS_NOTIFY 样式。
然后在 WM_MOUSEMOVE 中重新设置颜色即可
问题就出在这里,
按照你现在的做法是不应该修改这个属性的。
你的MouseMove是针对窗体的,而当你将鼠标移动到static控件上面时,此时接收事件的是static控件,而不是窗体。
如果你将NOTIFY属性去掉,就可以改变颜色了。当然,你的做法是不太好的。正确的做法是重写控件类,这时要注意,别忘了修改那个属性。明白了吗?
而我为了能够让它触发单击消息,我做了这样的处理:
将NOTIFY设置为TRUE,然后获取这个静态文本的矩形区域,对上下左右分别扩展1。
void CSerialComDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CRect RcStaticClrRx,RcStaticClrTx; GetDlgItem(IDC_STATIC_CLRREC)->GetWindowRect(&RcStaticClrRx);//这里是得到屏幕坐标系的坐标
GetDlgItem(IDC_STATIC_CLRSEND)->GetWindowRect(&RcStaticClrTx);
//RcStaticClrRx.NormalizeRect();
ScreenToClient(&RcStaticClrRx);//这里是变为这个对话框客户区的坐标
ScreenToClient(&RcStaticClrTx); if(RcStaticClrRx.left!=0) RcStaticClrRx.left -= 1;
if(RcStaticClrRx.right!=0) RcStaticClrRx.right += 1;
if(RcStaticClrRx.top!=0) RcStaticClrRx.top -= 1;
if(RcStaticClrRx.bottom!=0) RcStaticClrRx.bottom += 1;
if( RcStaticClrRx.PtInRect(point))
{
GetDlgItem(IDC_STATIC_CLRREC)->RedrawWindow(); //引发重绘
}
CDialog::OnMouseMove(nFlags, point);
}
这样就可以了。