Creating a child window within a frame - visual-c++

I'd like to create a frame window that can contain child windows, without using MDI which is way too complex for my needs.
So, I create a frame window CMainFrame derived from CFrameWnd and then a class Child derived from CWnd, with the following constructor:
Child::Child()
{
CString strWndClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW, 0, 0, 0);
CWnd *cwnd = AfxGetApp()->m_pMainWnd;
Create(strWndClass, "Title child", WS_CHILD, CRect(0, 0, 200, 200), cwnd, 0, NULL);
}
But when I create the Child window from the main frame:
void CMainFrame::Click()
{
Child *child = new Child();
child->ShowWindow(SW_SHOW);
}
nothing appears. Though, the creation of the child did not fail. What's the problem?

Related

Difficulty creating thumbnails of all monitors connected to laptop to populate a list control

I have some code to create thumbnails of the monitors connected to a PC. They are rendered into a list control.
This is the code that iterates the monitors to create the thumbnails:
void CCenterCursorOnScreenDlg::DrawThumbnails()
{
int monitorIndex = 0;
// Stop redrawing the CListCtrl
m_ListThumbnail.SetRedraw(FALSE);
// Loop monitor info
for (auto& strMonitor : m_monitors.strMonitorNames)
{
// Create the thumbnail image
CImage monitorThumbnail;
CreateMonitorThumbnail(monitorIndex, monitorThumbnail, true);
// Convert it to a CBitmap
CBitmap* pMonitorThumbnailBitmap = CBitmap::FromHandle(monitorThumbnail);
// Add the CBitmap to the CImageList
m_ImageListThumb.Add(pMonitorThumbnailBitmap, nullptr);
// Build the caption description
CString strMonitorDesc = m_monitors.strMonitorNames.at(monitorIndex);
strMonitorDesc.AppendFormat(L" (Screen %d)", monitorIndex + 1);
// Add the item to the CListCtrl
m_ListThumbnail.InsertItem(monitorIndex, strMonitorDesc, monitorIndex);
monitorIndex++;
}
// Start redrawing the CListCtrl again
m_ListThumbnail.SetRedraw(TRUE);
}
The m_monitors variable is an instance of:
struct MonitorRects
{
std::vector<RECT> rcMonitors;
std::vector<CString> strMonitorNames;
static BOOL CALLBACK MonitorEnum(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData)
{
MonitorRects* pThis = reinterpret_cast<MonitorRects*>(pData);
pThis->rcMonitors.push_back(*lprcMonitor);
MONITORINFOEX sMI{};
sMI.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMon, &sMI);
pThis->strMonitorNames.emplace_back(sMI.szDevice);
return TRUE;
}
MonitorRects()
{
EnumDisplayMonitors(nullptr, nullptr, MonitorEnum, (LPARAM)this);
}
};
The initial thumbnail sizes is determined in OnInitDialog:
// 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;
These values are used when creating the CImageList to show the images.
Finally, I have the function that is supposed to make the thumbnails:
bool CCenterCursorOnScreenDlg::CreateMonitorThumbnail(const int iMonitorIndex, CImage &rImage, bool bResizeAsThumbnail)
{
const CRect rcCapture = m_monitors.rcMonitors.at(iMonitorIndex);
// Destroy the currently contained bitmap to create a new one
rImage.Destroy();
// Massage the dimensions as we want a thumbnail
auto nWidth = rcCapture.Width();
auto nHeight = rcCapture.Height();
if (bResizeAsThumbnail)
{
nWidth = m_iThumbnailWidth;
double dHt = ((double)rcCapture.Height() / (double)rcCapture.Width()) * (double)m_iThumbnailWidth;
nHeight = (int)dHt;
if (nHeight > m_iThumbnailHeight)
{
// Aspect ratio of this monitor is not the same as the primary monitor
}
}
// Create bitmap and attach it to this object
if (!rImage.Create(nWidth, nHeight, 32, 0))
{
AfxMessageBox(L"Cannot create image!", MB_ICONERROR);
return false;
}
// Create virtual screen DC
CDC dcScreen;
dcScreen.CreateDC(L"DISPLAY", nullptr, nullptr, nullptr);
// Copy (or resize) the contents from the virtual screen DC
BOOL bRet = FALSE;
auto dcImage = rImage.GetDC();
if (bResizeAsThumbnail)
{
// 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);
}
else
{
// Copy
bRet = ::BitBlt(dcImage, 0, 0,
rcCapture.Width(),
rcCapture.Height(),
dcScreen.m_hDC,
rcCapture.left,
rcCapture.top, SRCCOPY | CAPTUREBLT);
}
// Do cleanup and return
dcScreen.DeleteDC();
rImage.ReleaseDC();
return bRet;
}
On my PC at how where I have two monitors of the same dimensions it works fine. But when I tried it at another site, which has two large TVs connected to a laptop, and an additional monitor connected to the laptop, the thumbnails render wrong:
I would say that the thumbnail of screen two (the TVs) is about 2/3 of the size.
I was hoping to create a set of thumbnails for my list control of all monitors and did not anticipate this. What am I doing wrong?
I am wondering if the SetStretchBltMode / StretchBlt logic is incorrect that does the transform.
Update
I just realised:
GetMonitorInfo provides the screen data in virtual coordinates.
StretchBlt uses logical screen coordinates.
Is this the reason I am ended up with an incorrect thumbnail when I am trying to take another monitors screen and scale it down?

