Float (or detach) the tabbed CMDIChildWndEx window from CMDIFrameWndEx,any idea? - c++

In visual studio,all opened editor windows are tabbed in the workspace.when you right click one,there is a "float" option in the menu.if you float a window,the window is detached from the tab and you can drag it anywhere and after all you can dock it back to the tab.
i created a test mdi project,it's not a default behavior.
i overrided the ID_FILE_NEW command and followed the OpenDocumentFile routine,never found out where the framework add the newly created child frame window to the tab.
in the OpenDocumentFile routine,i bumped into a CMFCTabCtrl class and there is a AddTab function.
now the AddTab function:
virtual void AddTab(
CWnd* pTabWnd,
LPCTSTR lpszTabLabel,
UINT uiImageId = (UINT)-1,,
BOOL bDetachable = TRUE
);
virtual void AddTab(
CWnd* pTabWnd,
UINT uiResTabLabel,
UINT uiImageId = (UINT)-1,
BOOL bDetachable = TRUE
);
the Remarks:
If pTabWnd points to an object that is not derived from the CDockablePane Class and
if bDetachable is TRUE, the framework automatically creates a wrapper for the pTabWnd object.
The wrapper makes the pTabWnd object detachable.
By default, the wrapper is an instance of the CDockablePaneAdapter Class.
If the functionality offered by the default wrapper is unacceptable,
use the CMFCBaseTabCtrl::SetDockingBarWrapperRTC method to specify a different wrapper.
still no luck.
i did try to on the fly create a CView and attach it to the CDocument and put that CView in a CDockablePane.so i could switch the old tabbed one to the new dockable one. it's not working yet.
So any thoughts about floating the tabbed CMDIChildWndEx window from CMDIFrameWndEx?

Related

Getting a debug assertion error when calling GetDC() (MFC)

I'm making a MFC application using the Doc/View architecture with Visual Studio 2017, and for some reason I get that error whenever I call GetDC() inside this function:
void CDigitRecognizerView::ClearScreen(void)
{
CDC* dc;
dc = GetDC(); // debug assertion error here
CBrush brush;
brush.CreateSolidBrush(0xFFFFFF);
dc->SelectObject(&brush);
CRect rect;
GetWindowRect(&rect);
dc->FillRect(&rect, &brush);
CDigitRecognizerDoc* pDocument = GetDocument();
ReleaseDC(dc);
}
This is the message map macro defined in the app class:
BEGIN_MESSAGE_MAP(CDigitRecognizerApp, CWinApp)
ON_COMMAND(ID_APP_ABOUT, &CDigitRecognizerApp::OnAppAbout)
ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
ON_COMMAND(ID_EDIT_CLEARSCREEN, CDigitRecognizerView::ClearScreen)
END_MESSAGE_MAP()
So whenever I select the "Clear Screen" option from the menu of the app, ClearScreen() gets called but I can't get the DC of the View, it crashes.
I have looked at the variables in the debugger and the window handle seems OK so I don't know really.
I am also wondering what other way I could call a function of the View class from the App class whenever I select a menu option because this doesn't seem to work.
How did you get the CWinApp message-map pointing to a CDigitRecognizerView function? I think the "wizard" wouldn't do this. Did you add the handler manually?
As for accessing Doc/View instances from the CWinApp class, there are some functions available:
GetFirstDocTemplatePosition() / GetNextDocTemplate(), members of CWinApp class. Alternatively you can simply store the pDocTemplate instance created in the InitInstance() function. Then call:
GetFirstDocPosition() / GetNextDoc(), members of the CDocTemplate class, and finally:
GetFirstViewPosition() / GenNextView(), members of the CDocument class
But this is normally not needed (the events could be handled in the Doc/View classes), unless you want to perform some operation(s) on all (or some of the) DocTemplate/Doc/View instances (which rather implies that you are developing an MDI application).

CDHtmlDialog - make it modal?

