How could I Add Minimize and Maximize box to the system menu of CMFCPropertySheet.
I have tried modifying the style by
CMFCPropertySheet::ModifyStyle(NULL, WS_SYSMENU);
but nothing happened.
Assuming you have a class derived from CPropertySheet, let's call it MySheet:
// Capture the WM_NCREATE message
BEGIN_MESSAGE_MAP(CMySheet, CPropertySheet)
ON_WM_NCCREATE()
END_MESSAGE_MAP()
BOOL CMySheet::OnNcCreate(LPCREATESTRUCT lpCreateStruct)
{
if (!CPropertySheet::OnNcCreate(lpCreateStruct))
return FALSE;
// Modify the window style
LONG dwStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);
::SetWindowLong(m_hWnd, GWL_STYLE, dwStyle | WS_WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
return TRUE;
}
Note that you could do this in the OnInitDialog, but even though the Minimize/Maximize boxes will show, they won't do anything.
doing just this in the "OnInitDialog:" worked for me.
LONG dwStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);
::SetWindowLong(m_hWnd, GWL_STYLE, dwStyle | WS_WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
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 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.
I have found a GDI leak in our huge application software.
Below is a simple program to test this problem.
The Idea is that the main dialog box opens another dialog box(dialog box A).
If the dialog box A include a bitmap function for a CStatic control,
it will create GDI leak.
Even when I use "DeleteObject(bitmap)".
Have I done some thing wrong ?
Do you have any thoughts?
Thanks.
// Resource File
...
DIALOG_BOXA DIALOGEX 0, 0, 219, 142
STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_BORDER
EXSTYLE WS_EX_STATICEDGE
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,46,121,50,14
PUSHBUTTON "Cancel",IDCANCEL,119,121,50,14
CONTROL 131,RED_LIGHT0,"Static",SS_BITMAP,7,17,80,37
PUSHBUTTON "",RED_LIGHT1,7,60,80,37,BS_BITMAP | NOT WS_TABSTOP
END
// head file
DialogBoxA: public CDialog
{
...
CStatic m_static;
CButton m_button ;
...
}
/////////////////////////////////////////////////////////
void DialogBoxA::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, RED_LIGHT0, m_static);
DDX_Control(pDX, RED_LIGHT1, m_button);
}
BOOL DialogBoxA::OnInitDialog()
{
CDialog::OnInitDialog();
HBITMAP bitmap ;
// This will create GDI leak !!!
bitmap = LoadBitmap ( AfxGetApp()->m_hInstance,BEACON_BIG_RED_ON) ;
m_static.SetBitmap (bitmap );
DeleteObject(bitmap);
// This is OK !!!
bitmap = LoadBitmap ( AfxGetApp()->m_hInstance,BEACON_BIG_RED_ON) ;
m_button.SetBitmap (bitmap );
DeleteObject(bitmap);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Here is the exlanantion and solution for the GDI leak in static control:
http://blogs.msdn.com/b/oldnewthing/archive/2014/02/19/10501282.aspx
It's complicated. First, let's take a closer look at your code
bitmap = LoadBitmap ( AfxGetApp()->m_hInstance,BEACON_BIG_RED_ON) ;
m_static.SetBitmap (bitmap );
DeleteObject(bitmap);
You should not delete the bitmap that you just told the static control to use. It's still using it! It's going to continue using it until you set the bitmap to something else.
So instead of trying to delete the bitmap during dialog initialization, you should wait until the dialog is being destroyed. At that point, you have to get tell the static control to stop using the bitmap, and only then can you delete the bitmap object.
Second, static controls sometimes make a copy of your bitmap. Whether it makes a copy depends on a bunch of things, including whether the bitmap has a non-zero alpha channel. It becomes your problem to destroy this copy as well. (This may be why your one of your cases works and the other doesn't.)
You basically have to keep track of the bitmap you think you set until it's time to clean up. Then you have to compare what's actually in the static control to the one you thought you gave it. If they're different, then you destroy both (your bitmap and the static controls copy). If they're the same, then the static control didn't make a copy, and you have to destroy just the one you made (once!).
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;
}
}
}
I've embedded a property sheet in a dialog box IDC_CONTAINER by specifying the HWND of the container in the hwndParent of the property sheet header when creating the property sheet. In the callback I specified the following...
int CALLBACK PSCallback (HWND hWnd, UINT message, LPARAM lParam)
{
LONG Z, Y;
switch (message)
{
case PSCB_PRECREATE: // Remove Title Bar and Borders.
// SO FAR border removal not working
((LPDLGTEMPLATE)lParam)->style &= ~WS_CAPTION;
break;
}
return 0;
}
The property sheet is displayed without a title bar as expected but with a border. I expected the border to go away since WS_CAPTION contains the border style per the MSDN.
Is there some way to get rid of the border?
win32api no mfc c++
I managed to do what I needed, see comments added to original posting.