Rectangle in MFC - c++

I try to draw a rectangle on a mfc window using the instructions by: http://msdn.microsoft.com/en-US/library/8w4fzfxf%28v=VS.80%29.aspx . Much though I tried, the Rectangle appears on the border of the window covering the whole of it. What is the problem with the following code int the function OnDraw(CDC* pDC) ? What can be done to draw a Rectangle with particular coordinates in the window?
CPen penBlack;
penBlack.CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
CPen* pOldPen = pDC->SelectObject(&penBlack);
CPoint pt(10, 10);
CSize sz(100, 50);
CRect myRect(pt, sz);
GetClientRect(&myRect);
pDC->Rectangle(&myRect);

Drop the call to GetClientRect.
That function will write to the rectangle object passed to it, so by calling, you're overwriting your specific coordinates that you set up just before the call using pt and sz.

As #stakx suggested you should remove the GetClientRect, which gets the whole window client area, and overwrites your own rectangle.
As to the instruction, it first gets the whole client area, and shrinks the rectangle to get the rectangle to draw, so GetClientRect is needed there.

This site will help you to draw the rectangle in mfc Dialog-based-application.
http://cboard.cprogramming.com/windows-programming/37788-drawing-mfc.html
http://cboard.cprogramming.com/cplusplus-programming/102490-cplusplus-mfc-rectangle-class.html
Don't use GetClientRect().It will override your previous coordinates.

Related

How can I paint a rectangle around a window without overriding the title bar in win32

I want to draw a rectangle around my window but I don't want to override the title bar.
what I wrote so far in the window callback function is:
case WM_NCPAINT:
{
HDC hdc;
RECT rect;
HPEN pen;
hdc=GetDCEx(hWnd,(HRGN)wParam,DCX_WINDOW|DCX_CACHE|DCX_INTERSECTRGN|DCX_LOCKWINDOWUPDATE);
GetWindowRect(hWnd,&rect);
pen=CreatePen(PS_SOLID, 10, RGB(255, 0, 0));//red pen 10 pixels in size
SelectObject(hdc,pen);
Rectangle(hdc,0,0,(rect.right-rect.left),(rect.bottom-rect.top));
DeleteObject(pen);
ReleaseDC(hWnd,hdc);
}
break;
However, this draws over the window title bar with white brush.
How can I make it not to paint over the title bar? I'm loosing the title bar text and the menu...
I have tried using HOLLOW_BRUSH before creating the pen as follows:
HBRUSH b=CreateSolidBrush(HOLLOW_BRUSH);
SelectObject(hdc,b);
But that only caused the title bar to not be drawn at all (being black).
By handling the WM_NCPAINT message, you are telling the window manager that you are taking responsibility for painting the entire non-client area, and so the window manager will not draw any of it for you.
If you want the original title bar to be drawn then you need to call DefWindowProc() first, then do your own drawing "on top" of what it draws.
You may also need to use ExcludeClipRect() to prevent the client area from being drawn over if you wish to draw the entire non-client area at once with a single rectangle.

What is the difference between GetClientRect and GetWindowRect in WinApi?

What of these should I use in InvalidateRect to refresh my window? And why?
The window rect includes the non-client area, i.e. the window borders, caption bar etc. The client rect does not.
GetWindowRect returns a rect in screen coordinates whereas GetClientRect returns a rect in client coordinates.
InvalidateRect receives a rect in client coordinates. If you want to invalidate your entire client area, then pass NULL to InvalidateRect. You could pass in the rect returned by GetClientRect, but it is far simpler and clearer to pass NULL.
A very simple explanation is that GetWindowRect() gives you the rectangle that includes the borders of the window. GetClientRect() gives you the rectangle that excludes the borders - the area that is allocated to the window specific drawing.
Please note that GetWindowRect() returns a rectangle in screen coordinates - coordinates that are relative to the screen/monitor. GetClientRect() returns a rectangle that is relative to itself.
GetClientRect gets the coordinates of the window's client area. Specifically this is the area inside the window chrome and excludes the header etc. One of the comments on the MSDN page sums it up quite well:
I would say that this function return size of the area that I can render to.
GetWindowsRect gets the coordinates of the whole window. This includes the header, status bar etc. However according to a comment on the MSDN page
Apps under Vista that are not linked with WINVER=6 will receive a misleading set of values here, that do not account for the extra padding of "glass" pixels Vista Aero applies to the window.
So unless this have been fixed for Windows 7 double check the result you get and make sure you have the correct value of WINVER.
From MSDN:
GetWindowRect
Retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.
GetClientRect
Retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
More: client rect does not include title bar, borders, scroll bars, status bar...

