Closing all child windows in MFC MDI applications - visual-c++

I am using an MFC MDI application. I want to close all child windows on a notification. For this purpose, I am using this code:
CMDIFrameWnd *pFrame = NULL;
CMDIChildWnd *pChild = NULL;
CDocTemplate* pDocTemplate = NULL;
CDocument* pDoc = NULL;
for (POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition(); pos != NULL; )
{
pDocTemplate = AfxGetApp()->GetNextDocTemplate( pos );
for (POSITION pos1 = pDocTemplate->GetFirstDocPosition(); pos1 != NULL; )
{
if (pos1 == NULL)
break;
CDocument* pDoc = pDocTemplate->GetNextDoc( pos1 );
for (POSITION pos2 = pDoc->GetFirstViewPosition(); pos2 != NULL; )
{
CView* pView = (CSignalWindow*)pDoc->GetNextView( pos2 );
pView->CloseWindow();
}
}
}
When this code is executed, in debug mode, it looks its closing all window and UI show black screen in entire child window area.
I want to update this window area after closing all child windows.
How I can update this area?

You should not close the view. Just close the parent frame.
for (POSITION posTemplate = AfxGetApp()->GetFirstDocTemplatePosition(); pos != NULL; )
{
pDocTemplate = AfxGetApp()->GetNextDocTemplate(posTemplate);
POSITION posDoc;
while (posDoc = pDocTemplate->GetFirstDocPosition())
{
CDocument* pDoc = pDocTemplate->GetNextDoc(posDoc);
POSITION posView;
while (posView=pDoc->GetFirstViewPosition())
{
CView* pView = pDoc->GetNextView(posView);
pView->GetParentFrame()->DestroyWindow();
}
}
}
Because you want to close all, you just need to get the head of the lists and remove it.
Using DestroyWindow for the frame may delete more than one view, if you collect views in one child frame (ie. splitter window).
There should never be a problem in repainting, because the parent window always redraws its client area, when a child window is destroyed, as long as you don't use SetRedraw...

Related

In MFC how to increase the time tooltips remain Visible

