How do I refresh MFC dialog before Sleep() call - c++

I have an array of buttons holding bitmap images.
I'm trying to change the image of one of the buttons to a different image for one second, and then change it again - but during the sleep, the UI still displays the older image as if I hadn't changed it - this means I don't see bmp_explosion displayed at all.
void COOPFinalDlg::ShowExplosion(int position)
{
SetButtonImage(position, bmp_explosion);
Sleep(1000);
SetButtonImage(position, bmp_tile);
}
void COOPFinalDlg::SetButtonImage(int buttonId, int imageId)
{
CButton* button = buttons[buttonId - ButtonRangeStart];
CBitmap bmp;
bmp.LoadBitmap(imageId);
button->SetBitmap(bmp);
}
The buttons were initalized to a bitmap the following way:
CBitmap bmp;
bmp.LoadBitmap(bmp_tile);
CRect rect(....);
CButton* button = new CButton;
button->Create(NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | DT_CENTER | WS_BORDER, rect, this, idCounter++);
button->ModifyStyle(0, BS_BITMAP);
button->SetBitmap(bmp);
buttons.Add(button);

Related

Need to Make CDockablePane Background Transparent in c++ MFC

I'm developing an application in MFC C++ and I have derived a CDockablePane object that I can view on the main window. I need the dockable pane's background to be transparent so you can see the main window through the dockable pane.
How would I accomplish this? Thanks.
this how i create the CDockable Pane object:
if (!FeaturePane.Create("Features", this, CSize (400,500), TRUE, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI))
{
TRACE0("Failed to create Program window\n");
//return false; // failed to create
return FALSE;
}
i Know you can clear the background of a CDroDialog using the following code:
BOOL CDroDialog::OnInitDialog()
{
CDialog::OnInitDialog();
SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd,GWL_EXSTYLE) ^ WS_EX_LAYERED);
SetLayeredWindowAttributes(RGB(255,0,255),0, LWA_COLORKEY);
}
BOOL CDroDialog::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CRect clientRect ;
GetClientRect(&clientRect) ;
pDC->FillSolidRect(clientRect, RGB(255,0,255)) ; // paint background in magenta
//return CDialog::OnEraseBkgnd(pDC);
return FALSE;
}

how to create or dock dockable pane on full client screen MFC?

i want to create a dockable pane full size on client screen i want it load full size on client screen by default when application load at start point.i have tried with many ways like override setwindowpos() method and setminsize() method but it didn't work window not called this method on cdockable pane here is my code on oncreate() method in parent window
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
BOOL bNameValid;
// Create output window
CString strOutputWnd;
bNameValid = strOutputWnd.LoadString(IDS_OUTPUT_WND);
ASSERT(bNameValid);
if (!m_wndOutput.Create(strOutputWnd, this, CRect(0, 0, 0, 0), TRUE, ID_VIEW_OUTPUTWND, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_BOTTOM | CBRS_FLOAT_MULTI))
{
TRACE0("Failed to create Output window\n");
return FALSE; // failed to create
}
m_wndOutput.EnableDocking(CBRS_ALIGN_ANY );
int screen_height=GetSystemMetrics(SM_CYSCREEN);
int screen_width =GetSystemMetrics(SM_CXSCREEN);
m_wndOutput.SetMinSize(CSize(screen_height,screen_width)
m_wndOutput.CalcFixedLayout(FALSE,TRUE);
DockPane(&m_wndOutput);
}

MFC SDI Create button dynamically

I am trying to create a button dynamically. I have read some other resource and make the following code:
BEGIN_MESSAGE_MAP(Cdynamic_button_sdiView, CView)
// Standard printing commands
ON_BN_CLICKED(MYBUTTONID, OnMyBN_Click)
END_MESSAGE_MAP()
void Cdynamic_button_sdiView::OnInitialUpdate()
{
CView::OnInitialUpdate();
m_Button.Create(_T("Rearrange"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 128, 32), this, MYBUTTONID); // here will create a button
}
I can make a button successfully when I start the MFC application. The problem is that when I try to open a new document by clicking:
I get an error and my app crashed at m_Button.Create(_T("Rearrange"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 128, 32), this, MYBUTTONID);
I solved the problem with the following code:
Cdynamic_button_sdiView::Cdynamic_button_sdiView()
{
// TODO: add construction code here
m_Button = NULL;
}
Cdynamic_button_sdiView::~Cdynamic_button_sdiView()
{
if (m_Button != NULL)
delete m_Button;
}
void Cdynamic_button_sdiView::OnInitialUpdate()
{
CView::OnInitialUpdate();
if (m_Button != NULL)
delete m_Button;
m_Button = new CButton;
m_Button->Create(_T("Rearrange"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 128, 32), this, MYBUTTONID); // here will create a button
}
May be the problem is I should not re-create the window inside the OnInitialUpdate()

