MFC how to resize CStatic to a small size - c++

I have the following issue with the CStatic control:
When I call function SetIcon:
m_CStatic.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON1));
It loads an icon that have a size 14x14 pixels, but the actual size of the control becomes 21x20 and I can not to modify it.
I tried to call:
m_CStatic.SetWindowPos(NULL,0 , 0, 14, 14, SWP_NOMOVE);
But it only cuts a size without resizing of the icon. As result I have a part of zoomed image.
Is there any way to set a size of an icon to load?
EDIT
An actual size of the IDI_ICON1 is 14x14 pixels.
Also the size of the CStatic control is 21x20 and I can not change it with the designer.
When I load an icon it is stretched. I have no idea why.

An actual size of the IDI_ICON1 is 14x14 pixels.
Also the size of the CStatic control is 21x20 and I can not change it with the designer.
If you're looking at the size of the control in the designer, you're not comparing apples to apples here. The designer is reporting the size of controls in DLUs (dialog box units), not pixels.
There is not necessarily a 1-to-1 mapping between DLUs and pixels. In fact, the whole point of a DLU is that it is pixel-independent. The actual number of pixels represented by a single DLU will change depending on the fonts and DPI of the computer you're running the application on.
So the behavior you're seeing makes perfect sense to me.
If you don't believe this DLUs vs. pixels silliness, then try running the application under the debugger and using Spy++ to investigate the actual size (in pixels) of the static control. I'll bet it's 14x14.
And no, you cannot resize a static control in the designer if you have it set to display an icon. The control is automatically sized to accommodate the icon it is displaying. That's also by design. I can't imagine why you'd want to; your whole point seems to be that you don't want the icon to be clipped.
But like I said in a comment, static controls do not automatically scale their icons. You need to write code to do the icon scaling yourself (probably by calling the DrawIconEx function). Forcing the static control to resize itself will just crop the icon or add a border around it. Adding the SS_CENTERIMAGE style, as duDE suggested will alter this behavior so that the icon is aligned to the center of the static control, subtly changing how the cropping happens. But it will still get cropped to fit the static control's size.
Do be careful, though. The whole point of my line of questioning in the comments regarding the icon actually in IDI_ICON1 is that the LoadIcon function has some important limitations. Namely, it is only designed to load icons with the SM_CXICON and SM_CYICON sizes (on most systems, that will be 32x32). It does work as expected when you only have one icon defined in the icon resource, but it will fall apart otherwise. That could have been the explanation for the stretching. Instead, it's recommended that you use the LoadImage function. The code is rather more verbose, but it's a more powerful function:
HICON hIcon = static_cast<HICON>(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ICON1),
IMAGE_ICON,
14, /* width (x dimension) */
14, /* height (y dimension) */
LR_DEFAULTCOLOR));
// (make sure to call DestroyIcon() on hIcon when you're done with it!)

Try this:
CStatic m_CStatic;
// Create a child icon static control
m_CStatic.Create(_T("my static"),
WS_CHILD|WS_VISIBLE|SS_ICON|SS_CENTERIMAGE, CRect(0 , 0, 14, 14), pParentWnd);
// Set the icon
m_CStatic.SetIcon(::LoadIcon(IDI_ICON1));
The point is SS_CENTERIMAGE:
A bitmap is centered in the static control that contains it.
The control is not resized, so that a bitmap too large for the control will be clipped.

Related

GTK3 No resize limits

