VC/mfc dialog box-based image dragging (using memory DC to prevent image flashing)

Source: Internet
Author: User
Tags bmp image

When writing a dialog box-based MFC program, you need to move the image with the mouse. I checked a lot of information on the Internet and found that most of them are implemented based on cview. there are few examples of cdialog-based image dragging, and the examples are incomplete. here, I have posted the complete image dragging code on the Internet and compiled by myself over the past few days, hoping to help you.

First, I added an image control idc_static, and then dragged the image in this control. during the entire painting process, I used two memory DC, one of which is the memory DC of the BMP image, the other is the memory DC that draws the background (that is, the memory DC compatible with the image control DC ).

The principle and process of dragging and drawing an image are briefly introduced. The first is to press the mouse, move the mouse, and move the mouse up. The three events capture the mouse drag and get the new coordinates after the drag, then, call the invalidaterect () partial update function to re-paint the dragged part of the region. In this process, the system calls the onpaint message function of cdialog and places all the code drawn here, first, create a background bitmap for the idc_static control, and then select the bitmap into the memory DC that draws the background. Next, you can draw the map on this DC. then create the memory DC of the drag image and select the image into the DC. The last two steps are to drag the image memory DC bitblt to the memory DC of the drawing background, then, the memory DC that draws the background is bitblt to the DC of the idc_static control.

The Code is as follows:

. H file

Class cmydlg

{

// Add part

PRIVATE:

Bool m_bcapture;

Crgn m_rgn;

Size m_off; // The offset between the new and old positions of the image

Cpoint m_pt; // real-time image location

Size m_pic; // image size

Cbitmap m_bitmap;

};

. Cpp File

Bool cmydlg: oninitdialog ()

{

// Add part

M_pt.x = 0;

M_pt.y = 0;

M_bcapture = false;

Cwnd * pwnd = getdlgitem (idc_static );

Bitmap bm;

M_bitmap.loadbitmap (MONITOR );

M_bitmap.getobject (sizeof (BM), & BM );

M_pic.cx = BM. bmwidth;

M_pic.cy = BM. bmheight;

}

Void cmydlg: onlbuttondown (uint nflags, cpoint point)

{

Cwnd * pwnd = getdlgitem (idc_static );

Crect RC;

Pwnd-> getclientrect (& rc );

This-> clienttoscreen (& Point); // the coordinates in the dialog box are converted to the screen coordinates.

Pwnd-> screentoclient (& Point); // re-convert the control coordinates

 

Crgn RGN;

RGN. createrectrgnindirect (& rc );

If (RGN. ptinregion (point ))

{

Setcapture ();

M_bcapture = true;

M_off = point-m_pt;

Setcursor (loadcursor (null, idc_cross ));

}

Cdialog: onlbuttondown (nflags, point );

}

 

Void cmydlg: onmousemove (uint nflags, cpoint point)

{

If (m_bcapture)

{

This-> clienttoscreen (& Point); // the coordinates in the dialog box are converted to the screen coordinates.

Cwnd * pwnd = getdlgitem (idc_static );

Pwnd-> screentoclient (& Point); // re-convert the control coordinates

 

Crect oldrc (m_pt, m_pic); // coordinates in the control

Pwnd-> clienttoscreen (& oldrc); // convert to screen coordinates

This-> screentoclient (& oldrc );

// Calculate the new location

M_pt.x = point. X-m_off.cx;

M_pt.y = point. Y-m_off.cy;

This-> invalidaterect (oldrc, false );

}

Cdialog: onmousemove (nflags, point );

}

 

Void cmydlg: onlbuttonup (uint nflags, cpoint point)

{

Releasecapture ();

M_bcapture = false;

Cdialog: onlbuttonup (nflags, point );

}

Void cmydlg: onpaint ()

{

// Add part

Else

{

Cwnd * pwnd = getdlgitem (idc_static );

Crect RC;

Pwnd-> getclientrect (& rc); // obtain the size of the idc_static Control

// Create a background bitmap with the same size as the idc_static Control

Cbitmap bitmap;

Bitmap. createcompatiblebitmap (& DC, RC. Width (), RC. Height ());

// Create a memory DC for drawing the background

Cpaintdc DC (pwnd); // Control DC

CDC memdc; // memory DC that draws the background

Memdc. createcompatibledc (& DC );

Memdc. SelectObject (& Bitmap); // select the background bitmap

Memdc. fillsolidrect (255,255,255, RC. Width (), RC. Height (), RGB (); // fill the background color

Memdc. drawedge (& rc, edge_bump, bf_rect); // border around

// You can use memdc to draw the background pattern.

// Create a memory DC for the drag Image

CDC picdc;

Picdc. createcompatibledc (& memdc );

Picdc. SelectObject (& m_bitmap );

 

Memdc. bitblt (m_pt.x, m_pt.y, m_pic.cx, m_pic.cy, & picdc, 0, 0, srccopy); // bitbcopy to the DC that draws the background

DC. bitblt (0, 0, RC. Width (), RC. Height (), & memdc, 0, 0, srccopy); // Finally, bitblt is sent to the control DC.

Cdialog: onpaint ();

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.