简体   繁体   English

允许用户在CStatic C ++ MFC App中绘制拖动矩形

[英]Allow user to draw a drag rectangle in CStatic C++ MFC App

I have a MFC application where I have a Picture Control in the dialog. 我有一个MFC应用程序,该对话框中有一个Picture Control。 Eventually, I want to allow a user to draw a resizeable rectangle via mouse drag in the picture control over an image that I loaded. 最终,我希望允许用户通过在我加载的图像的图片控件中通过鼠标拖动来绘制可调整大小的矩形。

I defined my own picture control class as a sub class of CStatic and am working with the mouse down, mouse up, and mouse move events but I can't seem to figure out how to allow the user to draw a rectangle. 我将自己的图片控件类定义为CStatic的子类,并且正在处理鼠标按下,鼠标向上移动和鼠标移动事件,但是我似乎无法弄清楚如何允许用户绘制矩形。 Any guidance on this would be appreciated. 任何对此的指导将不胜感激。

Most of the examples I've looked at show me how to draw a rectangle in a CView:CWnd, but I'm not too familiar with MFC yet so I'm a bit lost. 我看过的大多数示例都向我展示了如何在CView:CWnd中绘制矩形,但是我对MFC不太熟悉,因此我有点迷茫。 Thanks. 谢谢。

When the user clicks the mouse button you need to record the mouse coordinates so you know where the rectangle starts. 用户单击鼠标按钮时,需要记录鼠标坐标,以便知道矩形的起始位置。 You should also set some type of flag to indicate that the user is dragging the mouse. 您还应该设置某种类型的标志,以指示用户正在拖动鼠标。 When the user moves the mouse get the current mouse position and use DrawDragRect or similar function to draw the rectangle. 当用户移动鼠标时,获取当前的鼠标位置并使用DrawDragRect或类似函数绘制矩形。 When the user releases the mouse button clear the previously mentioned "flag" and you're done with that part of the process. 当用户释放鼠标按钮时,请清除前面提到的“标志”,然后完成该过程。

You will also need to handle other events such as the control and/or parent window losing focus so that you can cancel the drag/draw operation. 您还需要处理其他事件,例如控件和/或父窗口失去焦点,以便可以取消拖动/绘制操作。 Since you did not include any code in your question it's hard to say what else you will need to do but those are the basics. 由于您的问题中未包含任何代码,因此很难说您还需要做什么,但这只是基础。

The usual technique for drawing a drag rect on top of the window contents is illustrated here: 此处说明了在窗口内容顶部绘制拖动矩形的常用技术:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd145184(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/dd145184(v=vs.85).aspx

That is Win32 API coding instead of MFC coding but the differences are minimal. 那是Win32 API编码而不是MFC编码,但是差异很小。 The basic idea is that by drawing with SetROP2(hdc, R2_NOTXORPEN); 基本思想是通过使用SetROP2(hdc,R2_NOTXORPEN)进行绘制; you invert the existing pixels, then drawing the same rect again re-inverts those pixels back to the original image. 您将现有像素反转,然后再次绘制相同的矩形,将这些像素重新反转回原始图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM