Home > Software engineering >  MFC dragging bitmap
MFC dragging bitmap

Time:10-11

In MFC dialog application dialog window to draw an area, the onpaint () by BitBIt function will display the bitmap resources in this area, the image is larger, can only display a part of me wants to through the mouse to drag the image, in turn, can see every part, double buffer how to implement, can't see a lot of, I am novice, looks at each prawn teach, not very grateful!

CodePudding user response:

 
BOOL CDragBmpDlg: : OnEraseBkgnd (pDC) CDC *
{
//TODO: Add your message handler code here and/or call the default
Return TRUE;
//return CDialog: : OnEraseBkgnd (pDC);
}
Void CDragBmpDlg: : OnPaint ()
{
If (IsIconic ())
{
CPaintDC dc (this);//device context for painting

SendMessage (WM_ICONERASEBKGND, (WPARAM) dc. GetSafeHdc (), 0).

//Center icon in the client rectangle
Int cxIcon=GetSystemMetrics (SM_CXICON);
Int cyIcon=GetSystemMetrics (SM_CYICON);
CRect the rect.
GetClientRect (& amp; The rect);
Int x=(the rect. Width () - cxIcon + 1)/2;
Int y=(the rect. Height () - cyIcon + 1)/2;

//Draw the icon
Dc. DrawIcon (x, y, m_hIcon);
}
The else
{
If (m_hBitmap)
{
CPaintDC dc (this);//device context for painting
CRect rc;
GetClientRect (rc);
The CDC memDC;
MemDC. CreateCompatibleDC (& amp; Dc);
HBITMAP old=(HBITMAP) memDC. SelectObject (m_hBitmap);
Dc. BitBlt (0, 0, rc Width (), rc, Height (), & amp; MemDC m_MouseX, m_MouseY SRCCOPY);
MemDC. SelectObject (old);
}
//CDialog: : OnPaint ();
}
}

CDragBmpDlg: : CDragBmpDlg (pParent/*=NULL CWnd * */)
: CDialog (CDragBmpDlg: : IDD, pParent)
{
//{{AFX_DATA_INIT (CDragBmpDlg)
//NOTE: the ClassWizard will add member initialization here
//}} AFX_DATA_INIT
//Note that LoadIcon does not require a subsequent DestroyIcon in Win32
M_hIcon=AfxGetApp () - & gt; LoadIcon (IDR_MAINFRAME);
M_hBitmap=(HBITMAP) LoadImage (AfxGetInstanceHandle (),
"1. BMP," IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
M_MouseX=0;
M_MouseY=0;
}
Void CDragBmpDlg: : onm ouseMove (UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call the default
The static CPoint ptLast;
If (MK_LBUTTON==nFlags)
{
SetCapture ();
The ScreenToClient (& amp; Point);
M_MouseX=point. X-ray ptLast. X;
M_MouseY=point. Y - ptLast. Y;
If (m_MouseX & lt; M_MouseX=0, 0);
If (m_MouseY & lt; M_MouseY=0, 0);
Invalidate ().
}
The else
{
ReleaseCapture ();
The ScreenToClient (& amp; Point);
PtLast=point;
}
//CDialog: : onm ouseMove (nFlags, point);
}

CodePudding user response:

Void CDragBmpDlg: : onm ouseMove (UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call the default
The static CPoint ptLast;
CRect rc;
GetClientRect (rc);
If (MK_LBUTTON==nFlags)
{
//SetCapture ();
The ScreenToClient (& amp; Point);
M_MouseX=point. X-ray ptLast. X;
M_MouseY=point. Y - ptLast. Y;
//
If (m_MouseX & lt; M_MouseX=0, 0);
If (m_MouseY & lt; M_MouseY=0, 0);
//
If ((m_MouseX + rc. Width ()) & gt;=m_bmpWidth) m_MouseX=m_bmpWidth - rc. Width ();
If ((m_MouseY + rc. Height ()) & gt;=m_bmpHeight) m_MouseY=m_bmpHeight - rc. Height ();
//afxDump & lt; Invalidate ().
}
The else
{
//ReleaseCapture ();
The ScreenToClient (& amp; Point);
PtLast=point;
//the accumulate for next dragging. - for (point. X-ray ptLast. X);
PtLast. X=m_MouseX;
PtLast. - y=m_MouseY;
}
//CDialog: : onm ouseMove (nFlags, point);
}


Init m_hBitmap=(HBITMAP) LoadImage (AfxGetInstanceHandle (),
"1. BMP," IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
BITMAP bm.
GetObject (m_hBitmap, sizeof (BITMAP), & amp; Bm);

M_bmpWidth=bm. BmWidth;
M_bmpHeight=bm. BmHeight;

CodePudding user response:

CPaintDC dc (this);//device context for painting
CRect rc;
GetClientRect (rc);
The CDC memDC;
MemDC. CreateCompatibleDC (& amp; Dc);
HBITMAP old=(HBITMAP) memDC. SelectObject (m_hBitmap);
Dc. BitBlt (0, 0, rc Width (), rc, Height (), & amp; MemDC m_MouseX, m_MouseY SRCCOPY);
MemDC. SelectObject (old);

The point is this

CodePudding user response:

The
reference 3 floor SWWLLX response:
CPaintDC dc (this);//device context for painting
CRect rc;
GetClientRect (rc);
The CDC memDC;
MemDC. CreateCompatibleDC (& amp; Dc);
HBITMAP old=(HBITMAP) memDC. SelectObject (m_hBitmap);
Dc. BitBlt (0, 0, rc Width (), rc, Height (), & amp; MemDC m_MouseX, m_MouseY SRCCOPY);
MemDC. SelectObject (old);

The focus is on the



This just shows the picture the designated area, can't drag

CodePudding user response:

refer to the second floor schlafenhamster response:
void CDragBmpDlg: : onm ouseMove (UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call the default
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related