I am popping up a CDHtmlDialog box when user clicks inside a another dialog box. Here is the pseudo code
CMyClass{
CDHtmlDialog * m_htmlDialog;
}
CMyClass::OnInitDialog(){
m_htmlDialog = new CDHtmlDialog(IDD_DIALOG_EMAIL, IDR_HTML_SUBMIT_EMAIL);
}
CMyClass::OnBnClickSendEmail{
m_htmlDialog->Create(IDD_DIALOG_EMAIL);
//m_htmlDialog->DoModal();
m_htmlDialog->ShowWindow(SW_SHOWNORMAL);
}
The code runs fine when the DoModal is commented. But the problem is that the Html Dialog is not modal and I can click on the background forms and dialogs. I want the Html dialog to be modal and when I uncomment the DoModal() line the code crashes.
This is how I display a modal dialog, when it begins as a null pointer:
auto *pDlgEditor = new CSomeDlg(this);
if (pDlgEditor != nullptr)
{
pDlgEditor->DoModal();
delete pDlgEditor;
}
Notice that I am passing this which is the owner for the window.
The actual class itself specifies the dialogue resource:
CSomeDlg::CChristianLifeMinistryEditorDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_DIALOG_EXAMPLE, pParent))
I realise that my code doesn't show a CDHtmlDialog ... According to the documentation there are 3 constructors:
CDHtmlDialog();
CDHtmlDialog(
LPCTSTR lpszTemplateName,
LPCTSTR szHtmlResID,
CWnd *pParentWnd = NULL);
CDHtmlDialog(
UINT nIDTemplate,
UINT nHtmlResID = 0,
CWnd *pParentWnd = NULL);
The third parameter is the parent which defaults to NULL. Try passing this as the third parameter.
Now, if you are inside a popup modaless window when you do the above the parent will be the modaless window. But if you pass the modaless windows' parent then that will become the owner instead. We don't have full information so the above is just generic advice.
Note that the documentation states that if you leave the pParentWnd as the default (NULL):
If it is NULL, the dialog object's parent window is set to the main application window.
So, it might not necessarily use the parent you expect, which is why it is good to specify it yourself.

CWindowImpl - Create() returns error code 1406

I just tried to setup a small win32 project, and was just about to create a window.
I created a dialog in the resource designer and set up this class for my window:
#pragma once
#include "stdafx.h"
class TTTMainDialog : public CWindowImpl<TTTMainDialog>
{
public:
DECLARE_WND_CLASS(_T("TTTDlg"))
enum { IDD = IDD_TTT_DIALOG };
BEGIN_MSG_MAP_EX(MusicPlayerDialog)
MSG_WM_INITDIALOG(OnInitDialog);
MSG_WM_CLOSE(OnClose);
MSG_WM_DESTROY(OnDestroy);
END_MSG_MAP()
TTTMainDialog();
~TTTMainDialog();
private:
const BOOL OnInitDialog(const CWindow wndFocus, const LPARAM lInitParam);
void OnClose();
void OnDestroy();
};
As you can see, I added the window class declaration, I inherited CWindowImpl, I registered the dialog. I don't think I forgot something here.
In the class which is supposed to create the dialog, I tried to create it like this:
TTTMainDialog myDialog;
HWND handle = myDialog.Create(NULL);
myDialog.ShowWindow(nCmdShow);
However, the Create method does return NULL all the time. I checked the error code with GetLastError(), and it turns out i am getting error code 1406, or "ERROR_TLW_WITH_WSCHILD".
The msdn documentation says the following about this error:
"Cannot create a top-level child window."
I tried to google up on this, but there is not much to find.
If I had to take a guess I would say the problem is caused by some window class name details, but i'm really not sure.
Any advice?
You are trying to build a window class from wrong pieces.
The error is pretty descriptive: you are trying to create a parentless window with a WS_CHILD style and this does not work out.
You get the child style from default template parameter: CWindowImpl -> CWindowImplBaseT -> TWinTraits -> CControlWinTraits. CControlWinTraits is supposed for use with child control windows.
If you are going to use a dialog template (IDD_TTT_DIALOG) then the proper base class is CDialogImpl, which is already prepared to use proper window styles. Also, it has what it takes to create both modal and modeless dialogs. The latter act more like windows and are non-blocking but in the same time take dialog template resource with predefined controls.
Small example for CDialogImpl use
Or, another one

is there a function in dlg class as getdocument()?