Creating a child CWnd inside a CView paint corruption

I want to create a CWnd derived class inside a CView derived class. I am using also a window splitter to create a tree control menu to the left side and a view area in the right side like the draft below.
------------------------
| tree | View |
| menu | |
| | |
------------------------
The problem is that when CWnd::OnEraseBkgnd is called for the 1st time it erases the background from the Window's most top left corner instead of the view's client area causing a temporary corrupted area. I tried to use the SetWindowPos but didn't solve the problem.
I create the CWnd derived class like this
CRect rect;
m_MediaWindow->GetClientRect(&rect); //Get CSplitterView client area size
if (!m_videoChildWnd.Create(NULL, NULL, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CHILD | WS_VISIBLE , rect, m_MediaWindow, ID_DSHOW_RENDER_WND)) {
// failed to create child
return TRUE;
}
::SetWindowPos(m_videoChildWnd,NULL, rect.left, rect.top, rect.Width(), rect.Height(),
SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
m_videoChildWnd.ShowWindow(SW_SHOW);
Now inside m_videoChildWnd exist the OnEraseBkgnd function
BOOL CWndVideoChild::OnEraseBkgnd(CDC* pDC) {
// TODO: Add your message handler code here and/or call default
CBrush brush;
CRect rect;
GetClientRect(&rect);
brush.CreateStockObject(BLACK_BRUSH);
pDC->FillRect(rect,&brush);
return TRUE;
}
after almost 3 calls from the framework the repaint system restore the corrupted area and everything works just fine.
Any ideas how to solve this ?

How to get the background colour of CTabCtrl?

I have a CTabCtrl on my dialog, and it has several labels (CStatic) on them. The problem is, the tab control has a white background, and the labels have grey backgrounds. I know why - the parent of the labels is actually the dialog, not the tab control. However, I should be able to use CWnd::OnCtlColor to provide a custom background brush for the labels:
HBRUSH MyDialog::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
{
HBRUSH hBrush = __super::OnCtlColor(pDC, pWnd, nCtlColor);
const int dialogId = pWnd->GetDlgCtrlID();
if (dialogId == IDC_MY_CONTROL)
{
pDC->SetBkMode(TRANSPARENT);
hBrush = m_nullBrush;
}
return hBrush;
}
Here I use m_nullBrush to provide a brush to paint the background of the labels with, the only trouble is, I don't know how to get the tab's background colour, and instead have got it hardcoded with m_nullBrush.CreateStockObject(WHITE_BRUSH);.
Even if I re-parent the labels onto the tab control, they still end up with a grey background (even though the tab control has a white background).
How do I retrieve the background colour of a CTabCtrl?
You can put your controls in a child dialog and you must enable theme for this child dialog using EnableThemeDialogTexture.
#include "Uxtheme.h"
...
BOOL CTabDemoDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
COneDlg* OneDlg= new COneDlg;
OneDlg->Create(IDD_ONE, this);
AddPage(OneDlg, L"One");
return TRUE;
}
void CTabDemoDlg::AddPage(CDialog *Dialog, const wchar_t* Title)
{
if (IsAppThemed())
EnableThemeDialogTexture(*Dialog, ETDT_ENABLETAB);
CRect Rect;
TabCtl.GetWindowRect(Rect);
Rect.top+= 20;
Rect.InflateRect(-4, -4);
ScreenToClient(Rect);
Dialog->MoveWindow(Rect);
TabCtl.InsertItem(0, Title);
}
IDD_ONE DIALOGEX 0, 0, 224, 111
STYLE DS_SETFONT | DS_FIXEDSYS | DS_CONTROL | WS_CHILD | WS_VISIBLE
EXSTYLE WS_EX_CONTROLPARENT
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
CONTROL "Check1",IDC_CHECK1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,20,16,39,10
LTEXT "Static",IDC_STATIC,20,36,19,8
EDITTEXT IDC_EDIT1,20,48,40,14,ES_AUTOHSCROLL
PUSHBUTTON "Button1",IDC_BUTTON1,84,16,50,14
END