How to draw an OCX to a CBitmap (MFC, c++)

How can I draw an OCX (I do have the sources) to an CBitmap-Object or something alike?
Background: My client creates PDF-Documents and part of these documents is an Output from an OCX. The PDF-lib-Interface has a Method to put an Image from an CBitmap-Object to the PDF-Page.
So what i want to do ist let the Program create an CBitmap-Object, pass that to the OCX to let it draw its content onto it and then pass the he CBitmap to the PDF-library to get it into the document.
So the main question is: how to draw my ocx into a CBitmap-Object?
I'm using Visual C++, Windows, MFC/ATL.
Thanks a lot
actually I didn't manage to render the OXC to a CBitmap (just got a black box drawn) but rendering into an ATL::CImage and making a CBitmap out of it worked:
ATL::CImage* CPrnBasePrinter::DrawBeamerToImage(CSPCListView* pListViewWithBeamer, const CRect& rect4Beamer)
{
ASSERT(pListViewWithBeamer != nullptr);
auto* pRetVal = new CImage();
pRetVal->Create(rect4Beamer.Width(), rect4Beamer.Height(), 24);
HDC hdcImage = pRetVal->GetDC();
//Draw Control to CImage
pListViewWithBeamer->DrawBeamerToDC(HandleToLong(hdcImage),
rect4Beamer.left, rect4Beamer.top, rect4Beamer.right, rect4Beamer.bottom);
pRetVal->ReleaseDC();
return pRetVal;
}
void CPrnBasePrinter::DrawImageFromCImage(
const ATL::CImage* pImage, const CRect& rect) const
{
CBitmap* pbmp2Print = CBitmap::FromHandle(*pImage);
// Get the size of the bitmap
BITMAP bmpInfo;
pbmp2Print->GetBitmap(&bmpInfo);
//virtual - Draws the CBitmap to an Printer-DC or a PDF-Document
DrawImageFromLoadedBitmap(pbmp2Print, &bmpInfo, rect);
}
void CPrnBasePrinter::Draw()
{
//m_pListviewDataSource is an OCX capable of drawing itself into a given DC
ATL::CImage* pBeamerImage = DrawBeamerToImage(m_pListviewDataSource, CRect(0, 0, 100, 50));
if (pBeamerImage != nullptr){
DrawImageFromCImage(pBeamerImage, CRect(0, 0, 100, 50));
delete pBeamerImage;
}
}

Xamarin iOS : Center UILabel inside UICollectionView Header created programmatically

The solution for centering any subview within a parent is usually simple, however, it doesn't seem to work in my case.
I'm working with a UICollectionView and have added a Header class programmatically. I have this constructor, where I also try to center the label within the screen:
[Export("initWithFrame:")]
public Header(System.Drawing.RectangleF frame) : base(frame)
{
label = new UILabel
{
Frame = new System.Drawing.RectangleF(frame.Size.Width / 2, 50, 200, 50),
BackgroundColor = UIColor.Clear,
TextColor = UIColor.White,
Font = UIFont.FromName("HelveticaNeueLTStd-ThCn", 35f),
Text = DateTime.Now.ToString("Y")
};
AddSubview(label);
}
And I initialize the class inside the UICollectionViewSource 's constructor like this:
public MyCollectionViewDataSource(MainController mainController, DateTime currentDate)
{
try
{
controller = mainController;
new Header(new RectangleF(0, 0, (float)mainController.View.Frame.Size.Width, 200));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
}
}
What exactly am I missing because this usually works in other instances but seems to fail here?
This is what it looks like :
I found an explanation here iOS Layout Gotchas by Adam Kemp which helped me resolve this issue.
The first solution
One very common mistake I made was adding the layout definition code in the constructor, instead of doing it in the rightful place : the LayoutSubviews override in this case.
Giving the label the frame size in the constructor assumes a static size set at the time of construction, which may later change depending on the screen size.
The second solution
He explains that :
Frame sets the position of a view within its parent while Bounds is in the coordinate system of the view itself (not its parent).
So, to center the UILabel, I used bounds and center together and this worked for me.
[Export("initWithFrame:")]
public Header(CGRect bounds) : base(bounds)
{
label = new UILabel
{
BackgroundColor = UIColor.Clear,
TextColor = UIColor.White,
Font = UIFont.FromName("HelveticaNeueLTStd-ThCn", 35f),
Text = DateTime.Now.ToString("Y"),
TextAlignment = UITextAlignment.Center
};
rectangle = bounds;
AddSubview(label);
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
label.Bounds = new CGRect (rectangle.Size.Width / 2, 50, 200, 50);
label.Center = new PointF((float)rectangle.Size.Width/2,50);
}

