I am having a tree control where initially I had set it style in OnInitDialog as follows,
BOOL OnInitDialog()
{
CPropertyPage::OnInitDialog();
//Setting Treecontrol with TVS_CHECKBOXES style
HWND m_hTreeWnd = ::GetDlgItem(m_hWnd,IDC_TREE);
DWORD dwStyle = GetWindowLong(m_hTreeWnd,GWL_STYLE);
dwStyle |= (TVS_CHECKBOXES);
SetWindowLongPtr(m_hTreeWnd,GWL_STYLE,dwStyle);//CTreeCtrl m_hTreeWnd;
//Now I had initialized the tree control and I am want only few items of the tree to have
//checkboxes,In order to achieve that I did it as follows(Removing checkboxes where not required)
tvInsertItem.hParent = NULL;
tvInsertItem.hInsertAfter = TVI_ROOT;
tvInsertItem.item.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
tvInsertItem.item.pszText = L"Name"
hParentItemHandle = m_TreeCtrl.InsertItem(&tvInsertItem);
//Removal of checkboxes for the above item
tvItem.hItem = hParentItemHandle;
tvItem.mask = TVIF_TEXT|TVIF_STATE|TVIF_SELECTEDIMAGE;
tvItem.stateMask = TVIS_STATEIMAGEMASK;
tvItem.state = 0;
tvItem.pszText = szCommonModel;
m_TreeCtrl.SetItem(&tvItem);
}
Everything is fine I removed checkbox in which ever the node it is not required.And having checkbox where they are required.
All of a sudden I noticed this issue i.e, after selecting an item in the tree control which does not have checkbox ,now if
I press "space bar"then a checkbox is getting append to that item.
I want avoid the checkbox when I press space bar.
To avoid checkbox I tried this but did not work.
BOOL CTreeControlDlg::OnTvnItemChangingTree(UINT i,NMHDR *pNMHDR, LRESULT *pResult)
{
NMTVITEMCHANGE *pNMTVItemChange = reinterpret_cast<NMTVITEMCHANGE *>(pNMHDR);
HTREEITEM hTree = pNMTVItemChange->hItem;
UINT ChangItem = pNMTVItemChange->uStateNew;
UINT ChangItem1 = pNMTVItemChange->uStateOld;
UINT ItemState = m_TreeCtrl.GetItemState(hTree, TVIS_STATEIMAGEMASK);
if(98 == ItemState)//98 is the thing I observed while debugging this is not correct I know.
return FALSE;
return TRUE;
}
I am bit confused how do we get the state image mask TVIS_STATEIMGAEMASK and how do we make that check whether the item has this mask or not .
Can anyone please suggest me a way to acheive this.
Related
I have an issue Controlling CTabControl Inner tab items with TAB and Arrow keys.
here is my code and a few screenshots:
OnInitDialog() method of the main dialog window:
BOOL PressetsDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// AUTO GENERATED MFC DIALOGUE CODE HERE
...
// ..
// TODO: Add extra initialization here
CTabCtrl* pTabCtrl = (CTabCtrl*)GetDlgItem(IDC_TAB1);
m_one.Create(IDD_TAB_ONE, pTabCtrl);
CTabCtrl* pTabCtrl2 = (CTabCtrl*)GetDlgItem(IDC_TAB1);
m_two.Create(IDD_TAB_TWO, pTabCtrl2);
TCITEM item1, item2, item3;
item1.mask = TCIF_TEXT | TCIF_PARAM;
item1.lParam = (LPARAM)&m_one;
item1.pszText = L"Normal Presets";
pTabCtrl->InsertItem(0, &item1);
item2.mask = TCIF_TEXT | TCIF_PARAM;
item2.lParam = (LPARAM)&m_two;
item2.pszText = L"Movement Presets";
pTabCtrl2->InsertItem(1, &item2);
CRect rcItem;
pTabCtrl->GetItemRect(0, &rcItem);
m_one.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
CRect rcItem2;
pTabCtrl2->GetItemRect(0, &rcItem2);
m_two.SetWindowPos(NULL, rcItem2.left, rcItem2.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
m_one.ShowWindow(SW_SHOW);
m_two.ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control
}
and the OnTcnSelchangeTab method:
void PressetsDlg::OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
int nSelect = m_Tab.GetCurSel();
if (nSelect == 0)
{
m_one.ShowWindow(SW_SHOW);
m_two.ShowWindow(SW_HIDE);
}
else if (nSelect == 1)
{
m_two.ShowWindow(SW_SHOW);
m_one.ShowWindow(SW_HIDE);
}
else
{
m_one.ShowWindow(SW_SHOW);
m_two.ShowWindow(SW_HIDE);
}
*pResult = 0;
}
[to see the tab design click here](https://i.stack.imgur.com/Tt8c1.jpg)
I've set the tab order with Ctrl + D for each dialogue resource and set Tabstop property to either True or False and still nothing happens.
At first I thought that this feature is supposed to be supported automatically but it seems that it's not.
the dialogue window moves between tabs and buttons that placed on it but as soon as I try to move to "inner Items" of each tab, it doesn't reach them.
I suspect the reason is probably that each tab is a separate window and that's probably the reason that the inner items are unreachable..
I've made a few changes and now Moving with TAB and arrow keys functions properly.
first of all I set the Control Property of both child dialogs to True.
You go to Resource View >> (Solution name) >> (project name) >> IDD + (the id you gave to the dialog) doble click on it >> Properties >> Control.
Essentially what it did is to add the flag WS_CONTROL as mentioned here early to each of the Child windows so that they could be accessed from the main dialog window that contains them.
Of course that alone didn't do much because I also had a few bugs in the code, after days of searching for it I found an example online which helped me solve the bugs.
Then I've changed my code to this: and it started working:
// first we create two modeless dialogs and embed them as child windows
// of CTabControlTutorialDlg.
// Have a look using Spy++ to see the layout of the controls as they
// appear to windows
m_one.Create(IDD_TAB_ONE, this);
m_two.Create(IDD_TAB_TWO, this);
// next we get the captions of the dialogs and use these as the caption
// for the tab window. Of course we could just load a string from the
// resources or hard code a string for the text of the tab.
TCITEM item1, item2;
item1.mask = TCIF_TEXT | TCIF_PARAM;
item1.lParam = (LPARAM)&m_one;
item1.pszText = L"Normal Presets";
m_Tab.InsertItem(0, &item1);
item2.mask = TCIF_TEXT | TCIF_PARAM;
item2.lParam = (LPARAM)&m_two;
item2.pszText = L"Custom Presets";
m_Tab.InsertItem(1, &item2);
// finally we set the tab order correctly for the so that we can tab through the dialogs and into
// the cancel and ok buttons. If we don;t do this then the tab order is tab control, ok button, cancel
// button embedded dialogs.
CRect rcItem;
m_Tab.GetItemRect(0, &rcItem);
m_one.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 5, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER);
CRect rcItem2;
m_Tab.GetItemRect(0, &rcItem2);
m_two.SetWindowPos(NULL, rcItem2.left, rcItem2.bottom + 5, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER);
m_one.ShowWindow(SW_SHOW);
I found reference in this site:https://www.codeproject.com/Articles/4408/Creating-embedded-dialogs-in-MFC but in order to get their code example you need to register to the website.
It works!
I have a window which has a CTreeCtrl. A user can right-click any element and display a context menu. From there they can choose to delete the entry. Something like this:
Here is a snippet of the context menu item handler:
void CAssignHistoryDlg::OnDeleteFromAssignmentHistory()
{
CString strINI = theApp.GetAssignHistoryPath();
HTREEITEM hItem = m_treeHistory.GetSelectedItem();
CString strName, strDeletedName, strEntry;
if (m_treeHistory.GetParentItem(hItem) != nullptr)
{
// The user has picked one of the history dates.
// So the parent should be the actual name.
hItem = m_treeHistory.GetParentItem(hItem);
// Now OK to proceed
}
strName = ExtractName(hItem);
GetParent()->EnableWindow(FALSE);
strEntry.Format(IDS_TPL_SURE_DELETE_FROM_ASSIGN_HIST, strName);
if (AfxMessageBox(strEntry, MB_YESNO | MB_ICONQUESTION) == IDNO)
{
The image shows my problem. If I first click on Test, so that it is selected and bright blue and then right-click, it shows Test in the popup message. This is fine. But ...
If the first name is initially selected, and I go ahead and directly right-click Test, even though it seems to go blue (as if selected), m_treeHistory.GetSelectedItem() is returning the original, first name. I think I am describing it not very well.
In short, I want to guarantee that I get the HTREEITEM for the item that the user right-clicked on. What I have is not 100% fool-proof.
If it helps, this is how I display the context menu:
void CAssignHistoryDlg::OnNMRclickTreeHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
CMenu mnuContext, *pMnuEdit = nullptr;
CPoint ptLocal;
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
GetCursorPos(&ptLocal);
mnuContext.LoadMenu( IDR_MENU_SM_ASSIGN_HIST_POPUP );
pMnuEdit = mnuContext.GetSubMenu( 0 );
if (pMnuEdit != nullptr)
{
pMnuEdit->TrackPopupMenu( TPM_LEFTALIGN|TPM_LEFTBUTTON,
ptLocal.x, ptLocal.y, this, nullptr );
}
*pResult = 0;
}
So to recap, at the moment the user must physically left click on a item in the tree to select it. Then they can right-click and it will offer to delete this person. But if they go ahead and just right-click on anyone, it will not offer to delete THAT person.
You can get the actual tree item at any given point using the HitTest() member of the CTreeCtrl class. Exactly how and where you do this will depend on your code design but, if you have a pointer to your CTreeCtrl (pwTree in the code below), then you can do something like this:
CPoint ptLocal;
GetCursorPos(&ptLocal);
pWTree->ScreenToClient(&ptLocal); // May not be required in certain handlers?
HTREEITEM hItem = pWTree->HitTest(ptLocal); // Remember to check for NULL return!
You can then either use the returned hItem directly, or use it to explicitly set the tree's selection (to that item), before doing any further processing.
The MS doc: https://learn.microsoft.com/en-us/cpp/mfc/reference/cwnd-class?view=vs-2019 doesn't have a "click" handler, it has CWnd::OnRButtonDblClk, CWnd::OnRButtonDown and CWnd::OnRButtonUp. Which one are you handling?
The reason for your defect might be that you don't let the tree control handle that right button event (to select that new item).
I would suggest to use CWnd::OnContextMenu instead.
I need to detect whether a Uiview is a standard opened view or if it is an activated viewport on a sheet. Querying the uiview’s view Id returns the Id of the activated viewport's view. I have found no direct way to detect that a uiview is actually a sheet with an activated viewport.
I am already tracking opened views in the view activated event for another purpose. So I considered storing the view Id with the uiview hashcode for later checking that it was indeed a sheetview prior to becoming an activated view. Unfortunately, and I think in opposition to standard use, the uiview hashcode is not stable. Multiple hashcode requests from the uiview object return different values.
Does anyone have a way to detect this condition? I need to be able to use the the methods on the uiview still. So any help to find the actual child windows I would like to relate to the uiview object. The view still says "Sheet: ..." in the title when a view is activated.
TaskDialog mainDialog = new TaskDialog("Hello, viewport check!");
mainDialog.MainInstruction = "Hello, viewport check!";
mainDialog.MainContent =
"Sadly Revit API doesn't automatically know if the user is in an active viewport. "
+ "Please click 'Yes' if your are, or 'No' if your not.";
mainDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1,
"Yes, I am in an active viewport on a sheet.");
mainDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2,
"No, I am just in an ordinary view.");
mainDialog.CommonButtons = TaskDialogCommonButtons.Close;
mainDialog.DefaultButton = TaskDialogResult.Close;
TaskDialogResult tResult = mainDialog.Show();
bool YesOrNo = true;
if (TaskDialogResult.CommandLink1 == tResult)
{
YesOrNo = true;
}
else if (TaskDialogResult.CommandLink2 == tResult)
{
YesOrNo = false;
}
else{
return;
}
You can use the ViewSheet GetAllViewports method to determine all the viewports on a given sheet. Using that, you could put together a bi-directional dictionary lookup system map any sheet to all the viewports it hosts and vice versa. That should help solve your task. Here is some example usage:
http://thebuildingcoder.typepad.com/blog/2014/04/determining-the-size-and-location-of-viewports-on-a-sheet.html
Im late to the party - but another way to sense if the user is in a viewport is to investigate the Process.MainWindow title. Something like this (in RevitPythonShell):
import threading, clr
from System.Diagnostics import Process
# need winform libraries for feedback form only
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form, Label
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
ui = __revit__.ActiveUIDocument
def lookAtWindow(activeView):
# Looking for one of three conditions:
# 1. User is on a sheet (ActiveView will be DrawingSheet)
# 2. User is in an active ViewPort on a sheet (ActiveView will NOT be be DrawingSheet, but MainWindowTitle will contain " - [Sheet: " string)
# 3. User is on a View (neither of the previous two conditions)
result = False
if str(activeView.ViewType) == 'DrawingSheet':
result = 'Youre on a sheet'
else:
processes = list(Process.GetProcesses())
for process in processes:
window = process.MainWindowTitle
if window and 'Autodesk Revit '+app.VersionName[-4:] in window and ' - [Sheet: ' in window and ' - '+doc.Title+']' in window:
result = 'I reckon youre in a Viewport'
if not result:
result = 'so you must be in a '+str(activeView.ViewType)
form = Form()
form.Width = 300
form.Height = 100
label = Label()
label.Width = 280
label.Height = 70
label.Text = result
label.Parent = form
form.ShowDialog()
# need to close RevitPythonShell console before checking MainWindowTitle, so run on timer
threading.Timer(1, lookAtWindow, [ui.ActiveView]).start()
__window__.Close()
I created an Outlook bar with tree controls and would like to have it auto size to always fully display the text of the tree control. Ideally the border in the picture would move so that "Healthcare Merchandising" is fully visible.
newDimbar is a CMFCOutlookBar object created in CMainFrame.
I have tried stretching it:
void CTreeDrill::OnTvnItemexpanded(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
HTREEITEM hItem = pNMTreeView->itemNew.hItem;
RECT treeRect;
GetItemRect(GetChildItem(hItem), &treeRect, FALSE);
CMainFrame *pMain = (CMainFrame*)AfxGetMainWnd();
int iStretch = treeRect.right;
pMain->m_ctlNewDimBar.StretchPane(iStretch, FALSE);
EnsureVisible(GetChildItem(hItem));
}
and using move:
void CTreeDrill::OnTvnItemexpanded(NMHDR *pNMHDR, LRESULT *pResult)
{
RECT treeRect;
RECT newRect;
RECT dimRect;
GetItemRect(GetChildItem(hItem), &treeRect, FALSE);
CMainFrame *pMain = (CMainFrame*)AfxGetMainWnd();
pMain->m_ctlNewDimBar.GetWindowRect(&dimRect);
newRect = dimRect;
newRect.right = treeRect.right;
pMain->m_ctlNewDimBar.MoveWindow(newRect);
EnsureVisible(GetChildItem(hItem));
}
without luck. What am I missing?
Solution
I'm happy i found it because this is something i would have to do too in the future, you need to use CWnd::SetWindowPos to change the CMFCOutlookBar's size, check this tutorial that has more infos, after you change the size of CMFCOutlookBar you will have to use CFrameWndEx::RecalcLayout method of the frame that contains the CMFCOutlookBar.
Why using CWnd::SetWindowPos?
i don't know, it's the only one that worked for me
Why use CFrameWndEx::RecalcLayout and not only call RecalcLayout() of the CMFCOutlookBar?
Because if you just recalculate the layout of the CMFCOutlookBar only the CMFCOutlookBar will be updated and then if you have anything attached to the CMFCOutlookBar it will not recieve the changes, so you might end with your CMFCOutlookBar overlapping some other control or your document's view, calling CFrameWndEx::RecalcLayout will make the whole frame reacalculate and so if you have tabbed document views they will be updated/resized accordingly.
Your case
you will have to calculate the whole width of the tree, not only the item, and then use the CWnd::SetWindowPos on the CMFCOutlookBar with the updated value width value but keeping the height of the CMFCOutlookBar.
newDimbar.GetWindowRect(pos);
ScreenToClient(&pos);
UINT flags = SWP_NOZORDER | SWP_NOMOVE;
newDimbar.SetWindowPos(NULL, 0, 0, iNewWidth, pos.Height(), flags);
In MFC how to remove a menu-item of POPUP type. RemoveMenu() either take ID or position. Since, there is no ID for POPUP menu, option left is by using position.
But I am not getting how and where I can call RemoveMenu().
File Edit Test
Test_submenu_1
Test_submenu_2
Test_submenu_3 > submenu_3_item_1
Test_submenu_4
Test_submenu_5
I want to remove Test_submenu_3? I am not getting how do find CMenu object for Test so that I can call RemoveMenu() by passing position "2" for submenu_3_item_1.
Any suggestion for doing this will be greatly appreciated.
Thanks!
You cannot use LoadMenu, since this function does just that.
After modifying loaded menu it is killed when menu object used to load it goes out of scope. You have to modify menu that is currently used.
Your menu is a popup part of the main menu, second in position. It contains 5 items and second one is another popup. To my understanding, you want to remove this item and popup of this item.
To make it work you will have to ask main window for the current menu:
CMenu* pMenu = GetMenu(); // get the main menu
CMenu* pPopupMenu = pMenu->GetSubMenu(2);//(Test menu with item....)
pPopupMenu->RemoveMenu(2, MF_BYPOSITION);
Of course, this code is from the main frame. If you want to use it elsewhere, you will have to access all using pointer to the main frame.
Try the below. You get the Test sub-menu first (index 2), then once you have that you tell it to remove its Test_submenu_3 submenu by position (also 2).
CMenu topMenu;
topMenu.LoadMenu(IDR_YOUR_MENU);
CMenu& testSubMenu = *topMenu.GetSubMenu(2);
testSubMenu.RemoveMenu(2,MF_BYPOSITION);
'Test' is the 3rd menu item (by position) on the top level menu. It's just been rendered horizontally rather than vertically. Assuming you have a handle to the top level menu use the same code you'd use to the get the sub menus as you would to get the 'Test' menu.
You can use this below code to remove the submenu, by comparing name
bool RemoveSubmenu(CMenu * pMenu) {
for (int pos = 0; pos < pMenu->GetMenuItemCount(); pos++) {
wchar_t *name = new wchar_t[mf.cch + 1];
MENUITEMINFO mf;
ZeroMemory(&mf, sizeof(mf));
mf.cbSize = sizeof(mf);
mf.fMask = MIIM_SUBMENU | MIIM_FTYPE | MIIM_STRING;
mf.fType = MIIM_STRING;
mf.dwTypeData = NULL;
if (!GetMenuItemInfo(pMenu->m_hMenu, pos, TRUE, &mf))
break;
if (mf.hSubMenu != NULL){
mf.fMask = MIIM_TYPE;
mf.fType = MFT_STRING;
++mf.cch;
mf.dwTypeData = (LPSTR)name;
if (!GetMenuItemInfo(pMenu->m_hMenu, pos, TRUE, &mf)){
bRet = false;
break;
}
//
// compare sub menu name (i.e mf.dwTypeData) here, do the required
// modifications
//
pMenu->RemoveMenu(pos, MF_BYPOSITION);
bRet = true;
break;
}
}
}