I am making a PropertySheet control with the Windows API, and for some reason tabs are closing when I click on them.
For instance, if I add 4 tabs:
Then if I click on any of those tabs except the active one (in which case nothing happens), it becomes:
And if I click on the inactive tab it disappears, leaving only one tab left.
I am creating the tabs with a dialog template in memory, created like this:
static const char initText[] = "Tab";
pagetemplate = (DLGTEMPLATE*)new char[sizeof(DLGTEMPLATE) + sizeof(initText) * 2];
pagetemplate->style = WS_POPUP | DS_3DLOOK | WS_BORDER | WS_SYSMENU | WS_CAPTION | DS_CENTER;
pagetemplate->cdit = 0; // 0 controls
pagetemplate->cx = PROP_SM_CXDLG;
pagetemplate->cy = PROP_SM_CYDLG;
LPWORD lpword = (LPWORD)(pagetemplate + 1);
*lpword++ = 0; // no menu
*lpword++ = 0; // predefined dialog box class (by default)
MultiByteToWideChar(CP_ACP, NULL, initText, -1, (LPWSTR)lpword, sizeof(initText) * 2);
Then creating the PropertySheet like this:
PROPSHEETHEADER psh;
SecureZeroMemory(&psh, sizeof(PROPSHEETHEADER));
psh.dwSize = sizeof(PROPSHEETHEADER);
psh.dwFlags = PSH_MODELESS | PSH_PROPSHEETPAGE | PSH_USECALLBACK;
psh.pszCaption = "Window";
psh.nPages = 1;
psh.nStartPage = 0;
PROPSHEETPAGE pages[1];
SecureZeroMemory(pages, sizeof(PROPSHEETPAGE));
pages[0].dwSize = sizeof(PROPSHEETPAGE);
pages[0].dwFlags = PSP_DLGINDIRECT;
pages[0].pResource = CGTabWindow::pagetemplate;
pages[0].pfnDlgProc = DialogProc;
pages[0].lParam = (LPARAM)this;
psh.ppsp = (PROPSHEETPAGE*)&pages;
propsheet = PropertySheet(&psh);
And adding the four pages like this:
PROPSHEETPAGE page;
SecureZeroMemory(&page, sizeof(PROPSHEETPAGE));
page.dwSize = sizeof(PROPSHEETPAGE);
page.dwFlags = PSP_DLGINDIRECT;
page.pResource = CGTabWindow::pagetemplate;
page.pfnDlgProc = DialogProc;
page.lParam = (LPARAM)this;
HPROPSHEETPAGE hpage = CreatePropertySheetPage(&page);
SendMessage((HWND)propsheet, PSM_ADDPAGE, NULL, (LPARAM)hpage);
And finally, here's my DialogProc:
BOOL CALLBACK DialogProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
cout << "hey" << endl;
switch (iMsg)
{
case WM_INITDIALOG :
return TRUE ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case ID_EDIT :
return TRUE;
case ID_HELP :
return TRUE;
case IDOK :
EndDialog (hDlg, 0) ;
return TRUE ;
}
break ;
}
return FALSE;
}
But for some reason, "hey" is never displayed. Also, sending the PSM_INDEXTOHWND with any index always returns NULL. Does anyone know why this is happening?
It was because you must have at least 1 control in the dialog template.
Related
I'm trying to implement CPrintDialogEx. I have some additional needed options and I want to add another property page to the window. There are no MFC examples and trying the Win API example fails miserably. It cashes. What am I doing wrong?
CPrintDialogEx dlg;
PROPSHEETPAGE optionsPage1;
HPROPSHEETPAGE hOptionsPage;
memset(&optionsPage1, 0, sizeof(PROPSHEETPAGE));
optionsPage1.dwSize = sizeof(PROPSHEETPAGE);
optionsPage1.dwFlags = PSP_DLGINDIRECT;
optionsPage1.hInstance = AfxGetInstanceHandle();
optionsPage1.pszTemplate = MAKEINTRESOURCE(IDD_QREPORT_OPTIONS);
optionsPage1.pResource = (DLGTEMPLATE*)IDD_QREPORT_OPTIONS;
optionsPage1.hIcon = NULL;
optionsPage1.pszIcon = NULL;
optionsPage1.pszTitle = "Options";
optionsPage1.pfnDlgProc = AfxWndProc;
optionsPage1.lParam = NULL;
dlg.m_pdex.nPropertyPages = 1;
hOptionsPage = CreatePropertySheetPage(&optionsPage1);
dlg.m_pdex.lphPropertyPages = &hOptionsPage;
if (dlg.DoModal() == IDOK)
NULL pointer crash
optionsPage1.dwFlags = PSP_DLGINDIRECT;
...
optionsPage1.pResource = (DLGTEMPLATE*)IDD_QREPORT_OPTIONS;
IDD_QREPORT_OPTIONS is an integer, it should not be forced to cast in to DLGTEMPLATE*. Doing so will point pResource to some random memory address and is likely the cause of crash.
You don't need pResource anyway. Replace PSP_DLGINDIRECT with PSP_DEFAULT, this will instruct CreatePropertySheetPage to use pszTemplate.
PROPSHEETPAGE documentation:
pszTemplate
Type: LPCSTR
Dialog box template to use to create the page. This member can specify
either the resource identifier of the template or the address of a
string that specifies the name of the template. If the PSP_DLGINDIRECT
flag in the dwFlags member is set, pszTemplate is ignored. This member
is declared as a union with pResource.
Example:
INT_PTR CALLBACK dlgproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_COMMAND:
if(LOWORD(wParam) == IDC_BUTTON1)
MessageBox(hwnd, _T("test"), 0, 0);
return 0;
}
return FALSE;
}
PROPSHEETPAGE optionsPage1 = { 0 };
optionsPage1.dwSize = sizeof(PROPSHEETPAGE);
optionsPage1.dwFlags = PSP_DEFAULT | PSP_USETITLE;
optionsPage1.hInstance = AfxGetInstanceHandle();
optionsPage1.pszTemplate = MAKEINTRESOURCE(IDD_QREPORT_OPTIONS);
optionsPage1.pszTitle = _T("Options");
optionsPage1.pfnDlgProc = dlgproc;// AfxWndProc;
I have created TreeView like this:
TreeView=CreateWindowEx(0, WC_TREEVIEW, TEXT("Tree View"), WS_VISIBLE | WS_CHILD, 0, 0, 200, 500, hwnd, (HMENU)ID_TREE_VIEW, GetModuleHandle(NULL), NULL);
Now I added one item to it like shown on this website.
It all okay, but after hours and hours of googling I still didn't found answer to these questions:
How to add subitems (nodes)?
How to add checkbox on each item (how to determine if specified checkbox is checked)?
EDIT #4:
In response to OPs request, I have added an example that removes checkbox from a parent node.
THE PROBLEM IS THAT CHECKBOX STILL APPEARS WHEN USER SELECTS A NODE AND PRESSES SPACEBAR.
This question solves that problem.
EDIT #3:
I have added the code that creates a node that is already checked.
It is the second child bode in the WM_CREATE handler.
END OF EDIT
case WM_CREATE:
{
// this is your treeview
TreeView = CreateWindowEx(0, WC_TREEVIEW,
TEXT("Tree View"), WS_VISIBLE | WS_CHILD,
0, 0, 200, 500,
hwnd, (HMENU)ID_TREE_VIEW, GetModuleHandle(NULL), NULL);
/************ enable checkboxes **************/
DWORD dwStyle = GetWindowLong( TreeView , GWL_STYLE);
dwStyle |= TVS_CHECKBOXES;
SetWindowLongPtr( TreeView , GWL_STYLE, dwStyle );
/************ add items and subitems **********/
// add root item
TVINSERTSTRUCT tvis = {0};
tvis.item.mask = TVIF_TEXT;
tvis.item.pszText = L"This is root item";
tvis.hInsertAfter = TVI_LAST;
tvis.hParent = TVI_ROOT;
HTREEITEM hRootItem = reinterpret_cast<HTREEITEM>( SendMessage( TreeView ,
TVM_INSERTITEM, 0, reinterpret_cast<LPARAM>( &tvis ) ) );
// and here is an example of removing a checkbox
// from a specific item/subitem in case you ever need it
TVITEM tvi;
tvi.hItem = hRootItem ;
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
TreeView_SetItem( TreeView, &tvi );
// add firts subitem for the hTreeItem
memset( &tvis, 0, sizeof(TVINSERTSTRUCT) );
tvis.item.mask = TVIF_TEXT;
tvis.item.pszText = L"This is first subitem";
tvis.hInsertAfter = TVI_LAST;
tvis.hParent = hRootItem;
HTREEITEM hTreeSubItem1 = reinterpret_cast<HTREEITEM>( SendMessage( TreeView ,
TVM_INSERTITEM, 0, reinterpret_cast<LPARAM>( &tvis ) ) );
// now we insert second subitem for hRootItem
memset( &tvis, 0, sizeof(TVINSERTSTRUCT) );
tvis.item.mask = TVIF_TEXT | TVIF_STATE; // added extra flag
tvis.item.pszText = L"This is second subitem";
tvis.hInsertAfter = TVI_LAST;
tvis.hParent = hRootItem;
// for demonstration purposes let us check this node;
// to do that add the following code, and add the extra flag for
// mask member like above
tvis.item.stateMask = TVIS_STATEIMAGEMASK;
tvis.item.state = 2 << 12;
HTREEITEM hTreeSubItem2 = reinterpret_cast<HTREEITEM>( SendMessage( TreeView ,
TVM_INSERTITEM, 0, reinterpret_cast<LPARAM>( &tvis ) ) );
// let us expand the root node so we can see if checked state is really set
TreeView_Expand( TreeView, hRootItem, TVE_EXPAND );
}
return 0L;
EDIT #2:
Here Is the part that explains how to check if item is checked ( it now properly checks when you click on a checkbox and when you press spacebar! ) :
case WM_NOTIFY:
{
LPNMHDR lpnmh = (LPNMHDR) lParam;
if( lpnmh->idFrom == ID_TREE_VIEW ) // if this is our treeview control
{
switch( lpnmh->code ) // let us filter notifications
{
case TVN_KEYDOWN: // tree has keyboard focus and user pressed a key
{
LPNMTVKEYDOWN ptvkd = (LPNMTVKEYDOWN)lParam;
if( ptvkd->wVKey == VK_SPACE ) // if user pressed spacebar
{
// get the currently selected item
HTREEITEM ht = TreeView_GetSelection( ptvkd->hdr.hwndFrom );
// Prepare to test items state
TVITEM tvItem;
tvItem.mask = TVIF_HANDLE | TVIF_STATE;
tvItem.hItem = (HTREEITEM)ht;
tvItem.stateMask = TVIS_STATEIMAGEMASK;
// Request the information.
TreeView_GetItem( ptvkd->hdr.hwndFrom, &tvItem );
// Return zero if it's not checked, or nonzero otherwise.
if( (BOOL)(tvItem.state >> 12) - 1 )
MessageBox( hwnd, L"Not checked!", L"", MB_OK );
else
MessageBox( hwnd, L"Checked!", L"", MB_OK );
}
}
return 0L; // see the documentation for TVN_KEYDOWN
case NM_CLICK: // user clicked on a tree
{
TVHITTESTINFO ht = {0};
DWORD dwpos = GetMessagePos();
// include <windowsx.h> and <windows.h> header files
ht.pt.x = GET_X_LPARAM(dwpos);
ht.pt.y = GET_Y_LPARAM(dwpos);
MapWindowPoints( HWND_DESKTOP, lpnmh->hwndFrom, &ht.pt, 1 );
TreeView_HitTest(lpnmh->hwndFrom, &ht);
if(TVHT_ONITEMSTATEICON & ht.flags)
{
// Prepare to receive the desired information.
TVITEM tvItem;
tvItem.mask = TVIF_HANDLE | TVIF_STATE;
tvItem.hItem = (HTREEITEM)ht.hItem;
tvItem.stateMask = TVIS_STATEIMAGEMASK;
// Request the information.
TreeView_GetItem( lpnmh->hwndFrom, &tvItem );
// Return zero if it's not checked, or nonzero otherwise.
if( (BOOL)(tvItem.state >> 12) - 1 )
MessageBox( hwnd, L"Not checked!", L"", MB_OK );
else
MessageBox( hwnd, L"Checked!", L"", MB_OK );
}
}
default:
break;
}
}
}
break;
The relevant idea for proper testing when spacebar is pressed is handling of TVN_KEYDOWN message.
We use this message to get NMTVKEYDOWN structure filled, which will give us virtual key code of the pressed button and the HWND of the treeview that sent the notification.
Now we use TreeView_GetItem() macro to get the currently selected node and we check its state the same way we did when we did hit testing.
My only problem is concerning this part from the documentation for TVN_KEYDOWN:
Return value
If the wVKey member of lParam is a character key code, the character
will be used as part of an incremental search. Return nonzero to
exclude the character from the incremental search, or zero to include
the character in the search. For all other keys, the return value is
ignored.
I just do not know what to do with the return result so I have put 0L.
Important note: If you need to return value from dialog box procedure use something like this:
SetWindowLongPtr( hwnd, DWLP_MSGRESULT, (LONG_PTR)1 );
return TRUE;
see the remarks for Return value in this documentation and use SetWindowLongPtr instead of SetWindowLong so you can support both x32 and x64 versions of Windows.
That would be all. Hopefully you have your problem solved. If you need further help leave a comment.
END OF EDIT
I have never done checking if tree item is checked but I believe that accepted answer to this question is the way to go.
NOTE:
I would highly appreciate if there someone who can provide code snippet for showing how to determine if treeview node is checked or not.
I have a straight win32 c++ app and I'm filling the window with a ListView whose view type is set to LV_VIEW_TILE and I'm also setting the style to LVS_OWNERDATA.
I'm having trouble trying to work out how to get the subitems to show. This code creates the view.
DWORD exstyle =WS_EX_CLIENTEDGE|LVS_EX_DOUBLEBUFFER|LVS_EX_JUSTIFYCOLUMNS|LVS_EX_INFOTIP;
g_hwndList = CreateWindowEx(exstyle, WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_ICON | LVS_OWNERDATA, 0, 0, 0, 0, hWnd, (HMENU) 2702, hInst, NULL);
ListView_SetView(g_hwndList, LV_VIEW_TILE);
LVTILEVIEWINFO tileViewInfo = { };
tileViewInfo.cbSize = sizeof(LVTILEVIEWINFO);
tileViewInfo.dwFlags = LVTVIF_AUTOSIZE;
tileViewInfo.dwMask = LVTVIM_COLUMNS;
tileViewInfo.cLines = 1;
BOOL tst = ListView_SetTileViewInfo(g_hwndList, &tileViewInfo);
I only want one more subitem/column to appear. In my LVN_GETDISPINFO I currently have this:
static int colfmt[1];
colfmt[0] = LVCFMT_LEFT;
static int order[1];
order[0] = 1;
if ((nimfo->item.mask & LVIF_COLUMNS) == LVIF_COLUMNS) {
nimfo->item.cColumns = 1;
nimfo->item.piColFmt = PINT(colfmt);
nimfo->item.puColumns = PUINT(order);
}
if ((nimfo->item.mask & LVIF_TEXT) == LVIF_TEXT) {
nimfo->item.pszText = di->LABEL;
}
if ((nimfo->item.mask & LVIF_IMAGE) == LVIF_IMAGE) {
nimfo->item.iImage = di->IMAGE_INDEX;
}
I can't work out at what point and where I need to supply the subitem/column text, I'm never seeing the nimfo->item.subitem changing from 0 and for each call for LVIF_TEXT the structure values are always the same.
So at what point do I need to supply the extra textual data?
Many thanks.
I, rather stupidly, wasn't adding any columns and therefore wasn't being asked for the other items.
added this and everything works
LVCOLUMN col = {};
col.mask = LVCF_SUBITEM;
col.iSubItem = 0;
ListView_InsertColumn(g_hwndList, 0, &col);
I'm creating taskbar icon of a mfc application and in MyView.cpp file I've written
static const UINT WMU_NOTIFY_TASKBAR_ICON = ::RegisterWindowMessage(_T("NOTIFY_TASKBAR_ICON"));
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
// added messages by the developer
ON_REGISTERED_MESSAGE(WMU_NOTIFY_TASKBAR_ICON, OnNotifyTaskbarIcon)
END_MESSAGE_MAP()
//...
void CMyView::AddTaskbarIcon()
{
DWORD dwMessage = NIM_ADD;
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = HWND(AfxGetApp()->m_pMainWnd);
nid.uID = 0;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = WMU_NOTIFY_TASKBAR_ICON;
nid.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_SS_ICON));
nid.dwInfoFlags = NIIF_INFO;
::Shell_NotifyIconW(dwMessage, &nid);
}
LRESULT CMyView::OnNotifyTaskbarIcon( WPARAM wParam, LPARAM lParam )
{
UINT uID = (UINT)wParam;
UINT uMouseMsg = (UINT)lParam;
switch(uMouseMsg)
{
case WM_LBUTTONDOWN:
break;
case WM_LBUTTONDBLCLK:
//if(IsIconic())
{
break;
}
case WM_RBUTTONDOWN:
{
// must be implemented:
// app popup menu will be showed
CMenu* pMenu = GetMenu();
if( pMenu )
{
CMenu *pSubMenu = NULL;
pSubMenu = pMenu->GetSubMenu( 0 );
{
SetForegroundWindow(); // *** little patch here ***
POINT pointCursor;
::GetCursorPos( &pointCursor );
pSubMenu->TrackPopupMenu( TPM_RIGHTALIGN,
pointCursor.x, pointCursor.y,
this );
}
}
}
break;
case WM_RBUTTONDBLCLK:
break;
case WM_MOUSEMOVE:
break;
}
return 0L;
}
and in My.cpp
BOOL CMyApp::InitInstance()
{
//...
myViewPtr->AddTaskbarIcon();
//...
}
the app launches, the icon appears on the taskbar but it disappears on mouse hovering.
Have I done something wrong? Thanx
AfxGetApp()->m_pMainWnd points to the main frame window, not to the view. I suspect that the frame window is receiving the WMU_NOTIFY_TASKBAR_ICON message and not handling it so Windows removes the icon.
You could either handle the message in the frame window class, or pass the handle to the view instead, like this:
void CMyView::AddTaskbarIcon()
{
...
nid.hWnd = GetSafeHwnd();
...
}
I was thinking i could use the LVITEM structures LPARAM to attach a pointer to my class, but i can't seem to get it to work!
Heres the main parts of my code:
Creating the listview:
hlvQuiz = CreateChild(WC_LISTVIEW, "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | LVS_ICON | LVS_AUTOARRANGE,
0, 0, 320, 240, m_hwnd, FontNormal);
Adding items:
if (vQuizes.size() > 0)
{
LVITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iItem = 0;
lvi.iSubItem = 0;
lvi.cchTextMax = QUIZSTRLEN;
for (unsigned int i = 0; i < vQuizes.size(); i++)
{
lvi.lParam = (LPARAM)&vQuizes[i]; // adding pointer to lparam
lvi.pszText = vQuizes[i].szName;
ListView_InsertItem(hlvQuiz, &lvi);
}
}
Then later when i go to get my class back from the LPARAM:
LVITEM lvi;
lvi.iItem = ListView_GetNextItem(hwnd, -1, LVNI_SELECTED);
lvi.iSubItem = 0;
if (ListView_GetItem(fm->hlvQuiz, &lvi) == TRUE)
{
Quiz* q = (Quiz*)lvi.lParam;
if (q != NULL) // i get stopped here because my pointer is NULL
if (Exists(q->szPath) == IS_FILE)
ShellExecute(NULL, "edit", q->szPath, NULL, NULL, SW_SHOWNORMAL);
}
Is there anything that i'm doing wrong? the listview creates fine, and the items add, but the pointer to my class which i put in the LPARAM value seems to be ignored, or changed by the time i come to dereference it
I haven't worked at this low level before, but I suspect you need to set the mask member of the LVITEM structure to LVIF_PARAM (as well as the appropriate values for anything else you need) for the call to ListView_GetItem.
Your code works fine in Debug mode but not in Release mode because
you missed to specify the name of LPARAM in lvi.mask (and the name of any other field you want back).
Try this:
lvi.iItem = ListView_GetNextItem(hlvQuiz, -1, LVNI_SELECTED);
lvi.mask = LVIF_PARAM;
if (ListView_GetItem(fm->hlvQuiz, &lvi) == TRUE) ...
You'll receive a copy of the LPARAM's value you've setted. I think that this little strange behaviour is due to the Debug mode's help that initalize everything for you. The Release mode instead do not.