I am trying to create a CCombobox with the following code:
CComboBox* cSearchBar = new CComboBox();
if (!cSearchBar->Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | CBS_OWNERDRAWFIXED | CBS_DROPDOWN | CBS_AUTOHSCROLL, CRect(150,10,325,15), this, IDC_COMBO))
TRACE0("Failed to create search bar\n");
But it gives an error message of "Debug Assertion Failed" on
File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\winctrl1.cpp, Line 271
This problem only occurs when I try to create the CCombobox with CBS_OWNERDRAWFIXED flag. Someone please tell me how to create a CCombobox programmatically with Owner Drawn property set to fixed.
To use ownerdraw control you have to use your own class.
class CMyComboBox : public CComboBox
{
public:
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
void MeasureItem(LPMEASUREITEMSTRUCT ms);//ms->itemHeight = 15...
};
Example:
https://msdn.microsoft.com/en-us/library/y5hb5f9t.aspx
Or you can use a regular combobox and just change its font. Declare font as class member. Create the font and call SetFont after ComboBox is created:
{
CFont m_font;
//...
}
cSearchBar->Create...
m_font.CreatePointFont(120, L"Segoe UI");
cSearchBar->SetFont(&m_font);
Related
I'm trying to do exactly the same as the article describes here:
"C++ MFC Feature Pack --> Create multiple CDockablePanes onto an CDialog"
I followed his procedure and now am able to undock and move the CDockablePane, but get the same crash when dock it back. In his own answer, he said he created the dummywnd by himself so MFC skipped the creation and the call to GetTopLevelFrame(). And this is where I got confused, how do I create the dummywnd exactly?
My second question is how do I exchange data between the CMyFrame and CDialog?
The person who asked and answered the question seems to be inactive for years and unreachable. Could anyone please help or have any ideas?
Thanks,
Edit:
I break the program and traced back exactly as the other author described. The dummy window mentioned above is actually in afxdragframeimpl.cpp:
void CMFCDragFrameImpl::MoveDragFrame(BOOL bForceMove)
where it creates:
m_pWndDummy = new CDummyDockablePane;
and calls:
m_pWndDummy->CreateEx(0, _T(""), AFXGetTopLevelFrame(m_pDraggedWnd), CRect(0, 0, 0, 0), FALSE, AFX_DUMMY_WND_ID, WS_CHILD);
And yes, I'm trying to create a CFrameWndEx as child window in my dialog, and then add child CDockablePane in that CFrameWndEx.
Basically I have a MFC dialogA with some controls, and within this dialogA I need some tear-off tabsXYZ, and I need to add some controls within each of the tear-off tabsXYZ. So this means each tear-off tabsXYZ is actually one child dialogB. So this comes to where I try to use CDockablePanes (CPaneDialog actually) within the dialogA.
BOOL CMyDlg::OnInitDialog()
{
CRect wndRect;
GetWindowRect(wndRect);
m_pFrame = new CMyFrame();
m_pFrame->Create(NULL, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, wndRect, this);
m_pFrame->MoveWindow(wndRect);
CDialog::OnInitDialog();
...
}
I don't recommend the above code where frame window is placed in a dialog, because CFrameWndEx does all sorts of strange things, it's easy to break this code. Surprisingly, it works fine on VS2015, I couldn't duplicate any crash. But the window's behavior is still odd.
It's better to make a new frame window, and place a child dialog in frame. For example:
class CMyFrame : public CFrameWndEx
{
CDialog m_dialog;
int OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CFrameWndEx::OnCreate(lpCreateStruct);
m_dialog.Create(IDD_CHILD1, this);
CRect rc;
m_dialog.GetClientRect(&rc);
m_dialog.SetWindowPos(NULL, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);
return 1;
}
DECLARE_MESSAGE_MAP()
};
You open the window as follows:
void CMyMainFrame::OnButton()
{
CMyFrame *frame = new CMyFrame;
frame->LoadFrame(IDR_MAINFRAME,
WS_POPUPWINDOW | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU, this);
frame->SetMenu(0);
frame->ShowWindow(SW_SHOW);
}
You can also create a child dialog and put that inside a docking pane. For example:
class CMyFrame : public CFrameWndEx
{
CDockablePane m_DockWnd;
CDialog m_dialog;
int OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CFrameWndEx::OnCreate(lpCreateStruct);
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
CDockingManager::SetDockingMode(DT_SMART);
EnableAutoHidePanes(CBRS_ALIGN_ANY);
m_DockWnd.Create(_T("Test"), this, CRect(0, 0, 200, 200), TRUE, 0,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
CBRS_LEFT | CBRS_FLOAT_MULTI);
m_dialog.Create(IDD_PAGE1, &m_DockWnd);
CRect rdialog;
m_dialog.GetClientRect(&rdialog);
m_dialog.SetWindowPos(NULL, 0, 0, rdialog.Width(), rdialog.Height(), SWP_SHOWWINDOW);
m_DockWnd.SetMinSize(rdialog.Size());
m_DockWnd.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockPane(&m_DockWnd);
return 0;
}
...
};
Also remember, if your main window is also CFrameWndEx then you make something like the following calls in InitInstance:
SetRegistryKey(_T("MyCompany\\MyApp"));
SetRegistryBase(_T("MainFrame"));
When you open a new frame window you must change the registry base with
SetRegistryBase(_T("CMyFrame"));
Then change it back to SetRegistryBase(_T("MainFrame")) when you exit CMyFrame
I've got a situation.
There is a huge app (C++ MFC). I write a .dll module with a dockable pane.
Pane interface structure:
Pane -> CMFCToolBar
-> CSplitterWndEx -> CListCtrl
-> CDialogEx
That's how i create my DialogEx:
int CMyPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
///////////////////////////////////////////
////////// TAB CTRL ///////////////////////
///////////////////////////////////////////
const int dwResTabCtrlStyle = WS_CHILD | WS_VISIBLE | TCS_VERTICAL;// | LVS_SINGLESEL;
if(!m_SptitterWndEx.AddTabCtrl(0, 1, &m_tabCtrl, CMFCTabCtrl::STYLE_3D, CMFCBaseTabCtrl::LOCATION_TOP, CSize(10,10)))
return -1;
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
m_DialogEx.Create(CAccuracyResultPage::IDD, NULL);
}
m_DialogEx.SetParent(&m_tabCtrl);
if(!m_DialogEx.GetParent())
return -1;
str.LoadString( AfxGetStaticModuleState()->m_hCurrentResourceHandle, IDS_RESULT_TAB);
m_tabCtrl.AddTab(&m_DialogEx, str, 0);
AdjustLayout();
return 0;
}
I getting assert on CDialogEx::PreTranslateMessage. The reason is the when it gets it's parents
_AFXWIN_INLINE CWnd* CWnd::GetParent() const
{ ASSERT(::IsWindow(m_hWnd)); return CWnd::FromHandle(::GetParent(m_hWnd)); }
m_hWnd is not a Wnd. But the CDialog looks perfectly OK, it has m_pParentWnd, but it's not m_tabCtrl.
So my questions are: Why doesn't CDialogEx set its parent?! And how it can be dealed with?!
Where is your dialog template residing? Is it in the same dll? If not, then i think that's your problem.
My guess is, if your dialog template resides in a different dll then windows might search in the HWND=>CWnd map of the module state of that dll. If that happens then it wont be able to find the CWnd in the map and will create a temporary CWnd object and set it as the parent of the dialog.
I created a multi doc ribbon based MFC application through the MFC Wizard. Im trying to get a handle to m_wndFileView to update its view. I know there are several ways to do it but Im not understanding why the method Im using is not working. Soooo to start
class CMainFrame : public CMDIFrameWndEx
{
...
CFileView m_wndFileView;
CPropertiesWnd m_wndProperties;
...
}
class CFileView : public CDockablePane
{
...
protected:
CViewTree m_wndFileView;
...
};
class CPropertiesWnd : public CDockablePane
{
...
protected:
CMFCPropertyGridCtrl m_wndPropList;
...
};
The main frame is created from MAINAPPLICATION.cpp
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
{
delete pMainFrame;
return FALSE;
}
m_pMainWnd = pMainFrame;
// call DragAcceptFiles only if there's a suffix
// In an MDI app, this should occur immediately after setting m_pMainWnd
// Enable drag/drop open
m_pMainWnd->DragAcceptFiles();
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Enable DDE Execute open
EnableShellOpen();
RegisterShellFileTypes(TRUE);
// 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();
MainFrm.cpp creates these two panes:
// Create file view
CString strFileView;
bNameValid = strFileView.LoadString(IDS_FILE_VIEW);
ASSERT(bNameValid);
if (!m_wndFileView.Create(strFileView, this, CRect(0, 0, 200, 200), TRUE, ID_VIEW_FILEVIEW, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT| CBRS_FLOAT_MULTI))
{
TRACE0("Failed to create File View window\n");
return FALSE; // failed to create
}
// Create properties window
CString strPropertiesWnd;
bNameValid = strPropertiesWnd.LoadString(IDS_PROPERTIES_WND);
ASSERT(bNameValid);
if (!m_wndProperties.Create(strPropertiesWnd, this, CRect(0, 0, 200, 200), TRUE, ID_VIEW_PROPERTIESWND, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT | CBRS_FLOAT_MULTI))
{
TRACE0("Failed to create Properties window\n");
return FALSE; // failed to create
}
From MAINAPPLICATION.cpp, I can access the properties pane through
CWnd * pwnd = ((CWnd*)(AfxGetApp()->m_pMainWnd));
CPropertiesWnd * pPropertiesWnd = (CPropertiesWnd*)pwnd->GetDlgItem(ID_VIEW_PROPERTIESWND);
CMFCPropertyGridCtrl * m_wndPropList = (CMFCPropertyGridCtrl *)pPropertiesWnd->GetDlgItem(2);
but for some reason I cannot access the fileview pane with
CWnd * pwnd = ((CWnd*)(AfxGetApp()->m_pMainWnd));
CFileView * pFileViewWnd = (CFileView*)pwnd->GetDlgItem(ID_VIEW_FILEVIEW);
CViewTree * m_wndFileView= (CViewTree*)pFileViewWnd ->GetDlgItem(4);
the (CFileView*)pwnd->GetDlgItem(ID_VIEW_FILEVIEW); returns NULL
please help. This is driving me crazy. In the end I can modify the m_wndPropList but not the m_wndFileView because I cannot get a handle to pFileViewWnd. Two panes created in the same way cannot be accessed in the same way. Why? If more code is needed, let me know. Thanks.
You should create a inline getters in CMainFrame class like this:
CFileView& GetFileViewPane()
{
return m_wndFileView;
}
CPropertiesWnd& GetPropsPane()
{
return m_wndProperties;
}
After that you can access those windows just like this:
CMainFrame* pMainFrame = DYNAMIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
if (pMainFrame && pMainFrame->GetSafeHwnd()) // sanity check
{
pMainFrame->GetFileViewPane().DoStuff();
}
So #MarkRansom really helped out with the spy++ idea. To get a handle, I had to do the following:
// get CWnd to main window
CWnd * pwnd = ((CWnd*)(AfxGetApp()->m_pMainWnd));
// from spy++ i found that the tabbed panes were in a window called
// "File View" so i found a child window with that title
HWND h = FindWindowExW(pwnd->GetSafeHwnd(), NULL, NULL, L"File View");
// casted it to a tabbedpane pointer
CTabbedPane * pFileViewWn = (CTabbedPane *)CWnd::FromHandle(h);
// mfc wizard did what seems to me as weird naming. to find the docking
// panes i did the same as above but there was no title to this window
HWND hh = FindWindowExW(pFileViewWn->GetSafeHwnd(), NULL, NULL, L"");
// casted that
CDockablePane* pTabbedBar = (CDockablePane*)CWnd::FromHandle(hh);
// was able to find my specific docking pane using a resource id
CFileView * pFileViewWnd = (CFileView*)pTabbedBar->GetDlgItem(ID_VIEW_FILEVIEW);
// was able to find the control i wanted to use using a resource id
CViewTree * m_wndFileView = (CViewTree *)pFileViewWnd->GetDlgItem(4);
HTREEITEM hRoot = m_wndFileView->GetRootItem();
m_wndFileView->InsertItem(name, 2, 2, hRoot);
Going through the code i would think that the logic would be
tabbed pane
File View Dockable Pane
File View controls
Class View Dockable Pane
Class View controls
but somehow another window slide its way in to make
tabbed pane
MYSTERY DOCKABLE WINDOW
File View Dockable Pane
File View controls
Class View Dockable Pane
Class View controls
I've making simple desktop game in mfc for school project, I've managed to make my app be full screen and to remove menu bar but I can't find out how to remove default built in toolbar from my app or status bar. I tried everything that came across my mind...is there some kind of get function to call from your CWnd object to retrieve toolbar and status bar?
The creation of ToolBar and StatusBar is inside the CMainFrame class. You can easily remove them if you do not need them as follows:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// *** creation of ToolBar starts, just remark/delete the whole block if you dont't want it
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// *** creation of ToolBar ends -------------------------------------------------------
// *** creation of StatusBar starts, just remark/delete the whole block if you dont't want it
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// *** creation of StatusBar ends -------------------------------------------------------
// *** you have to remark/delete these lines too, if you removed the ToolBar above
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
// *** ToolBar extra ends -------------------------------------------------------
return 0;
}
m_pMainWnd->SetMenu(NULL); right before m_pMainWnd->ShowWindow(SW_SHOW); is called in the APPLICATION_NAME.cpp file.
Go to your resource file, double click on it , locate the toolbar, right click on it and choose delete :)
How do I add a context menu in a list box in MFC? I don't see any WM_CONTEXTMENU handler in list box's properties. Any ideas?
EDIT: I followed this tutorial MFC List Control: How to use a context menu in a list control?. The tutorial says to derive my own class from CListBox which I did, but now how do I add list box of my derived class to the dialog?
Since my edit was rejected with the rationale of "changing too much", I will put my proposal here because in my opinion the original code promotes bad coding practices.
void CYourDialog::OnContextMenu(CWnd* pWnd, CPoint point)
{
int CtrlID = pWnd->GetDlgCtrlID();
if (CtrlID == ID_YOUR_LIST) {
CMenu menu;
// Create your menu items...
int retVal = menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, this);
// Handle selected options here...
}
}
Put an OnContextMenu handler in the parent class. Then add a popup menu
Edit To add the OnContextMenu handler, add an event handler to the PARENT window (ie not the list class). This is most easily done through the resource editor. Go to the properties page then go to the messages section. Then add a function for WM_CONTEXTMENU.
void CYourDialog::OnContextMenu(CWnd* pWnd, CPoint point)
{
CListCtrl* pList = (CListCtrl*)GetDlgItem( ID_YOUR_LIST );
if ( (CWnd*)pList == pWnd )
{
CMenu menu;
// Create your menu items.
int retVal = menu.TrackPopupMenu( TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, this );
// Handle your returns here.
}
}
You need to take the following steps:
Add
ON_WM_CONTEXTMENU()
to
BEGIN_MESSAGE_MAP()
So you will have something like
BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_QUERYDRAGICON()
ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()
Add the Context Menu function in the header file:
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point)
Then add the Context Menu function, as suggested in the article:
void CMyDialog::OnContextMenu(CWnd* pWnd, CPoint point)
{
CListCtrl* pList = (CListCtrl*)GetDlgItem( ID_YOUR_LIST );
if ( (CWnd*)pList == pWnd )
{
CMenu menu;
// Create your menu items.
int retVal = menu.TrackPopupMenu( TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, this );
// Handle your returns here.
}
}
Add a handler for your dialog window. That will generate this:
void YourDialogClass::OnContextMenu(CWnd* pWnd, CPoint point) {
...
}
pWnd will point to the window/control in which the user right clicked the mouse.
If you followed the tutorial to derive you own class, make sure ON_WM_CONTEXTMENU() is added to the new class message map.
To add a list box of your derived class, you simply add a variable for your ListBox control and specify the variable class as your derived class.
However I think #Goz's answer is also a valid solution, and a simpler one.