I am using below code to create tooltips.
m_ctrlToolTip.Create(this, TTS_ALWAYSTIP|TTS_BALLOON);
m_ti.cbSize = sizeof(TOOLINFO);
m_ti.uFlags = TTF_IDISHWND|TTF_TRACK|TTF_TRANSPARENT|TTF_ABSOLUTE;
m_ti.hwnd = m_hWnd;
m_ti.hinst = NULL;
m_ti.uId = (UINT)1;
m_ti.lpszText = "";;
m_ti.rect=CRect(0,0,0,0);
m_ctrlToolTip.SetMaxTipWidth(SHRT_MAX);
m_ctrlToolTip.SetDelayTime(TTDT_AUTOPOP,5000);
m_ctrlToolTip.Activate(TRUE);
void CLadIOView::OnMouseMove(UINT nFlags, CPoint point)
{
static CPoint prevPoint =0;
static CLadRemoteIOModule* pLastIO=NULL;
CLadRemoteIOModule* pLastIO1=pLastIO;
pLastIO=NULL;
bool bToolTipSet = false;
// Go thru each module already added
POSITION pos = gobjEztouchApp.m_objLadderLogic.m_objSysAttr.m_objRemoteIOModuleLst.GetHeadPosition();
for( ; pos != NULL; )
{
CLadRemoteIOModule* pIO = gobjEztouchApp.m_objLadderLogic.m_objSysAttr.m_objRemoteIOModuleLst.GetNext(pos);
// Get the rectangle for the module
CRect rectModule = GetIOModuleRect(pIO->m_nModulePosition);
if(!rectModule.PtInRect(pt)) continue;
pLastIO=pIO;
if(pLastIO1==pIO) break;
if(!m_bMouseDown && !m_bPlacingANewModule && prevPoint != pt)
{
CString sDescription, sPartNumber, sAddressRange;
GetIOModuleText2(pIO,sDescription, sPartNumber, sAddressRange);
sPartNumber.Remove('[');
sPartNumber.Remove(']');
CString sModuleDetails;
sModuleDetails.Format(_T("Position: M%d\nModule Type: %s\nModule Part No: %s"), pIO->m_nModulePosition+1, sDescription,sPartNumber);
if(pIO->GetIPSize() > 0)
sModuleDetails+=_T("\nInput Address: ")+ pIO->GetInputAdr()+_T(" - ")+pIO->GetEndInputAdr();
if(pIO->GetOPSize() > 0)
sModuleDetails+=_T("\nOutput Address: ")+ pIO->GetOutputAdr()+_T(" - ")+pIO->GetEndOutputAdr();
CPoint pp = pt-GetScrollPosition();
ClientToScreen(&pp);
m_ctrlToolTip.UpdateTipText(sModuleDetails,this,1);
m_ctrlToolTip.SendMessage(TTM_TRACKPOSITION, 0, (LPARAM)MAKELPARAM(pp.x, pp.y+16));//+16 to move the tooltip stem down
m_ctrlToolTip.SendMessage(TTM_TRACKACTIVATE, TRUE, (LPARAM)&m_ti);
//to track mouse leave abd there we can remove tooltip
TRACKMOUSEEVENT tk;
tk.cbSize = sizeof(tk);
tk.dwFlags = TME_LEAVE;
tk.hwndTrack = m_hWnd;
_TrackMouseEvent(&tk);
prevPoint = pt;
bToolTipSet = true;
}
bToolTipSet = true;
break;
}
if(!bToolTipSet)
{
m_ctrlToolTip.SendMessage(TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ti);
m_ctrlToolTip.UpdateTipText("",this,1);
m_ctrlToolTip.Pop();
}
The tooltip remains visible for very few seconds and then they disappear. I find this time too short to read some of the longer tooltips. Is there a way to increase the time they remain visible?
I tried to increase the time using setdelaytime function but it doesn't help me.
Thanks in advance.

Doc ptr always coming NULL in MDI application

I am working on an MDI application where I have 2 views. Here is code to add views and document:
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_STRING_FILTERWINDOW,
RUNTIME_CLASS(CEmuDiagnosticsClientDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CFilterWindow));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
//adding another template
CMultiDocTemplate* pDocTemplate1;
pDocTemplate1 = new CMultiDocTemplate(IDR_STRING_SIGNALWINDOW,
RUNTIME_CLASS(CEmuDiagnosticsClientDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CSignalWindow));
if (!pDocTemplate1)
return FALSE;
AddDocTemplate(pDocTemplate1);
I have 2 views and 1 Document.
My requirement is to update CSignalWindow view based on some notification. When I run this application, I have opened a CSignalWindow view and to update this I have written following piece of code:
for( POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition(); pos != NULL; )
{
pTempl = AfxGetApp()->GetNextDocTemplate(pos );
for( POSITION pos1 = pTempl->GetFirstDocPosition(); pos!= NULL; )
{
if (pos1 == NULL)
break;
CDocument* pDoc = pTempl->GetNextDoc( pos1 );
for( POSITION pos2 = pDoc->GetFirstViewPosition(); pos2 != NULL; )
{
CView* pView = pDoc->GetNextView( pos2 );
if( pView->IsKindOf( RUNTIME_CLASS(CSignalWindow) ) )
{
pView->UpdateData(true);
}
}
}
}
But pos1 is always coming NULL at this line:
for( POSITION pos1 = pTempl->GetFirstDocPosition(); pos!= NULL; )
I am unable to understand why it is returning NULL always?
There is copy-paste-error in this line:
POSITION pos1 = pTempl->GetFirstDocPosition(); pos!= NULL; )
It should be:
POSITION pos1 = pTempl->GetFirstDocPosition(); pos1!= NULL; )

Retrieving doc templates in MFC

I am using MFC MDI application and i am adding two doc templates as shown in code below:
BOOL CEmuDiagnosticsClientApp::InitInstance()
{
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinAppEx::InitInstance();
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
//Added new code
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_STRING_FILTERWINDOW,
RUNTIME_CLASS(CEmuDiagnosticsClientDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CFilterWindow));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(4); // Load standard INI file options (including MRU)
InitContextMenuManager();
InitKeyboardManager();
InitTooltipManager();
CMFCToolTipInfo ttParams;
ttParams.m_bVislManagerTheme = TRUE;
theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);
AddDocTemplate(new CMultiDocTemplate(IDR_STRING_SIGNALWINDOW,
RUNTIME_CLASS(CEmuDiagnosticsClientDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CSignalWindow)));
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
{
delete pMainFrame;
return FALSE;
}
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line. Will return FALSE if
// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The main window has been initialized, so show and update it
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
return TRUE;
}
I am using this code to iterate over documents and get view classes's reference in my CMainFrame class.
CFilterWindow* p = NULL;
CSignalWindow* p1 = NULL;
for( POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition(); pos != NULL; )
{
CDocTemplate* pTempl = AfxGetApp()->GetNextDocTemplate( pos );
for( POSITION pos1 = pTempl->GetFirstDocPosition(); pos!= NULL; )
{
CDocument* pDoc = pTempl->GetNextDoc( pos1 );
for( POSITION pos2 = pDoc->GetFirstViewPosition(); pos2 != NULL; )
{
CView* pView = pDoc->GetNextView( pos2 );
if( pView->IsKindOf( RUNTIME_CLASS(CFilterWindow) ) )
{
p = (CFilterWindow*)pView;
p->UpdateUI();
// Do what you need with the view...
}
else
{
p1 = (CSignalWindow*)pView;
p1->UpdateUI();
}
}
}
}
Problem is with loop for( POSITION pos2 = pDoc->GetFirstViewPosition(); pos2 != NULL; )
as pDoc->GetFirstViewPosition(); always returning NULL.
It is puzzling me why it is returning NULL.
Can anybody suggest where I am wrong?

