In the VC ++ SDK, the movement of straight lines (rectangles, ovans, etc.) and the moving of straight lines attached to rectangular ovans (Appendix: Running Effect Diagram) are implemented)

Source: Internet
Author: User

In the SDK, there are ready-made functions for us to call to draw a straight line and a rectangle. Sometimes, after the rectangle is drawn, we need to be able to move with the mouse to adjust the position of the image.

I don't know how to describe it. I just paste the code and have added detailed comments to the code.

Paste the complete example code directly, which saves the most trouble.

// Areatest. cpp: defines the entry point of the application. // # Include "stdafx. H "# include" areatest. H "# include <math. h> # define max_loadstring 100 // global variable: hinstance hinst; // the current instance tchar sztitle [max_loadstring]; // Title Bar text tchar szwindowclass [max_loadstring]; // main window class name // The Forward Declaration of the function contained in this Code module: atommyregisterclass (hinstance); boolinitinstance (hinstance, INT); lresult callbackwndproc (hwnd, uint, wparam, lparam); int_ptr callbackabout (hwnd, uint, wparam, lparam); int API Entry _ twinmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow) {unreferenced_parameter (hprevinstance); unreferenced_parameter (lpcmdline); // todo: Place code here. MSG; haccel hacceltable; // initialize the global string loadstring (hinstance, ids_app_title, sztitle, max_loadstring); loadstring (hinstance, idc_areatest, szwindowclass, max_loadstring); myregisterclass (hinstance ); // execute application initialization: If (! Initinstance (hinstance, ncmdshow) {return false;} hacceltable = loadaccelerators (hinstance, makeintresource (idc_areatest); // main message loop: While (getmessage (& MSG, null, 0, 0) {If (! Translateaccelerator (MSG. hwnd, hacceltable, & MSG) {translatemessage (& MSG); dispatchmessage (& MSG) ;}} return (INT) MSG. wparam;} // function: myregisterclass () /// purpose: register the window class. //// Note: //// only when you want this code to be compatible with the Win32 system before the "registerclassex" function added to Windows 95, this function and its usage are required. It is very important to call this function, // so that the application can obtain the associated // "correct format" small icon. // Atom myregisterclass (hinstance) {wndclassex wcex; wcex. cbsize = sizeof (wndclassex); wcex. style = cs_hredraw | cs_vredraw; wcex. lpfnwndproc = wndproc; wcex. cbclsextra = 0; wcex. cbwndextra = 0; wcex. hinstance = hinstance; wcex. hicon = loadicon (hinstance, makeintresource (idi_areatest); wcex. hcursor = loadcursor (null, idc_arrow); wcex. hbrbackground = (hbrush) (color_window + 1); wcex. lpszmenuname = makeintresour Ce (idc_areatest); wcex. lpszclassname = szwindowclass; wcex. hiconsm = loadicon (wcex. hinstance, makeintresource (idi_small); Return registerclassex (& wcex) ;}/// function: initinstance (hinstance, INT) /// purpose: save the instance handle and create the main window // Note: // in this function, we save the instance handle in the global variable and // create and display the main program window. // Bool initinstance (hinstance, int ncmdshow) {hwnd; hinst = hinstance; // store the instance handle in the global variable hwnd = createwindow (szwindowclass, sztitle, ws_overlappedwindow, cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null); If (! Hwnd) {return false;} showwindow (hwnd, ncmdshow); updatewindow (hwnd); Return true ;}/// function: wndproc (hwnd, uint, wparam, lparam) //// purpose: to process messages in the main window. /// Wm_command-process application menu // wm_paint-draw the main window // wm_destroy-Send the Exit message and return /// lresult callback wndproc (hwnd, uint message, wparam, lparam) {// define 2 points for draw line static point start = {200,200}, end = {500,500}; // Save the static point ptbuttondown pressed by the mouse; // Save the point of the mouse movement static point ptmousemove; // determine whether to draw static bool draw; int wmid, wmevent; paintstruct pS; HDC, hdcmem; hbitmap; static hrgn; rect clientrect; SW Itch (Message) {Case wm_command: wmid = loword (wparam); wmevent = hiword (wparam); // select switch (wmid) {Case idm_about: dialogbox (hinst, makeintresource (idd_aboutbox), hwnd, about); break; Case idm_exit: destroywindow (hwnd); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} break; case wm_lbuttondown: {/////////////////////////////////////// //////////// each time you press the mouse, you need to destroy the original sensing region and recreate it. // create a new sensing region // /// // Deleteobject (hrgn ); hrgn = NULL; point pointlist [4]; //////////////////////////////////////// ///////////// simple description here, since a straight line is composed of the first two points, you must create an sensing area for it. For convenience, create a // rectangular sensing area. The Theorem 3 and 4 are used, and 5 is used to create a rectangle with a width of 2*5, the cropping area of a rectangle with a straight line length. When the mouse is pressed in this area, drag the Mouse shape and perform the following operations ///////////////////////////// //// // pointlist [0]. X = (INT) (start. x-3); pointlist [0]. y = (INT) (start. Y + 4); PO Intlist [1]. X = (INT) (start. X + 3); pointlist [1]. y = (INT) (start. y-4); pointlist [2]. X = (INT) (end. x-3); pointlist [2]. y = (INT) (end. Y + 4); pointlist [3]. X = (INT) (end. X + 3); pointlist [3]. y = (INT) (end. y-4); // create a line at the new position of the sensing region hrgn = createpolygonrgn (pointlist, _ countof (pointlist), winding); ptbuttondown. X = loword (lparam); ptbuttondown. y = hiword (lparam); If (ptinregion (hrgn, ptbuttondown. x, ptbuttondown. y) {outputdebugstrin G (_ T ("inregion"); // sets the Mouse capture so that the mouse cannot escape from the palm of my window to control setcapture (hwnd ); // set the cursor to setcursor (loadcursor (null, idc_sizeall); Draw = true ;}} break; Case wm_mousemove: HDC = getdc (hwnd ); if (draw = false) break; outputdebugstring (_ T ("move \ n"); setcursor (loadcursor (null, idc_sizeall )); // get the coordinates of the mouse movement position at the moment ptmousemove. X = loword (lparam); ptmousemove. y = hiword (lparam); // calculate the start and end points of the new line, plus the offset start. X = start. X + ptmousemove. x-ptButtondown.x; start. y = s Tart. Y + ptmousemove. y-ptButtondown.y; end. X = end. X + ptmousemove. x-ptButtondown.x; end. y = end. Y + ptmousemove. y-ptButtondown.y; // draw lines movetoex (HDC, start. x, start. y, null); lineto (HDC, end. x, end. y); // assign the coordinates of the current mouse position to ptbuttondown. The next offset calculation is accurate to ptbuttondown. X = ptmousemove. x; ptbuttondown. y = ptmousemove. y; // draw a straight line invalidaterect (hwnd, null, true); releasedc (hwnd, HDC); break; Case wm_lbuttonup: // release window for Mouse capture releasecapture (); // Setcursor (loadcursor (null, idc_arrow); // set the draw line mark draw = false; break; Case wm_paint: HDC = beginpaint (hwnd, & PS ); // todo: add any drawing code here... ////////////// // if you do not want to discard the flickering state, you can use the double buffer getclientrect (hwnd, & clientrect); hdcmem = createcompatibledc (HDC); hbitmap = createcompatiblebitmap (HDC, clientrect. right, clientrect. bottom); SelectObject (hdcmem, hbitmap); fillrect (hdcmem, & clientrect, null); movetoex (hdcmem, start. x, Start. y, null); lineto (hdcmem, end. x, end. y); bitblt (HDC, 0, 0, clientrect. right, clientrect. bottom, hdcmem, 0, 0, srccopy); deleteobject (hbitmap); deletedc (hdcmem ); //////////////////////////////////////// //// // endpaint (hwnd, & PS); break; //////////////////////////////////////// //// // the entire background is re-painted, therefore, you no longer need to erase the background. The default background is erased using // defwindowproc (XXX, wm_erasebkgnd, x, x), so here we Take the initiative to inform /// I have handled the complaint system. //////////////////////////////////////// //// Case wm_erasebkgnd: return true; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;} // message processing program in the "about" box. Int_ptr callback about (hwnd hdlg, uint message, wparam, lparam) {callback (lparam); Switch (Message) {Case wm_initdialog: Return (int_ptr) True; Case wm_command: if (loword (wparam) = idok | loword (wparam) = idcancel) {enddialog (hdlg, loword (wparam); Return (int_ptr) True;} break ;} return (int_ptr) false ;}

As for the rectangle, the moving of the elliptic is not much to be said.

Let's give a simple running example.

Singles' Day is coming .........

Good luck !!!!

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.