Adding CSliderCtrl in CStatusBar in MFC application

I am trying to add to CSliderCtrl in CStatusBar. For this
- Created CSliderCtrl in CMainFrame class
- In CMainFrame::OnCreate() added code for creating statusbar and slider bar control as
bStatus = m_ZoomSlider.Create(
WS_CHILD | WS_VISIBLE,
CRect(0, 0, 100, 30),
&m_StatusBar,
56666);
Things are working fine.
Now I want this slider to be on the right side of the status bar. For this I've added a INDICATOR in the status bar and I am trying to get the rect of this indicator and placing the slider over that rect.
CRect rectSlider;
m_StatusBar.GetItemRect(1, &rectSlider);
bStatus = m_ZoomSlider.Create(
WS_CHILD | WS_VISIBLE,
rectSlider,
&m_StatusBar,
56666);
Here the rectSlider is having negative value, causing the slider to be invisible.
I need to know Is this the correct way for doing this. Any suggestion for advice will be very helpful.
I am using Visual Studio 2005.
You should be using GetRect rather than GetItemRect, I think
The slider control cannot be displayed because its Z-order is not correct. So override on resize to reposition the slider properly. &CWnd::wndTop means placing the window at the top of the Z-order
Firstly, define CSliderCtrl *m_pZoomSlider in MainFrame.h
The following code used the lazy initialization pattern: initialize when required, free the allocated memory when frame is being destroyed.
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
...
ON_WM_SIZE()
END_MESSAGE_MAP()
void CMainFrame::SetSliderPosition(int pos)
{
if (!m_pZoomSlider) {
CRect rectSlider;
m_wndStatusBar.GetItemRect(1, &rectSlider);
rectSlider.DeflateRect(1, 1); // 1 pixel border...
m_pZoomSlider = new CSliderCtrl();
m_pZoomSlider->Create(WS_CHILD | WS_VISIBLE, rectSlider, &m_wndStatusBar, ID_INDICATOR_SCALE_SLIDER);
m_pZoomSlider->SetRange(1, 100);
}
RECT rc;
m_wndStatusBar.GetItemRect(pos, &rc);
// Reposition the slider control correctly!
m_pZoomSlider->SetWindowPos(&CWnd::wndTop, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
}
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
SetSliderPosition(1); //index of indicator of status bar
}
BOOL CMainFrame::DestroyWindow()
{
if (m_pZoomSlider) {
m_pZoomSlider->DestroyWindow();
delete m_pZoomSlider;
}
return CFrameWnd::DestroyWindow();
}

How do I make a button caption bold in MFC?

I have a button on an MFC dialog. How can I make the text bold?
class CYourDialog : CDialog
{
public:
virtual BOOL OnInitDialog(); // override
private:
CButton m_button;
CFont m_font;
};
BOOL CYourDialog::OnInitDialog()
{
__super::OnInitDialog();
CFont* font = m_button.GetFont();
LOGFONT logFont;
font->GetLogFont(&logFont);
logFont.lfWeight = FW_BOLD;
m_font.CreateFontIndirect(&logFont);
m_button.SetFont(&m_font);
return TRUE; // => system will set input focus to the first control item in the dialog box; (0 => you set the focus to a control of your choice)
}
You can create a new CFont and call WM_SETFONT on the button. Something like this:
// note: m_font is a class variable of type CFont
m_font.CreateFont(10, 0, 0, 0, FW_BOLD, 0, 0, 0, 0, 0, 0, 0, 0, "Arial")
GetDlgItem(IDC_BUTTON1)->SendMessage(WM_SETFONT, WPARAM(HFONT(font)), 0);

Resources