In MDI mode, Dynamic change menu caption which without ID

The old question, is for menu on dialog, to change the menu caption which no id
How do I dynamic change MENU text which without ID
the code is
////////////////////////////////////////////////////////
//CMenu* pMenu = CMenu::FromHandle(GetSkinMenu(m_pMainWnd->m_hWnd))->GetSubMenu(1);
HMENU hMenu;
hMenu=LoadMenu(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MENU_S8521));
//CMenu* pMenu = CMenu::FromHandle(hMenu);
CMenu* pMenu = GetMenu();
int i, nCou = pMenu->GetMenuItemCount();
UINT uID;
CString strMenu;
for (i = 0; i < nCou; i++)
{
uID = pMenu->GetMenuItemID(i);
if (uID == 0) // separator
{
//TRACE(_T("----------------------\n"));
continue;
}
//pMenu->GetMenuString(i, ss, MF_BYPOSITION);
if (uID == (UINT)-1)
{
//TRACE(_T("Popup '%s' "), ss);
if(i == 0)
{
strMenu = theApp.mLang.structMenuLang.strMenuFile;
}
if(i == 1)
{
strMenu = theApp.mLang.structMenuLang.strMenuSet;
}
if(i == 2)
{
strMenu = theApp.mLang.structMenuLang.strMenuLanguage;
}
if(i == 3)
{
strMenu = theApp.mLang.structMenuLang.strMenuHelp;
}
pMenu->ModifyMenu(i, MF_BYPOSITION|MF_STRING|MF_ENABLED, 0, strMenu);
//TRACE(_T("modified to '%s' "), ss);
}
//else
// TRACE(_T("Item '%s', ID=%d "), ss, uID);
//TRACE(_T("\n"));
}
//Invalidate(true);
//UpdateWindow();
this->SetMenu(pMenu);
Now I want to change the MID menu caption which also no id,
the keyword code is
this->SetMenu(pMenu);
then the menu will reflash. My question is On MDI Menu, how do I reflash the menu caption
, In out command window i find the TRACE message is right, but the UI no change.
ok, I get method to solve it using
CMenu menu;
menu.LoadMenu(IDR_MENU_MAIN);
AfxGetMainWnd()->SetMenu(pMenu);
AfxGetMainWnd()->DrawMenuBar();
pMenu->Detach();

Is there a way to use normal ASCII characters (like a comma) as wxWidgets menu accelerators?

