Download the program/Files/xqzhao/DrawDragRect.rar in this article (the instance is a Form-based program written in C #. It displays a file opening dialog box to open an image file. After you select an image file, you can select a small area of the image, which will be separately displayed on another form .)
Program description
In most image processing applications (such as Adobe's Photoshop, Google's Picasa or Coral's Paint Shop Pro), they all need to be able to select a part of the image. You may want to enlarge the area of the image or select to further process the area.
For that application, most image processing software must support this function. When we write programs under the MFC framework, they use the DrawDragRect (…) of the CDC class (...). DrawDragRect (...) API.
Although almost all MFC APIs correspond to Win32 APIs, Microsoft does not provide a DrawDragRect (...) The Win32 API of the API. Therefore, this problem occurs under the. net Framework, because we cannot find such an API.
Therefore, when we use C # To write an image processing software, this problem becomes very prominent, and there is no DrawDragRect (...) Similar API to complete the image selection function. Moreover, a WIN32 api that can be called through pInvoke cannot be found. This article uses the C # language to implement DrawDragRect (...) API, you can easily call it in your program.
Implementation Details
The entire code is embedded in the CDrawDragRect. cs file and implemented in the CDrawDragRect class. If your program needs this function, you only need to integrate your program from the previous Form to the integration relationship of CDrawDragRect.
Public partial class MainForm: CDrawDragRect
// Form
{
...
// Other implementation details skipped for brevity
}
In addition, you also need to do one thing: In the CDrawDragRect class, the void DrawDragRect (MouseEventArgs e) function processes this function, when the mouse event OnMouseMove (MouseEventArgs e) occurs, respond to the action.
The DrawDragRect (MouseEventArgs e) function first creates four Windows regions:
RgnOld
RgnNew
RgnDiff
RgnDiffOld
RgnDiff records the difference between rgnOld and rgnNew ......