CTreeCtrl - changing its position without moving the whole area - c++

Helloes and thank you for reading :)
I am writing an application in MFC/SDI. I split the window in two views. The one on the left is derived from CTreeView, the one on the right from CView. I also created an invisible splitter to part them. Here's the picture showing it: http://i.imgur.com/hdKqArZ.png (the area on the left is CTreeView derived, my bad). I wanted to move the CTreeCtrl so it doesn't cover the picture. I googled for the possible solution but the only one I found doesn't work quite as I expected. Using GetWindowRect moves the whole area and doesn't allow me to draw on the area that was previously a part of the rectangle but no longer is. So when I move the CTreeCtrl I can't put a picture in the area above it. I am doing all these things in OnInitialUpdate().
Here's a sample code of what I've found on the internet and tried doing:
CTreeCtrl &list_recipes = this -> GetTreeCtrl();
list_recipes.GetWindowRect(&rect);
ScreenToClient(&rect);
rect.top += 100;
rect.bottom += 100;
list_recipes.MoveWindow(&rect);
// inserting items into the CTreeCtrl
// (...)
CImage image;
CDC dc;
image.LoadFromResource(AfxGetInstanceHandle(), IDB_CUPCAKE);
dc.CreateCompatibleDC(pDC);
CRect rect3(0,0,202,126);
image.Draw(pDC -> m_hDC,rect3);
Any help will be appreciated, thanks in advance :) And I have to use MFC, it wasn't my decision.

I would avoid drawing your image on the treeview. Try using a separate view which contains the treeview and a static control with the treeview moved below.

Instead of CTreeView you could use CFormView for the left pane. On this form view (which uses a dialog template) put a CTreeCtrl and a CStatic for the picture.

Related

Change style of CSpinButtonCtrl

I have tried to change orientation property of CSpinButtonCtrl in C++ MFC.
By default I have specified orientation property as vertical, nevertheless when I try to change property style to horizontal (UDS_HORZ) during execution, it doesn't work...
Sample code
CRect rect;
CWnd *pWnd;
pWnd = GetDlgItem(IDC_SPIN_GRAD_CONTRAST);
pWnd->GetWindowRect(&rect);
((CSpinButtonCtrl*)pWnd)->Create(WS_VISIBLE | UDS_HORZ, rect, pWnd->GetParent(), IDC_SPIN_GRAD_CONTRAST);
It creates a horizontal spin button control.
What I pretend to do is find any way to change the style without changing position and behaviour of the control previously created
I will appreciate any kind of help.
Some styles of controls can only be used when you create the control.
AFAIK you have to recreate the control.
On the other hand I looked into the source code of the CMFCSpinButtonCtrl. Using this allows dynamically changing the orientation. Make sure that you force to redraw the control.
PS: Changing this at runtime seams to be a strange scenario for me.

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);

Qt - QGraphicsView without ScrollBar

I am trying to show a picture in it's full view using QGraphicsScene. But when ever I put the QgraphicsScene inside the QGraphicsView, I am getting a scroll bar. I tried so many ways But all are went to veins. So can anybody tell me how to obtain the full view without the scrollbar.
You might be getting scrollbars because the scene is larger than the usable area within the graphics view. By default, a QGraphicsView comes with a 1-pixel margin. To fix this, you can try:
QRect rcontent = graphicsView.contentsRect();
graphicsView.setSceneRect(0, 0, rcontent.width(), rcontent.height());
I had been getting scrollbars because I was manually setting the scene rect to the size of the graphics item I was adding -- which was as large as the QGraphicsView widget. I wasn't taking into account the margin.
QGraphicsView v;
v.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
v.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
To adjust the scrolling programmatically once these have been hidden, use one of the overloads of v.ensureVisible().

Creating a ruler bar in MFC

What's the best way to go about creating a vertical and horizontal ruler bars in an SDI app? Would you make it part of the frame or the view? Derive it from CControlBar, or is there a better method?
The vertical ruler must also be docked to a pane and not the frame.
To make it a little clearer as to what I'm after, imagine the vertical ruler in the Dialog Editor in Visual Studio (MFC only). It gets repositioned whenever the tree view is resized.
I would not use control bars. I have no good reason other then (IMOHO) are difficult to get to do what you want - if what you want if something other than a docking toolbar.
I would just draw them directly on the View window using GDI calls.
I guess I might think about making each ruler its own window, and draw the rulers on their own window. I would then create these two CWnd derived classes in the view and position as child windows. This is good if you want to interact with the mouse on these rulers (easier to sort out what messages are for the rulers).
I ended up deriving the ruler from CWnd as you suggested and skipping the control bar route. This code works in either case:
m_wndSplitter.CreateStatic(this, 1, 3);
m_wndLeftPane.Create(&m_wndSplitter,WS_CHILD|WS_VISIBLE,m_wndSplitter.IdFromRowCol(0, 0));
m_ruler.Create(&m_wndSplitter,WS_CHILD|WS_VISIBLE,m_wndSplitter.IdFromRowCol(0, 1));
m_wndSplitter.CreateView(0, 2, pContext->m_pNewViewClass, CSize(300, 0), pContext);
SetActiveView((CScrollView*)m_wndSplitter.GetDlgItem(m_wndSplitter.IdFromRowCol(0, 2)));