Recently made a similar Windows drawing board applet, to share with you. The function and the interface all imitate with the drawing board which the Windows own, the interface like screenshot. The main functions are: hand-painted lines, simple graphics, text input, block drag and drop, repeat undo, Zoom, open the Save picture file, in addition to directly from the database or XML access to the picture, but also provides access to the image from the Base64 interface, and other implementation and the same effect of the drawing board toolbox and color box.
1, Drawing:
The drawing function is implemented mainly in OnLButtonDown, OnMouseMove, OnLButtonUp, and is drawn according to the styles of the tools and tools currently selected. Every tool in the program is drawn through a primitive object, such as pens, brushes because they are all hand-drawn lines, so all through the cdrawpen to achieve, straight and rectangular, etc. through the cdrawshape to achieve, All of the entity objects are placed in the file DrawObject.h, see the code for the specific implementation. In addition, in order to avoid screen flicker, the program did two processing: first, the view of their own redrawing the background code, that is, the response to the view of the WM_ERASEBKGND message, directly return true, and in the OnDraw program to draw their own background on it; two, in the OnDraw, A DC that is drawn to a memory DC and then posted back to the view. This basically prevents the screen from blinking.
2. Undo Redo:
Because the whole program is simpler, there is no way to record the action, but to take a simpler approach, in the event of an entity drawing action at the end of the canvas content to be stored in BMP for revocation, these historical images stored in a BMP array, and defines a stack class to manage the array (this implementation is referenced in the online magazine 21, "Brush-like Drawing Control-Wei Lin", thank you!). , and then drawing on the screen is to put the current bitmap on the screen, and the bitmap has not yet access to the image of the entity painted on the line.
3. Zoom:
The implementation of general view scaling is achieved through two functions of DCs: SetViewportExt and SetWindowExt, but these two functions are only effective in the mm_isotropic and mm_anisotropic mapping modes. The commonly used view class with scrollbars CScrollView does not support the implementation of these two modes (see cscrollview::setscrollsizes). So had to take the MFC CScrollView code to take over to support the Mm_isotropic mapping mode, as shown below, see the program file "ADMMScrollView.h":
::SetMapMode(hdc,MM_ISOTROPIC);
int XLogMm = ::GetDeviceCaps(hdc, HORZSIZE);
int YLogMm = ::GetDeviceCaps(hdc, VERTSIZE);
int XLogPix = ::GetDeviceCaps(hdc, HORZRES);
int YLogPix = ::GetDeviceCaps(hdc, VERTRES);
::SetWindowExtEx(hdc,XLogMm*100,YLogMm*100,NULL);
::SetViewportExtEx(hdc,(int)(XLogPix*fZoomScale),(int)(YLogPix*fZoomScale),NULL);
4. Toolbox:
First of all, in order to achieve the effect of the Toolbox button in two columns, you need to set the tbbs_wrapped of the button, see the Ctoolpalettebar class in the program, and then show the tool style when you select a different tool, such as when you select "Straight line" to list the available line width styles, This feature is implemented by placing a CListCtrl on the toolbar, the current tool style is displayed in the form of an icon, and the effect is similar to that of the drawing board.
5, Pigment box:
Inherits a class from the CDialogBar class, then draws a small color rectangle within the WM_PAINT response function and rewrites its onlbuttondown, Onrbuttondown, and ONLBUTTONDBLCLK to interact with the user. The effect is good, it looks the same as the paint box of the drawing board.
6, mouse cursor style:
First add the required cursor resources to the resource file, and then call the SetCursor function in the view's OnSetCursor message function to set the cursor style, and note whether the HitTest parameter is htclient. Otherwise, the cursor or the brush's style when the mouse is moved to the scroll bar makes people feel strange.
Finally, compared with the Windows drawing board, this applet, in addition to all the functions of the storyboard, there are some advantages: convenient text input function and can set font and text color, multi-step undo Redo function, from BASE64 encoded access to pictures. Hope to be helpful to a friend in need.
This article supporting source code