MFC selection of a user drawn rectangle on picture control - mfc

I'm trying to create an interface that allows the user to draw a rectangle over a picture control box. I have a picture control class and used CRectTracker to allow the user to draw a rectangle. I want the user to also be able to select a previously drawn rectangle but I don't know how to handle the selection of a drawn rectangle.
I want to be able to select the rectangle and also add resize handlers on it.
Here is my code for drawing the rect.
void PictureCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// If mouse click is outside of rectangle
if(m_drawRect.m_tracker.HitTest(point) < 0 ) {
if(m_drawRect.m_tracker.TrackRubberBand(this, point, TRUE)) {
CDC* pDC = GetDC();
m_drawRect.m_tracker.m_nStyle &= CRectTracker::resizeInside;
// Paint transparent rectangle
pDC->SelectStockObject(NULL_BRUSH);
pDC->Rectangle(m_drawRect.m_tracker.m_rect);
ReleaseDC(pDC);
}
}
CStatic::OnLButtonDown(nFlags, point);
}
Any help would be appreciated. Thank you.

You will need to store the coordinates of the rectangle in your class (also save/load) and perform a HitTest during mouse-down.
To implement the resize handles, you will need a boolean to denote that the rectangle is selected (set boolean to FALSE if the click is not on the rectangle) and draw grab handles during paint if the boolean is TRUE; if the mouse moves over the grab handles, change the mouse-cursor, perform the resize during mouse-down and mouse-up in this case.
It's all quite complicated and gets more so if you have more than just one rectangle!
Here is a DrawCLI MSDN example which does all of that with rectangles, rounded rectangles, ellipses, lines and polylines plus support for OLE -- maybe this will help, it's probably easier to delete classes/functions from DrawCLI before it's in a state to merge with your application...

Related

How to clip the corner rectangle created by two scrollbar controls

Let's say you have a resizable window with child scrollbar controls, and the scrollbars come and go depending on whether the window contents are large enough to require scrolling.
When both scrollbars are present, a small rectangle is effectively created in the bottom right corner of the window, at their intersection. Is there a clean strategy for clipping that rectangle when drawing on the window, so that you don't paint on it?
I guess my current approach is to obtain the rectangles for each scrollbar, and if those rectangles are not null, then use the rectangles' locations to determine the rectangle that we want to clip. And then call ExcludeClipRect for that rectangle. I guess a similar approach could be used, except with GetSystemMetrics(SM_CXVSCROLL) and GetSystemMetrics(SM_CYVSCROLL) to get the rectangle dimensions.
But is there a more accepted way of doing this, perhaps using some helpful clipping API functions? Thank you for any input.

MFC getting mouse pointer coordinate problem

I'm trying to get coordinate of the Rect (Picture control) but It's a bit glitchy.
So here're the process that I've done.
1st. made a picture control
2nd. I've earned WindowRect through GetWindowRect
// myDialogDlg.cpp
CRect m_rcDisp // (is acually in myDialogDlg.h)
BOOL myDialogDlg::OnInitDialog()
{
// IDC_PIC1 == ID of the (static) picture control
GetDlgItem(IDC_PIC1)->GetWindowRect(m_rcDisp);
...
}
3rd. I've made OnMouseMove event, and used PtInRect to make some action while mouse pointer is inside the picture control.
// myDialogDlg.cpp
void myDialogDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CString debug;
{
if (m_rcDisp.PtInRect(point))
{
// my event starts
OutputDebugString(_T("here"));
if (m_CamTrig == CAMERA_TRIG_SW)
{
m_CurSor.x = point.x;
m_CurSor.y = point.y;
InvalidateRect(m_rcDisp, NULL);
}
// my event ends
}
}
CDialogEx::OnMouseMove(nFlags, point);
}
and... where actually PtInRect works is about here inside the red box
:( Hope I get the best answer.
thx!
GetWindowRect returns the window dimensions in screen coordinates. WM_MOUSEMOVE reports the mouse position in client coordinates. ScreenToClient1 can be used to translate from screen coordinates to client coordinates, making the picture control's window rectangle coordinates and hit testing function agree on a common origin.
This needs to be done whenever the picture control is moved relative to its parent dialog. If the picture control is never moved you only need to adjust the rectangle once.
This answer links to the Windows API documentation, since it's generally more informative and better maintained than the respective MFC entries. You can call either one from MFC code, though it's usually more convenient to just call into the CWnd members.
1 Use MapWindowPoints instead if you plan on supporting RTL and LTR layouts. See Window Layout and Mirroring for guidance.

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. 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. 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. Thanks.
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
That is Win32 API coding instead of MFC coding but the differences are minimal. The basic idea is that by drawing with SetROP2(hdc, R2_NOTXORPEN); you invert the existing pixels, then drawing the same rect again re-inverts those pixels back to the original image.
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. 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.

Drag a rectangle over image in Qt

I guess there are lots of ways to achieve this. I have an application in which a video stream is shown over a custom QWidget that I have subclasses from QLabel, and painting frames using QPainter. Given that, is it possible to let the user to drag a rectangle over the image and retrieve the coordinates? The requirement is that the rectangle must be visible during the dragging.
Thanks in advance,
Have a look at QRubberBand. It allows you to place such a rect on top of e.g. a QLabel. The documentation also contains an example how to move and resize the rubberband using the mouse.
the QGraphicsView has the void setRubberBandSelectionMode ( Qt::ItemSelectionMode mode ) but i dont know if the QLabel has some similar feature ...
maybe you have to draw your own rectangle while the user drags the rectangle and catch it on mouserelease
soo long zai
In you widget you could track mouse pressed and released events and track where on the widget the corners of the selection rect are. For drawing the rectangle, I'd take a look at QStyle::drawFocusRect. I think the intent of that is to draw a rect you'd be able to see regardless of what's behind it.
Or perhaps try this:
QStylePainter painter(this);
QStyleOptionFocusRect option;
option.initFrom(this);
option.backgroundColor = palette().color(QPalette::Background);
painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);

C++/CLI Visual C++ 2010 Express - Drawing different shapes inside one panel

I got a problem. There is panel1 which i want to use as my paiting window. And i also have 2 buttons. One should draw "fillRectangle" in the middle of panel and second one should draw "fillellipse" next to it. I dont have the problem with drawing itself
Color aColor = Color::FromArgb( 255, 0, 0 );
SolidBrush^ aBrush = gcnew SolidBrush(aColor);
Rectangle rect = Rectangle(x, y, 10, 10);
e->Graphics->FillEllipse(aBrush, rect);
But i want to know, how to make that pressing each button draws different shape inside panel. Do i need to put both, ellipse and rectangle in panel1_Paint and use flags like...
if(ellip == 1) FillEllipse;
if(recta == 1) FillRectangle
which are set using buttons? I hoped i can code drawing part inside button or function, and then somehow refer to that panel. Is it possible?
If you want the buttons to have their own painting routines, then they draw themselves and your panel is unnecessary, in terms of drawing anyway, they'll have their own canvas.
If you want a panel that draws shapes on itself dependant on some property, then yes you go down something like the route you suggested, though I'd be tempted by some sort of shape class even if it wasn't a control. Would make adding other shapes or more of a shape much easier.
You could have panel carry a collection of shapes that implement say IDraw which takes a Graphics reference, and then just iterate through them and call it with Panel1's graphics instance.
If it was me though particulary if I wanted enabled, visible, tab, click etc. I'd have a ShapedButton Control.