All the way to MFC-mouse response

Source: Internet
Author: User
Tags transparent image
The key to mouse response is to operate two functions: onlbuttondown and onlbuttonup;

1. Use MFC Appwizard (exe) to create a single-document MFC project

2. Add necessary data members to the definition of the cxxxview class.

Class cdrawview: Public cview
{
............
PRIVATE:
Cpoint m_ptorigin; // used to record the point when the mouse is pressed
Bool m_bdraw; // The icon that is pressed by the mouse to determine whether the mouse is displayed
Cpoint m_ptold; // used to record the moving force of the mouse
};

 

Void cdrawview: onlbuttondown (uint nflags, cpoint point) // press the mouse
{
MessageBox ("haibara ai"); // a message is sent when you press it (in the form of a dialog box)
M_ptorigin = m_ptold = point; // use m_ptorigin to record the current cursor position
M_bdraw = true; // press the mouse to set the flag to true.
Cview: onlbuttondown (nflags, point); // function Self-call, loop detection. Nflags indicates the control key status

// (Including Ctrl, shift, and the state of the 5 keys left, center, and right of the mouse)
// Point indicates the coordinates of the mouse. (Coordinates relative to the current window)

}

Void cdrawview: onlbuttonup (uint nflags, cpoint point) //
{

// Draw a straight line with the mouse
HDC; // first define an HDC object: Handler to a device context (DC), pointing to a DC (device description table) Handle
HDC =: getdc (m_hwnd); // ":" Use a global function (that is, a function in the SDK)


Movetoex (HDC, m_ptorigin.x, m_ptorigin.y, null); // move from the origin () to the start point (that is, click the mouse)
Lineto (HDC, point. X, point. Y); // you can move the cursor from the current position to the coordinate point (x, y );
: Releasedc (m_hwnd, HDC); // release the DC

 

 

// Method 2

Cwnd: getdc

CDC * getdc ();

Return Value

Identifies the device context forCwndClient area if successful; otherwise, the return value isNull. The pointer may be temporary
And shoshould not be stored for later use.

CDC * PDC = getdc (); // gets the pointer of a CDC object.
PDC-> moveTo (m_ptorigin );
PDC-> lineto (point );
Releasedc (PDC );

 

// Method 3
// Cclientdc DC (this); // draw a straight line in the customer Zone
Cclientdc DC (getparent (); // draw a straight line in the frame window
DC. moveTo (m_ptorigin );
DC. lineto (point );

 

// Method 4

// Cwindowdc DC (this); // draw a straight line in the customer area
// Cwindowdc DC (getparent (); // draw a straight line in the frame window
Cwindowdc DC (getdesktopwindow (); // draw a straight line on the entire desktop panel
DC. moveTo (m_ptorigin );
DC. lineto (point );

 

// Draw a straight line with the mouse (paint brush)

Cpen pen (ps_solid, 20, RGB (255, 0, 0); // create a pen (cpen class encapsulates the operations related to the paint brush)

// (Line type, line width, line color)
Cclientdc DC (this); // create a DC
Cpen * poldpen = Dc. SelectObject (& pen); // select the pen in the device description table
DC. moveTo (m_ptorigin );
DC. lineto (point );
DC. SelectObject (poldpen );

 

// Draw a rectangle with the mouse response)
Cbrush brush (RGB (, 0); // create a paint brush, red
Cclientdc DC (this); // create a DC
DC. fillrect (crect (m_ptorigin, point), & brush); // fill a specified area with a specified paint brush,
// The first parameter is used to set the region. You can set the region by the start and end points saved during the draw.

// The running result is: the DC is filled with a rectangle area with the red paint brush we created.

 

// Use a bitmap object to fill the rectangular frame drawn with the mouse
Cbitmap bitmap; // first define a bitmap
Bitmap. loadbitmap (idb_bitmap1); // use the resource ID to load the bitmap.
Cbrush brush (& Bitmap); // with this bitmap, you can create a painter for this bitmap. The parameter is the pointer of this bitmap object.
Cclientdc DC (this); // then create a DC
DC. fillrect (crect (m_ptorigin, point), & brush); // fill a rectangle with the specified image brush (Bitmap)

 

 


//////////////////////////////////////// ////////////////////
/// Create a transparent image brush, that is, an empty image brush /////////
//////////////////////////////////////// ////////////////////
Cclientdc DC (this );
Cbrush * pbrush = cbrush: fromhandle (hbrush) getstockobject (null_brush ));
// Note that the getstockobject function returns an hgdiobj
// A handle, which needs to be converted into a paint brush handle by force conversion
// (Hbrush) getstockobject (null_brush)
// Fromhandle is a static member function of the cbrush class (see msdn), so it can be called directly by the class name.
Cbrush * poldbrush = Dc. SelectObject (pbrush); // select the painter to our device description table,

// Use an empty painter to replace our previous painter (default painter and default painter)
DC. rectangle (crect (m_ptorigin, point); // a function used in DC to draw a rectangle.
DC. SelectObject (poldbrush); // return to the default paint brush before the paint brush is replaced.
M_bdraw = false; // set m_bdraw to false

Cview: onlbuttonup (nflags, point );
}

 

 

// Move the response function with the mouse to draw a curve

Void cdrawview: onmousemove (uint nflags, cpoint point) // point is the coordinate of the current cursor location obtained by the function.

// It moves the mouse and keeps changing the value
{
Cclientdc DC (this); // create a DC pointing to the current window (customer zone)
DC. setrop2 (r2_black); // sets the painting mode.
Cpen pen (ps_solid, 1, RGB (255, 0, 0); // you can specify the paint brush (linear, line width, and line color)
Cpen * poldpen = Dc. SelectObject (& pen); // select the paint brush from the device description table.
If (m_bdraw = true)
{

///////////////////////
/// Draw a curve
DC. moveTo (m_ptorigin); // move to the start point
DC. lineto (point); // draw the line to. Note that each draw line here is very short. The curve is obtained after a short line is drawn multiple times.
M_ptorigin = point; // set the end point to the start point of the next draw line .... This keeps repeating until the mouse pops up and jumps out of the loop to end the draw line.
//////////////////////

//////////////////////
/// Draw a fan line
DC. moveTo (m_ptorigin); // The starting point remains unchanged.
DC. lineto (point );
//////////////////////

//////////////////////
// Draw a line with an edge
DC. moveTo (m_ptorigin); // The starting point remains unchanged.
DC. lineto (point); // start to draw a line
DC. lineto (m_ptold); // lead the end point of the online line to the end point of the other line
M_ptold = point; // store the end point of the previous line to connect it when the next line is crossed.
//////////////////////

}

DC. SelectObject (poldpen); // return to the default paint brush before the paint brush is replaced.

Cview: onmousemove (nflags, point); // function Self-call, cyclic detection. Nflags indicates the control key status

// (Including Ctrl, shift, and the state of the 5 keys left, center, and right of the mouse)
// Point indicates the coordinates of the mouse. (Coordinates relative to the current window)


}

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.