Currently if I make a window in GTK3
For example 300x300
And I put a button at the bottom, right hand corner, I can not shrink my window
Size because this button is preventing me is there a function in gtk3 that can allow me to ignore all widgets, and resize to anything even 0x0
And this is the user doing this with the window resize, drag and click
And is there a way where I can set this resize limit myself, and not have this dependent on whats in my window
If you initially use set_size_request() to set the window to 300x300, then it won't shrink below that. To allow users to shrink below an initial value, use set_default_size(). I seem to have read that the minimum size of a widget is 1x1, which seems logical, as, at 0x0 you wouldn't be able to resize it anymore. If you want less than 1x1, you can use hide() and just hide the contents.
But if you have any widgets inside the window, then the minimum size is determined by the widgets! (Called the 'natural size')
To allow a window smaller that than the one determined by the widgets, you can maybe use a Gtk.ScrolledWindow.
Also, recall that the outer border is drawn by the window manager, NOT by Gtk. However, you can disable the outer border by using set_decorated(). Not that this may not work - depending if the window manager respects this (not Gtk's fault).

How to set size and Transparent / Clear CMFCToolBar Button and Icon in mfc?

I have created some (CMFCToolBar) toolbars and added buttons and icons to them. I read on Microsoft's official website that CMFCToolBar takes 23x22 button size and 16x15 icon size (ref: link).
If I use 16x15 for the icons, then icons appear blurry. This is because the icons are originally with size 16x16. I used the function SetSizes(CSize (23,23), CSize(16,16)) to change icon size but the icons do not appear right:
Is there another way to set icon and button size?
Update
I called the SetSize function before create toolbar but the icon still appear a little blurry:
I want to know if there is a way to set Icon/button Transparent or make it clear like we can set toolbar transparent through TBSTYLE_TRANSPARENT in CreateEx function.
SetSizes is a static function that affects the complete library.
It should be called before you create any toolbar or menu object.
Best location is in InitInstance of you applicxation.
But my tipp: Use the sizes that are recommended! 16x15 and 23x22....
Transparency can be done with standard 32bit RGB/A bitmaps.
If you have a 16 color bitmap you should use RGB(192,192,192) as the standard color for the background. It is automatically replaced with the needed background color.
This has been answered here too.

SDL adjust window size

I want SDL window size to stay within working area (SPI_GETWORKAREA) of windows which excludes windows taskbar other panels.
The problem is that both SDL_CreateWindow and SDL_SetWindowSize set the client area of window, not the size including window borders. So when I set window size to fit on a small working area, the borders still go out of working area.
SDL_CreateWindow: Use this function to set the size of a window's client area.
Does SDL provide a way to set window size within working area? or how do I get border size of SDL window so that I can do that myself?
You don't even need WinAPI to do that.
The size of window borders can be determined by SDL_GetWindowBordersSize(), and the part of the display not occupied by taskbar should be returned by SDL_GetDisplayUsableBounds().
With these functions, maintaining a proper window position (and maybe size) should be easy.
The only way I know of (SDL 1.2) is to first create a small window (2x2 pixels) and then check the total window size using the Windows API (GetWindowPlacement). Based on this, you can calculate the parameters necessary to get the window size you are looking for, and finally resize the window (MoveWindow).
I hope someone has a better solution, because this is a very ugly workaround.

Image in picture control in MFC dilaog is larger when running applicaiton than is shown in dialog editor

I am creating a MFC CDialog and adding a bitmap in a picture control and I have edit controls that need to be placed relative to positions on the image. However the size of the image in the picture control changes when I run my application.
This makes it difficult to align my edit boxes with the image.
Can anybody tell me why this happens?
There is no code to post as this is entirely done in the dialog editor of VS2013.
Windows adjusts the size of dialogs to match the system font, which can be changed by the user. For information about this look up dialog base units. If you need your dialog layout to match a bitmap then you need to override the Windows adjustment and explicitly set the size and location of the controls at run time. That would mean that in OnInitDialog you use MoveWindow on the dialog itself and on every control to set their size and location in pixel units that match the bitmap.

Why do I get wrong text size for the toolbar?

In a Win32 GUI application I need to determine the width of area occupied by a string on a toolbar button so that I adjust the button width accordingly. The toolbar is plain old ToolbarWindow32 windows class. I use the following code:
HDC dc = GetDC( toolbarWindowHandle );
SIZE size;
GetTextExtentPoint32( dc, stringToMeasure, tcslen(stringToMeasure), &size );
For some fixed string (say "Hello") size.cx is filled with say 72 but when I make a screenshot of the toolbar with the very same string displayed on a button I see that the string occupies say 56 pixels.
The difference clearly depends on system fonts settings. I use "large fonts" and the value obtained by code is bigger than what is occupied on screen. On machines with "small fonts" the value obtained is smaller.
I thought if I use GetTextExtentPoint32() on a window device context it will return exactly the right size. What am I doing wrong?
The font used by the toolbar won't be selected into the DC so you'll need to work out what font it is using, create a copy, select it into the DC, get the size and then select the font out (else you could end up with a resource leak). You will currently be getting the size of the system font I expect, or whatever the default DC font is.
You could try finding the font handle used by sending a WM_GETFONT message to the toolbar window but this isn't guaranteed to return the actual font used when the text is displayed. It all depends on how the toolbar works internally.
However I'm pretty sure that the Win32 toolbar uses the menu font for rendering button text, which can be discovered using a combination of SystemParametersInfo and the NONCLIENTMETRICS structure.
If I was at work I'd post some code.
Don't you just love Win32?
BTW, I use the toolbar button text feature and have never had to size the button by hand in this way.
http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms724506(VS.85).asp