Hello,
I have a picture box, in which I need to draw a rectangle when user
makes a selection by dragging the mouse.
I am able to capture the mouse click event in the picture box(by
deriving from CStatic and handled the
ON_STN_CLICKED event from main Form).
But I am unable to understand how to capture mouse drag/ draw a
rectangle over picture box when the mouse moves. (New to MFC
programming, Pls help, any tips on how to handle OnLButtonUp,
OnMouseMove events for CStatic controls!) I really appreciate any
help.
Heres the code of what I have right now:
CpictureBox.h
--------------------------
----------
----------
----------
----------
----------
----------
----------
------
class CPictureBox : public CStatic
{
DECLARE_DYNAMIC(CPictureBo
x)
public:
CPictureBox();
virtual ~CPictureBox();
void SetBitmap(CString strBitmap);
protected:
afx_msg void OnPaint();
void ShowBitmap(CPaintDC *pDC);
CString m_sBitmap;
CBitmap m_bmpBitmap;
BITMAP bm;
CRect m_Rect;
DECLARE_MESSAGE_MAP()
};
void CPictureBox::OnLButtonDown
(UINT nFlags, CPoint point)
{
m_Tracking = TRUE;
SetCapture();
m_Rect.right = m_Rect.left = point.x;
m_Rect.bottom = m_Rect.top = point.y;
Invalidate();
CStatic::OnLButtonDown(nFl
ags, point);
}
--------------------------
----------
----------
----------
----------
----------
----------
----------
------
CPictureBox.cpp -- These dont get the control when events are fired!
--------------------------
----------
----------
----------
----------
----------
----------
----------
----
void CPictureBox::OnMouseMove(U
INT nFlags, CPoint point)
{
if (m_Tracking)
{
m_Rect.right = point.x;
m_Rect.bottom = point.y;
Invalidate();
}
CStatic::OnMouseMove(nFlag
s, point);
}
--------------------------
--
Main Dialog.cpp--
-----------------
BEGIN_MESSAGE_MAP(CmfcThsP
roj1Dlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BTN_BROW
SE, OnBnClickedBtnBrowse)
ON_WM_MOUSEMOVE()
ON_STN_CLICKED(IDC_STATIC_
PICBOX, OnStnClickedStaticPicbox)
END_MESSAGE_MAP()
void CmfcThsProj1Dlg::OnStnClic
kedStaticP
icbox()
{
// TODO: Add your control notification handler code here
MessageBox("hello world");
//CPictureBox::OnLButtonUp
//m_PictureBox.Invalidate(
);
}
--------------------------
----------
----------
----------
--
-Thanks