I want a few menu entries that show accelerators that are normal keys, like the space-bar or comma key, but I don't want wxWidgets to make those accelerators itself (because then they can't be used anywhere in the program, including in things like edit boxes).
Unfortunately, wxWidgets insists on always making anything it recognizes in that column into an accelerator under its control, and simply erases anything it doesn't recognize.
I'm looking for some way to either put arbitrary text into the accelerator column (which I don't think exists, I've looked at the source code), or get 'hold of the accelerator table used for the menus so I can modify it myself (haven't found it yet). Can anyone point me in the right direction?
You can try wxKeyBinder. It allows you to bind hotkeys to commands (usually menu entries), save/load/add/remove/modify ... them easily
I couldn't find a way to access the menu's accelerator keys directly, but modifying the accelerator menu text works just as well. Here's the code I came up with:
In a header file:
class accel_t {
public:
// If idcount == -1, idlist must be null or terminated with a -1 entry.
accel_t(): mMenu(0) { }
accel_t(wxMenuBar *m, int *idlist = 0, int idcount = -1);
void reset(wxMenuBar *m, int *idlist = 0, int idcount = -1);
void restore() const;
void remove() const;
private: //
struct accelitem_t {
accelitem_t(int _id, wxAcceleratorEntry _key): id(_id), hotkey(_key) { }
int id;
wxAcceleratorEntry hotkey;
};
typedef std::vector<accelitem_t> data_t;
void noteProblemMenuItems(wxMenu *m);
static bool isProblemAccelerator(wxAcceleratorEntry *a);
wxMenuBar *mMenu;
data_t mData;
};
In a cpp file:
accel_t::accel_t(wxMenuBar *m, int *idlist, int idcount) {
reset(m, idlist, idcount);
}
void accel_t::reset(wxMenuBar *m, int *idlist, int idcount) {
mMenu = m;
mData.clear();
if (idlist == 0) {
for (int i = 0, ie = m->GetMenuCount(); i != ie; ++i)
noteProblemMenuItems(m->GetMenu(i));
} else {
if (idcount < 0) {
int *i = idlist;
while (*i != -1) ++i;
idcount = (i - idlist);
}
for (int *i = idlist, *ie = i + idcount; i != ie; ++i) {
wxMenuItem *item = mMenu->FindItem(*i);
if (item) {
wxAcceleratorEntry *a = item->GetAccel();
if (a != 0) mData.push_back(accelitem_t(*i, *a));
}
}
}
}
bool accel_t::isProblemAccelerator(wxAcceleratorEntry *a) {
if (a == 0) return false;
int flags = a->GetFlags(), keycode = a->GetKeyCode();
// Normal ASCII characters, when used with no modifier or Shift-only, would
// interfere with editing.
if ((flags == wxACCEL_NORMAL || flags == wxACCEL_SHIFT) &&
(keycode >= 32 && keycode < 127)) return true;
// Certain other values, when used as normal accelerators, could cause
// problems too.
if (flags == wxACCEL_NORMAL) {
if (keycode == WXK_RETURN ||
keycode == WXK_DELETE ||
keycode == WXK_BACK) return true;
}
return false;
}
void accel_t::noteProblemMenuItems(wxMenu *m) {
// Problem menu items have hotkeys that are ASCII characters with normal or
// shift-only modifiers.
for (size_t i = 0, ie = m->GetMenuItemCount(); i != ie; ++i) {
wxMenuItem *item = m->FindItemByPosition(i);
if (item->IsSubMenu())
noteProblemMenuItems(item->GetSubMenu());
else {
wxAcceleratorEntry *a = item->GetAccel();
if (isProblemAccelerator(a))
mData.push_back(accelitem_t(item->GetId(), *a));
}
}
}
void accel_t::restore() const {
if (mMenu == 0) return;
for (data_t::const_iterator i = mData.begin(), ie = mData.end(); i != ie;
++i)
{
wxMenuItem *item = mMenu->FindItem(i->id);
if (item) {
wxString text = item->GetItemLabel().BeforeFirst(wxT('\t'));
wxString hotkey = i->hotkey.ToString();
if (hotkey.empty()) {
// The wxWidgets authors apparently don't expect ASCII
// characters to be used for accelerators, because
// wxAcceleratorEntry::ToString just returns an empty string for
// them. This code deals with that.
int flags = i->hotkey.GetFlags(), key = i->hotkey.GetKeyCode();
if (flags == wxACCEL_SHIFT) hotkey = wx("Shift-") + wxChar(key);
else hotkey = wxChar(key);
}
item->SetItemLabel(text + '\t' + hotkey);
}
}
}
void accel_t::remove() const {
if (mMenu == 0) return;
for (data_t::const_iterator i = mData.begin(), ie = mData.end(); i != ie;
++i)
{
wxMenuItem *item = mMenu->FindItem(i->id);
if (item) {
wxString text = item->GetItemLabel().BeforeFirst(wxT('\t'));
item->SetItemLabel(text);
}
}
}
The easiest way to use it is to create your menu-bar as normal, with all accelerator keys (including the problematic ones) in place, then create an accel_t item from it something like this:
// mProblemAccelerators is an accel_t item in the private part of my frame class.
// This code is in the frame class's constructor.
wxMenuBar *menubar = _createMenuBar();
SetMenuBar(menubar);
mProblemAccelerators.reset(menubar);
It will identify and record the accelerator keys that pose problems. Finally, call the remove and restore functions as needed, to remove or restore the problematic accelerator keys. I'm calling them via messages passed to the frame whenever I open a window that needs to do standard editing.

Resources