📜 ⬆️ ⬇️

Image Control do it yourself in Visual C ++

In Visual C ++, there is no normal standard way to display arbitrary images on a form. Of course, you can use Picture Control, but the image in it can be set only from resources (as I recall), there is no possibility of scaling, and there is no support for scrolbars if the image goes beyond the boundaries of the control. These tasks were implemented in Image Control (part of the code was borrowed from another control, but unfortunately the source was already lost, since I did it a long time ago).

The class is called CImageViewer, and it is inherited from CWnd. The image displayed in the control is stored in a member of the m_image class with the CImage type. This will allow us to display images of most popular formats. The main methods of the class:

- Image installation: void SetImage (CImage * image);
- Zoom: void ZoomIn ();
- Decrease: void ZoomOut ();
')
The rest of the methods and class members are official.

void Paint () is responsible for rendering the image and setting the Scroll Bar if necessary.

Using a class is very simple, throw a Custom control on the form, set the Class property to the CImageViewer value and add the image_ctrl variable with the CimageViewer type (remember to include the header file ImageViewer.h). Next, create an object with the CImage type, for example, img and call the CImageViewer :: SetImage method:
image_ctrl.SetImage(&img) 

Using the ZoomIn () / ZoomOut () functions you can zoom in and out.

Sometimes you may need coordinates in which you clicked the left mouse button, for this you need to override the CImageViewer class like this:
 class CMyImageViewer : public CImageViewer { public: //CMyImageViewer():CImageViewer(){} //~CMyImageViewer(){} DECLARE_MESSAGE_MAP() public: afx_msg void OnLButtonDown(UINT nFlags, CPoint point); CWnd* m_sxema; };    OnLButtonDown: void CMyImageViewer::OnLButtonDown(UINT nFlags, CPoint point) { int hp = GetScrollPos(SB_HORZ); int vp = GetScrollPos(SB_VERT); point.x = point.x + hp; point.y = point.y + vp; CWnd::OnLButtonDown(nFlags, point); } 



PS: Source code example can be downloaded here . This class still needs to be slightly modified, when scaling the image, the colors are distorted, and the size of the scrollbar sliders do not change when the image is resized.

Source: https://habr.com/ru/post/140423/


All Articles