首先對按下鼠標的一點進行記錄,因此在WM_LBUTTONDOWN添加代碼:
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調用默認值
m_ptOrigin=point;
CView::OnLButtonDown(nFlags, point);
}
再次記錄鼠標彈起WM_LBUTTONUP的點的位置才可完成底下1-3的任務。
1、 畫線
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息處理程序代碼和/或調用默認值
//畫黑色的線(方法一)
/*HDC hdc; //聲明一個hdc
hdc=::GetDC(m_hWnd); //用全局函數HDC GetDC(HWND hWnd);來獲取設備句柄。由于是全局函數因此在函數前加“::”即可
MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);
LineTo(hdc,point.x,point.y);
::ReleaseDC(m_hWnd,hdc); //釋放設備hdc*/
/*方法二說明:利用CDC類 (Do all drawing through the member functions of a CDC object. The class provides member functions for device-context operations, working with drawing tools, type-safe graphics device interface (GDI) object selection, and working with colors and palettes.)
在其底下有兩個函數
MoveTo | Moves the current position. |
PolyBezier | Draws one or more Bézier splines. The current position is neither used nor updated. |
方法二與方法一相比的好處在于:方法二無須再定義窗體的句柄*/
//畫黑色的線(方法二)
/*CDC *pDC=GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);*/
/*方法三說明:利用CDC的一個派生類CWindowDC類(The CWindowDC class is derived from CDC. It calls the Windows functions GetWindowDC at construction time and ReleaseDC at destruction time. This means that a CWindowDC object accesses the entire screen area of a CWnd (both client and nonclient areas).)
其與方法二比好處就在于:在實例化GetWindowDC對象的時候就已經隱含獲取了hdc以及在對象生命周期結束的時候自動釋放它,以減輕我們的負擔。
*/
//畫黑色的線(方法三)
/*CWindowDC dc(this); //this代表當前窗體的句柄
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);*/
/*值得說明的是:在CWindowDC dc(this);中this代表當前窗體的句柄,因此我們只需要將需要繪畫的窗體指針傳入即可。在本例子中由于該消息基于View類的,因此在程序中View窗口進行繪畫有效。若要修改為MainFrame類的話,由于MainFrame類是View類的父窗口,因此我們僅僅需要獲取當前類的父窗口的指針既可,也就是把當前的this替換為GetParent()。若要在整個Windows桌面上繪畫的話可以獲取桌面的指針GetDesktopWindow()。具體代碼實現如下:*/
//畫黑色的線(方法三)(基于MainFrame類框)
/*CWindowDC dc(GetParent());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);*/
//畫黑色的線(方法三)(基于Windows桌面)
/*CWindowDC dc(GetDesktopWindow());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);*/
/*方法四說明:利用CDC的一個派生類CClientDC類 (The CClientDC class is derived from CDC and takes care of calling the Windows functions GetDC at construction time and ReleaseDC at destruction time. This means that the device context associated with a CClientDC object is the client area of a window.)
其方法與CwindowDC基本類似。
*/
//畫黑色的線(方法四)
/*CClientDC dc(this);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);*/
2、 改變顏色
/*****************************************************************************************/
//畫紅色的線
/*CPen pen(PS_SOLID,1,RGB(234,23,53)); //定義一個CPen類對象,并利用RGB宏改變其顏色
CClientDC dc(this);
CPen *pOldPen=dc.SelectObject(&pen); //將其選入設備表
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
dc.SelectObject(pOldPen); //還原先前設備表中被改動的缺省值*/
/*方法說明:通過改變畫筆CPen的屬性并把它們選入設備表,然后再進行畫線,設備表的缺省值是黑色 */
3、 畫矩形
/*****************************************************************************************/
//畫無邊框的矩形
/*方法說明:利用FillRect函數填充矩形。填充內容為畫刷的內容,即畫刷對象的指針。*/
//畫無邊框的矩形
/*CBrush brush(RGB(5,255,255));
CClientDC dc(this);
dc.FillRect(CRect(m_ptOrigin,point),&brush);*/
//以位圖作為畫刷的資源
//載入位圖到畫刷
/*CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1); /*從“項目”“添加資源”中添加位圖資源并將其ID號選到LoadBitmap()函數中。*/
CBrush brush(&bitmap);
CClientDC dc(this);
dc.FillRect(CRect(m_ptOrigin,point),&brush);*/
//畫有邊框的矩形
/*方法說明:利用Rectangle方法來畫矩形。其中CBrush與CPen類分別負責矩形內部和矩形邊框的屬性*/
//畫中心為白色的矩形
/*CClientDC dc(this);
dc.Rectangle(CRect(m_ptOrigin,point));*/
//畫中心為透明的矩形
/*CClientDC dc(this);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush=dc.SelectObject(pBrush);
dc.Rectangle(CRect(m_ptOrigin,point));
pOldBrush=pBrush;*/
//畫中心為透明邊為任意顏色的矩形
/*CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(234,23,53));
CPen *pOldPen=dc.SelectObject(&pen);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush=dc.SelectObject(pBrush);
dc.Rectangle(CRect(m_ptOrigin,point));
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);*/
4、 畫任意線條
/*要記錄每一個點的位置必須將以下代碼添加到WM_MOUSEMOVE的消息中才可記錄移動中的每一點。
但是要配合鼠標左鍵按下這個動作才可進行記錄,否則鼠標移動的事件將包括左鍵沒按下的情況。因此我們必須定義一個全局變量來記錄鼠標左鍵是否按下的信息。因此定義一個BOOL變量來記錄是或者否按下的信息。在C…VIEW.h頭文件中添加一個私有的BOOL m_bDraw;的變量來記錄。在WM_LBUTTONDOWN中將其值修改為m_bDraw=TRUE; 在WM_LBUTTONUP中修改m_bDraw=FALSE;在WM_MOUSEMOVE中添加如下代碼。*/
CClientDC dc(this);
if(m_bDraw==TRUE)
{
dc.MoveTo(m_ptOrigin);
dc.LineTo (point);
m_ptOrigin=point;
}
//畫扇形線條
/*CClientDC dc(this);
if(m_bDraw==TRUE)
{
dc.MoveTo(m_ptOrigin);
dc.LineTo (point);
}*/
//畫帶包絡線的扇形線條
/*需要記錄舊點的信息,因此在WM_LBUTTONDOWN中m_ptOld中賦值為point以后在WM_MOUSEMOVE中改變其值*/
/*CClientDC dc(this);
if(m_bDraw==TRUE)
{
dc.MoveTo(m_ptOrigin);
dc.LineTo(m_ptOld);
dc.MoveTo(m_ptOld);
dc.LineTo(point);
m_ptOld=point;
}*/
//介紹int SetROP2(int nDrawMode);方法,通過改變nDrawMode參數的值來改變繪畫參數
/*CClientDC dc(this);
if(m_bDraw==TRUE)
{
dc.SetROP2(R2_BLACK);
dc.MoveTo(m_ptOrigin);
dc.LineTo(m_ptOld);
dc.MoveTo(m_ptOld);
dc.LineTo(point);
m_ptOld=point;
}*/
/*MSDN:Sets the current drawing mode.
int SetROP2(int nDrawMode );
*/