1、創建名為Mouse的SDI項目
初始化一個二維數組m_nGrid[x][y]
=0(用于在視圖中顯示藍色或紅色方框)cursors[] (構造鼠標形狀信息的數組) m_hCursor[15](把預定義的光標加載到一個數組中,當用戶在工作區中移動鼠標時使用該數組)。
struct
{
char* id;
char szName[255];
} cursors[]={
IDC_APPSTARTING,"IDC_APPSTARTING",//箭頭和小沙漏的組合
IDC_ARROW,"IDC_ARROW", //標準箭頭
IDC_CROSS,"IDC_CROSS", //當處于文本文檔或編輯控件中時通常會見到的十字形光標
//IDC_HAND,"IDC_HAND",手型光標
IDC_HELP,"IDC_HELP", //用于表示上下文敏感幫助的箭頭和問號圖標
IDC_IBEAM,"IDC_IBEAM", //與IDC_CROSS相同
IDC_ICON,"IDC_ICON", //用于4.0或更早的版本,已不用
IDC_NO,"IDC_NO", //帶杠的圓,用于表示拖放操作中不能作為放置對象的目標
IDC_SIZE,"IDC_SIZE", //用于4.0或更早的版本,已不用
IDC_SIZEALL,"IDC_SIZEALL", //四向箭頭,分別指向東、南,西、北,用于表名對象可按箭頭所指方向改變大小
IDC_SIZENESW,"IDC_SIZENESW", //雙向箭頭,分別指向東北、西南,用于表名對象可按箭頭所指方向改變大小
IDC_SIZENS,"IDC_SIZENS", //雙向箭頭,分別指向北、南,用于表名對象可按箭頭所指方向改變大小
IDC_SIZENWSE,"IDC_SIZENWSE", //雙向箭頭,分別指向西北、東南,用于表名對象可按箭頭所指方向改變大小
IDC_UPARROW,"IDC_UPARROW", //垂直箭頭
IDC_WAIT,"IDC_WAIT" //沙漏,表示耗時很長的操作正在進行
};
CMouseView::CMouseView()
{
// TODO: add construction code here
for(int y=0;y<10;y++)
for(int x=0;x<10;x++)
m_nGrid[x][y]=0;
for(int i=0;i<15;i++)
m_hCursor[i]=::LoadCursor(NULL,cursors[i].id);
}
2、工作區的圖像處理
把工作區分成100塊,根據鼠標事件改變m_nGrid[x][y]的值使相應的區域的顏色發生改變
void CMouseView::OnDraw(CDC* pDC)
{
CMouseDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
RECT Rect;
GetClientRect(&Rect);
CBrush RedBrush(RGB(255,0,0));
CBrush BlueBrush(RGB(0,0,255));
CBrush WhiteBrush(RGB(255,255,255));
CBrush * pUseBrush;
for(int y=0;y<10;y++)
for(int x=0;x<10;x++)
{
RECT DrawRect;
DrawRect.left=(x*Rect.right)/10;
DrawRect.top=(y*Rect.bottom)/10;
DrawRect.right=DrawRect.left+(Rect.right/10);
DrawRect.bottom=DrawRect.top+(Rect.bottom/10);
pUseBrush=&WhiteBrush;
if(m_nGrid[x][y]==1)
pUseBrush=&BlueBrush;
else if(m_nGrid[x][y]==2)
pUseBrush=&RedBrush;
pDC->FillRect(&DrawRect,pUseBrush);
}
}
3、兩個輔助函數
ShowMouseInfo的功能是改變m_nGrid的值改變圖像還有就是把點擊的位置和數遍的信息顯示出來;
GetCursorRegion的功能是獲得數組m_hCursor的位置
void CMouseView::ShowMouseInfo( const char * lpszText, CPoint point, int nFlag /* = -1 */ )
{
//if(m_nInfoMode==MOUSE_SHOWGRID)
int x,y;
if(nFlag!=-1)
{
RECT Rect;
GetClientRect(&Rect);
x=(point.x*10)/Rect.right;
y=(point.y*10)/Rect.bottom;
if(m_nGrid[x][y]==nFlag)
m_nGrid[x][y]=0;
else
m_nGrid[x][y]=nFlag;
InvalidateRect(NULL,FALSE);
//該函數向指定的窗體添加一個矩形,然后窗口客戶區域的這一部分將被重新繪制
UpdateWindow();
//通過發送重繪消息 WM_PAINT 給目標窗體來更新目標窗體客戶區的無效區域
}
CClientDC ClientDC(this);
CString strInfo;
strInfo.Format("X:%d Y:%d %s ",point.x,point.y,lpszText);
ClientDC.TextOut(point.x,point.y,strInfo,strInfo.GetLength());
}
int CMouseView::GetCursorRegion(POINT *lpPt)
{
RECT Rect;
GetClientRect(&Rect);
int x=(lpPt->x*4)/Rect.right;
if(x>3)
x=3;
int y=(lpPt->y*4)/Rect.bottom;
if(y>3)
y=3;
return (y*4+x);
}
4、鼠標移動、左鍵按下和光標的改變事件
void CMouseView::OnMouseMove(UINT nFlags, CPoint point)
{
CClientDC ClientDC(this);
CPoint pt;
pt=point;
ClientToScreen(&pt);
CString strInfo;
strInfo.Format( "X:%d Y:%d ScnX:%d ScnY:%d ",point.x, point.y,pt.x, pt.y );
ClientDC.TextOut(10,10,strInfo,strInfo.GetLength());
int nCursor=GetCursorRegion(&point);
CString strInfo2;
strInfo2.Format("Cursor:%s ,%d ",cursors[nCursor].szName,nCursor);
ClientDC.TextOut(10,40,strInfo2,strInfo2.GetLength());
CView::OnMouseMove(nFlags, point);
}
void CMouseView::OnLButtonDown(UINT nFlags, CPoint point)
{
ShowMouseInfo("LButtonDown",point,1);
CView::OnLButtonDown(nFlags, point);
}
BOOL CMouseView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(&pt);
int nCursor = GetCursorRegion( &pt );
::SetCursor(m_hCursor[nCursor]);
return( TRUE );
//return CView::OnSetCursor(pWnd, nHitTest, message);
}
posted on 2009-07-03 11:21
The_Moment 閱讀(455)
評論(0) 編輯 收藏 引用 所屬分類:
VC實踐