I want to get a doc* in dlg class, and i know in view class we can get doc* like
doc* pdc=getdocument();
But how can i do it in dlg class?
There is no function in CDialog to retrieve a document pointer. To give your dialog access to the document, you can add a pointer or reference to the document as a member variable of the dialog class, and initialise it in the dialog's constructor.
CMyDocument* doc = GetDocument();
CMyDialog dialog(doc);
dialog.DoModal();
MFC's CDialog class does not have built-in CDocument's. You can implement a CFormView that is derived from CView (which are part of the Document/View architecture of MFC), but CFormView is not a dialog. However, CFormView's can hold controls like a dialog - you can actually assign a dialog template to a CFormView.
you can get document from dialog by doing as follows:
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
CMyProjDoc* pDoc = (CMyProjDoc*)(pFrame->GetActiveDocument());
pDoc->m_item[i].name // use

How do I create and use a CFormView in an MFC regular DLL? (visual studio 2008)

I recently asked this question which got me started in the right direction - at least for loading the MFC DLL and trying to show a dlg box.
The problem is, the typical dialog box is horrible as a main window for an APP. It is quite simple for me to create a new exe project to do what I want, but the problem is that I have a DLL and the tools just don't seem to allow me to hook up the classes to the windows forms in the resource editor. Thus I can't seem to handle the events that I need.
My questions:
How do I create and display a CFormView (based on an IDD_FORMVIEW I created in a resource editor) in an MFC DLL project?
How do I get the form to show and to process input?
How do I add event/message handlers for that window? (The menu item to do that from the .rc editor is greyed out)
How do I set a menu to the formview? (the properties for the resource in the editor do not let me associate it with a menu resource. (I can't figure out why)
The links I have been looking at are pretty light and ambiguous about how to do it. Most of them assume I can create a mainframe as an MFC single document app via the "wizard" - which is not the case.
Right now I call Create() on the window class I made and pass in the CWnd of the desktop as the parent.
I am not sure I have subclassed the CFormView Correctly. In fact, I am pretty sure I have done little of what I need to do, though I tried to follow the instructions I have seen.
I then call ShowWindow(SW_SHOW), but I still see nothing.
I think this SHOULD be simple. All I want to do is show the form I created in the form editor.
How do I do that and what is the simplest way?
Here is some code - the cpp code that calls it
MainForm *mf = new MainForm();
mf->Create(CWnd::GetDesktopWindow());
mf->ShowWindow(SW_SHOW);
Here is the .h file for the MainForm class
#include "afxcmn.h"
// MainForm form view
class MainForm : public CFormView
{
DECLARE_DYNCREATE(MainForm)
public:
MainForm();
virtual ~MainForm();
public:
virtual BOOL Create(CWnd* pParent);
public:
enum { IDD = IDD_FORMVIEW_MAIN };
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
CListCtrl m_SymbolSetList;
};
and here is the cpp for MainForm
#include "stdafx.h"
#include "MainForm.h"
// MainForm
IMPLEMENT_DYNCREATE(MainForm, CFormView)
MainForm::MainForm()
: CFormView(MainForm::IDD)
{
}
MainForm::~MainForm()
{
}
void MainForm::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_SYMBOLSETS, m_SymbolSetList);
}
BEGIN_MESSAGE_MAP(MainForm, CFormView)
END_MESSAGE_MAP()
// MainForm diagnostics
#ifdef _DEBUG
void MainForm::AssertValid() const
{
CFormView::AssertValid();
}
#ifndef _WIN32_WCE
void MainForm::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif
#endif //_DEBUG
BOOL MainForm::Create(CWnd* pParent)
{
CRect rect;
//pParent->GetClientRect(rect);
return CFormView::Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, pParent, 0, NULL);
}
I would investigate creating and showing your window in a separate MFC UI thread. MFC has got its own mechanism for delivering Windows messages to the CWnd dervived objects called message pump and it needs to initialize its internal structures for it to work. I think you need to use framework function call to do it. Try this version of AfxBeginThread:
CWinThread* AfxBeginThread(
CRuntimeClass* pThreadClass,
int nPriority=HREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);
and pass your form as a pThreadClass; there is RUNTIME_CLASS macro that will do it for you. You then end the thread from withing the frame code.
As for the message handling, try opening properties for your form in the resource editor and click the Messages button on the top. You can than add your handlers to the messages that you need to handle.
I was able to associate the Menu property with a resource ID of a menu. I am not sure why you are not able to do so. The resource editor might get confused sometimes if you select a different resource in the tree view but your main window displays a different resource.
Regards