I'm trying to draw a CSpinButtonCtrl as a buddy of an edit box in Windows 7. When my CEdit window is 12 dialog units high, the spin buttons are scaled really badly and the top border is clipped off.
This looks pretty ugly. How can I get around this, or must I restrict my CEdit controls to be 14 dialog units high?
My controls are declared thusly:
EDITTEXT IDC_LOWER_EDIT,51,20,63,12,ES_MULTILINE | ES_WANTRETURN,WS_EX_RIGHT
CONTROL "",IDC_LOWER_SPIN,"msctls_updown32",UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS | UDS_NOTHOUSANDS,104,17,11,12
I've tried resizing using MoveWindow, but that doesn't help. Any ideas?
I found the code for changing the width
CWnd* pWnd = GetDlgItem( IDC_SPIN1 );
CRect rect;
pWnd->GetWindowRect( &rect );
ScreenToClient( &rect );
rect.right += 5 ; // make 5 pixels wider
pWnd->MoveWindow(&rect) ;
Put it in the OnInitDialog().
I think I would go for #2 - are you that pressed for screen space?
Another option is: leave it unattached (remove UDS_ALIGNRIGHT) and place it right next to the edit control.
Related
I am perplexed about the SizeToContent method of the CMFCMenuButton control.
This is my dialog in the IDE:
As you can see, I have specifically made the button wider than the two on the far right.
I added the following code to OnInitDialog:
// Resize (if required)
const auto sizNewButton = m_btnReset.SizeToContent(true);
CRect rctButton;
m_btnReset.GetWindowRect(&rctButton);
if(sizNewButton.cx > rctButton.Width())
{
m_btnReset.SizeToContent();
}
Yet, when I run my application in English:
It has made it smaller. My application supports 50+ languages by using satellite DLLs and I was hoping to only resize to content if it was required. But it seems to resize it anyway. Have I missed a step here?
I have checked the properties for the control in the IDE and it is not set to auto resize:
I notice that the help documentation states:
The new size of the button is calculated to fit the button text, image, and arrow. The framework also adds in predefined margins of 10 pixels for the horizontal edge and 5 pixels for the vertical edge.
I had a look at my button:
Default size: 48 x 23 (the GeWindowRect result).
Calculated size: 57 x 23 (the SizeToContent result).
If I adjusted my code like this:
if((sizNewButton.cx - 10) > rctButton.Width())
That would bring it down to 47 and thus would not resize. I am assuming the code is not working right because of the padded margin that GetWindowRect knows nothing about.
Searched it, and found that the problem is MFC's CMFCMenuButton::SizeToContent() implementation in afxmenubutton.cpp:
CSize CMFCMenuButton::SizeToContent(BOOL bCalcOnly)
{
CSize size = CMFCButton::SizeToContent(FALSE); // <==== The culprit!!!
size.cx += CMenuImages::Size().cx;
if (!bCalcOnly)
{
SetWindowPos(NULL, -1, -1, size.cx, size.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}
return size;
}
That is, it calls the base implementation of SizeToContent() with the bCalcOnly parameter set to FALSE, which means it will also resize the control to just fit the text (without the drop-down arrow). This is less than required for the text plus the arrow, and of course the original size is lost.
A workaround can be get the (original) width, before the SizeToContent() call, and work with this instead of the new one:
CRect rctButton;
m_btnReset.GetWindowRect(&rctButton);
const auto nOrigWidth = rctButton.Width(); // Store the original width
const auto sizNewButton = m_btnReset.SizeToContent(true); // This resizes the control!!!
if (sizNewButton.cx > nOrigWidth) // Compare to the original width rather than the new one
m_btnReset.SizeToContent();
else // Restore original width
m_btnReset.SetWindowPos(NULL, -1, -1, nOrigWidth, sizNewButton.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
Alternative Workaround:
Define a new CMFCMenuButton-based class, overriding SizeToContent() - in the implementation call the base CMFCButton::SizeToContent() with the bCalcOnly parameter passed by the caller, not with FALSE. Map the control to this class instead of CMFCMenuButton. That is use a class that fixes it. Too much of an overkill for just a workaround though.
At the moment I am having to fudge my code like this:
CRect rcList;
m_ListThumbnail.GetClientRect(rcList);
rcList.DeflateRect(25, 25);
// Use monitor 1 to work out the thumbnail sizes
CRect rcMonitor = m_monitors.rcMonitors.at(0);
m_iThumbnailWidth = rcList.Width();
double dHt = ((double)rcMonitor.Height() / (double)rcMonitor.Width()) * (double)m_iThumbnailWidth;
m_iThumbnailHeight = (int)dHt;
I fudge it by deflating the rectangle by 25. m_ListThumbnail is a CListCtrl and I am trying to render my thumbnails so that I do not need a horizontal scroll bar.
When I render the thumbnails of the monitors, I attempt to massage these thumbnail values (incomplete):
nWidth = m_iThumbnailWidth;
double dHt = ((double)rcCapture.Height() / (double)rcCapture.Width()) * (double)m_iThumbnailWidth;
nHeight = (int)dHt;
if (nHeight > m_iThumbnailHeight)
{
AfxMessageBox(L"Need to investigate!");
}
Where rcCapture is the size of the monitor.
If I remove the DeflateRect line, my window displays like this:
As you can see, it is note as I expected. There is a horizontal scroll bar and I have to resize quite a bit to see the thumbnail:
All I want to compute is a set of thumbnail dimensions so that the scaled down monitor image is going to fit in the CListCtrl. I don't really want the user to have to resize the window.
Update
With my adjusted code which now uses the primary monitor aspect ratio to work out the thumbnail sizes (as added above) renders the app with better whitespace:
That was the reason of the extra space at the bottom because the monitors were 16:9 and I was squishing into 4:3. So that is fixed.
But as you can see, using the CListCtrl client width is not sufficient.
Update
This is the rendering code:
// Set the mode first!
SetStretchBltMode(dcImage, COLORONCOLOR);
int iTop = (m_iThumbnailHeight - nHeight) / 2;
// Copy (and resize)
bRet = ::StretchBlt(dcImage, 0, iTop,
nWidth,
nHeight,
dcScreen.m_hDC,
rcCapture.left,
rcCapture.top,
rcCapture.Width(),
rcCapture.Height(), SRCCOPY | CAPTUREBLT);
The control renders the icon with a margin. When I used GetItemPosition function it indicated that the x value was 21. This is why my DeflateRect(25, 25) worked. But I don;t know how the CListCtrl computed that margin value.
Eventually I found out how to do this without deflating, by using the SetIconSpacing function. Like this:
m_ListThumbnail.SetIconSpacing(
CSize(m_iThumbnailWidth, ::GetSystemMetrics(SM_CXHSCROLL)));
Once the above has been done the window looks like this:
Things might be a little different when there are 3 or 4 monitor thumbnails, thus causing a vertical scroll bar. But I only have two monitors to test with.
Control background is getting changed after minimizing and maximizing the window.
I want the background to be same and transparent.
This is an ActiveX control. which can be used in multiple projects.
CEdit is the base class for this control on which I have added some extra feature.
I tried setting Bkmode in OnCtlColor and OnCtlColor but it is not working out.
I resolved this by fetching the background color and filling the rec of the control
BOOL CComboBoxCtrl::OnEraseBkgnd(CDC* pDC)
{
COleControl::OnEraseBkgnd(pDC);
RECT rc,rc1;
GetClientRect(&rc);
// Get the color from the parent window
COLORREF crBkgnd = COleControl::AmbientBackColor();
//Fill the rect to overcome the black background issue
pDC->FillSolidRect(&rc,crBkgnd);
if(inputbox != NULL)
inputbox->Invalidate(TRUE);
return S_OK;
}
I have an old VC6 application to maintain.
When it starts, it shows the system buttons (minimize, maximize, close) in the usual Windows 7 or Windows 8 style.
As soon as I do an operation that triggers a redraw (SetMenu, move the window…), these buttons change to a Windows 2000 style (just gray boxes with black icons). We have other applications (VC6 as well) that do not have this behaviour.
Any idea where I should start to look ?
Edit:
After more investigation, I found that a call to DrawFrameControl is causing all this trouble:
CWindowDC dcWnd(this);
CRect rectWindow, rectClient;
GetWindowRect(&rectWindow);
m_rectDisconnectButton.left = rectWindow.Width() - DISC_BUTTON_WIDTH 6;
m_rectDisconnectButton.top = ::GetSystemMetrics(SM_CYCAPTION) + 4;
m_rectDisconnectButton.right = m_rectDisconnectButton.left + DISC_BUTTON_WIDTH;
m_rectDisconnectButton.bottom = m_rectDisconnectButton.top + BUTTON_HEIGHT;
if ( m_bDisconnectButtonPressed )
dcWnd.DrawFrameControl(&m_rectDisconnectButton, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
If I comment the last call, obviously my button will not be drawn, but I no longer have the issue with the "system buttons".
I want to display 9 buttons in the middle of the display, three on each row. In conclusion a big square that is composed from 9 little buttons displayed in the middle of the screen.
So I think I need to know the screen size to display buttons.
you only need to use Tablelayout or Linerlayout for this.but if need to know screen size it get as following :
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();