How to increase thickness of slider conrol in mfc?

How can I increase the thickness of a CSliderCtrl (slider control) in MFC?
MoveWindow() can be used for any MFC control. Try this:
CRect rc;
slider.GetWindowRect(rc); // Get the slider rectangle in absolute corrdinates
rc.InflateRect(30, 30); // Do whatever you want with your rectangle;
ScreenToClient (rc); // Convert to dialogs's coordinates
slider.MoveWindow(rc); // Move it!
Update:
For further customization you have to make an owner-drawn CListCtrl. You may take this article as a good start for that

c++ repaint part of window

i know how to repaint the full window but i don't know how to repaint a pieace of window like i draw a squre using gdi+ than i want to change it's coordinates so i want to repaint the squre not the whole window
anyidea?
i also tried this
RECT rect2;
rect2.left=0;
rect2.top=100;
rect2.right=225;
rect2.bottom=300;
InvadiateRect(hwnd, &rect2, false);
it still repaint the whole window
One way to do this is to call InvalidateRect() with a rectangle that is large enough to cover both the old and new positions of the square you moved. Windows will then call your WM_PAINT handler to repaint the area of the screen that changed.
The UnionRect() function is helpful for calculating this repaint rectangle.

How Can I Create Rounded Rectangle Buttons in WM6?

Yes, like those pretty buttons on the iPhone. ;)
I've been searching and reading for days now and everytime I find something that will get me close (like CreateRoundRectRgn), it blows up because Windows Mobile 6 GDI+ doesn't support it.
I can do the whole owner draw thing and such. But how do I curve those hard corners and reshape a button? :/
Note Tools available: Native Win32 only (no MFC)
That thought has occured to me, but it leaves two issues:
1) Won't the bitmap with rounded edges still leave the corners of the button visible.
2) Bitmaps are great for fixed screen size. But having a variety of resolutions, my goal is to dynamically create the button bitmap in memory at run-time and use it that way.
I've got it working with square buttons. Yet I have seen rounded edge buttons used by other software. There must be a way to reshape buttons.
Getting pretty buttons like that is typically done by doing a complete owner-drawn button and drawing an image that a graphic designer created to it rather than letting GDI do any of the control painting. You simply need an image for "up" and an image for "pressed". You can manually draw in the focus or use yet another image with a ROP mask to draw it on the button as well. To get the nice "rounded" effects, you simply create the image with a background color that you then use as a transparency color.
Tee scaling issue is somewhat unique to WinMo, since iPhone really has only one resolution. If you need to target different resolution WinMo devices you can do one of 2 things (which you use depends on the images you're using). Eitehr just draw the image scaled, or include different size versions of the images and decide at runtime which to use based on screen resolution.
You can use the RoundRect GDI function to do it on an owner drawn control.
//Set up a brush and pen
HBRUSH brush = CreateSolidBrush(RGB(255, 0, 0));
HPEN pen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
//Select it
HGDIOBJ old_brush = SelectObject(hdc, brush);
HGDIOBJ old_pen = SelectObject(hdc, pen);
//Draw your rectangle
RoundRect(hdc, m_rect.left, m_rect.top, m_rect.right, m_rect.bottom, 10, 10);
//restore the old state of your HDC
SelectObject(hdc, old_brush);
SelectObject(hdc, old_pen);
//Clean up
DeleteObject(brush);
DeleteObject(pen);
If you want to do something fancier like filling it with a gradient you can draw your gradient to an off screen buffer and use CreatePatternBrush to draw from it.