MFC CFileDialog prevent from opening *.ext.other_ext files - mfc

I'm trying to create file dialog using the next code:
CFileDialog fd (TRUE, NULL, _T("*.pid"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY);
fd.m_ofn.lpstrInitialDir=m_CurrentDir;
if (fd.DoModal() == IDOK)
...
When the dialog is opened I see both *.pid and *.pid.saved files.
Is there any to prevent from the dialog to show me *.pid.saved files?
Thanks!

You must specify the filter the dialog should use:
LPCTSTR szFilter = _T( "PID Files (*.pid)|*.pid|All Files (*.*)|*.*" );
DWORD dwFlags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
CFileDialog dlg( TRUE, _T( "pid" ), NULL, dwFlags, szFilter );
dlg.DoModal();
...

Related

Simplest method to create an OpenFileDialog and return the pathname to edittext box using MFC applications

How to create a MFC application in which I want to implement an OpenFileDialog box and the resultant path name to be displayed on the edittext box.
Here is an example that will help you get started:
const TCHAR szFilter[] = _T("CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||");
CFileDialog dlg(FALSE, _T("csv"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
if(dlg.DoModal() == IDOK)
{
CString sFilePath = dlg.GetPathName();
m_FilePathEditBox.SetWindowText(sFilePath);
}

Selecting and Highlighting an Item from List View

I would like to select and highlight an item from a list view control and am using the following code
#include <Windows.h>
#include <commctrl.h>
int main() {
//Hardcoded Handle to the ListView Windows of Add Printer Dialog
HWND hwndListView = (HWND)0x000206D6;
DWORD dwProcessID;
::GetWindowThreadProcessId( hwndListView, &dwProcessID );
HANDLE process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);
LVITEM lvi;
LVITEM* _lvi=(LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
lvi.state = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.mask = LVIF_STATE;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hwndListView, LVM_SETITEMSTATE, (WPARAM)0, (LPARAM)_lvi);
VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);
}
The result I am getting is
instead of the item getting selected and highlighted
Please let me know what might be going wrong
There are restrictions on which processes can set focus on a window, and chances are that the app selecting the ListView items does not satisfy those restrictions while the dialog is active. For example, the HWND being focused must be attached to the calling thread's message queue. So the highlighting app will have to use AttachThreadInput() before calling SetFocus() on another app's windows.

MFC Save file dialog

I am writing an MFC C++ application that has a Save As button for saving a .txt file to the disc. With it I am trying to add an extra verification for file overwriting (if a file with the same filename exists, then it should query the user if he wants to overwrite the old file or not). I have tried this with the below code, but it doesn't really work. When I click No on the MessageBox, it should reopen the Save As file dialog, but instead it gives me two errors: the first one is Debug assertion failed, and the second one is Encountered an improper argument. How should I do this better? This is the code:
char strFilter[] = { "Text Files (*.txt)|*.txt|" };
CFileDialog FileDlg(FALSE, CString(".txt"), NULL, 0, CString(strFilter));
while(true)
{
if( FileDlg.DoModal() == IDOK ) // this is the line which gives the errors
{
agendaName = FileDlg.GetFileName(); //filename
agendaPath = FileDlg.GetFolderPath(); //filepath (folders)
if(model->agendaExists(CSToString(agendaPath+TEXT("\\")+agendaName))) // there is another file called the same way
{
if(MessageBox(TEXT("A file with the specified name already exists. Overwrite?"), TEXT("File exists"), MB_YESNO) != 6) // user clicked NO (do not overwrite file)
{
continue;
}
}
model->sendToFile(CSToString(agendaPath+TEXT("\\")+agendaName)); // the file is unique so the agenda named agendaName found at path agendaPath is saved
return;
}
}
It should be mentioned that the errors occur on line 7 and only on the second loop through the while.
CFileDialog can detect itself if a file exists and prompt the user for overwriting.
explicit CFileDialog(
BOOL bOpenFileDialog,
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL,
DWORD dwSize = 0
);
Just pass OFN_OVERWRITEPROMPT for the flags.
As for your problem, run in Debugger and when you get that assertion press the Retry button to see where the problem comes from (you'll probably have to look through the call stack also). Maybe you should try putting this in the while loop:
CFileDialog FileDlg(FALSE, CString(".txt"), NULL, 0, CString(strFilter));
You should use the OFN_OVERWRITEPROMPT flag in the constructor. That flag is usually one of the default flags, but you have set your flags to 0. So, if you do:
CFileDialog FileDlg(FALSE, CString(".txt"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, CString(strFilter));
if (FileDlg.DoModal() == IDOK)
{
model->sendToFile(CSToString(FileDlg.GetPathName()));
}
It should work. By the way, GetPathName() gets the full path to the selected file, so you don't need to get the folder and the file name in 2 steps.
Try including below line inside the while loop (as first line in while loop)
CFileDialog FileDlg(FALSE, CString(".txt"), NULL, 0, CString(strFilter));
This line is outside the while loop in your code

How to get a folder's open icon with SHGetFileInfo() method?

This is what I did:
LPMALLOC malloc;
LPITEMIDLIST pidl;
SHFILEINFO FileInfo;
SFGAOF sfGao;
if (SUCCEEDED(SHGetMalloc(&malloc))
{
if (SUCCEEDED(SHParseDisplayName(strDirPath, NULL, &pidl, SFGAO_FOLDER, &sfGao)))
{
SHGetFileInfo((LPCWSTR)(PCHAR(pidl)), 0, &FileInfo, sizeof(FileInfo), SHGFI_PIDL | SHGFI_ICON);
CDC* pDC = GetWindowDC();
pDC->DrawIcon(10, 10, FileInfo.hIcon);
ReleaseDC(pDC);
}
malloc->Free(pidl);
}
malloc->Release();
Here's the problem: I found that I can get the icon of a folder easily with this approach. But I could not get its open icon, when I set the fourth parameter of SHGetFileInfo method to be SHGFI_PIDL | SHGFI_OPENICON. The hIcon of FileInfo is always NULL, and I don't know why.
Can anyone tell me how to fix the problem?
From the comments:
I have resolved the problem. Modify the fourth parameter from SHGFI_PIDL | SHGFI_OPENICON to SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_OPENICON, then I was able to retrieve the open icon of the specified folder.

How to get file icon using C++

I want to add Icon to treeview node, using C++. I want to get the icons from system, I tried
I tried with,
PMString ucPath("C:\\path\\to\\file.extension");
SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
iconView->SetRsrcID((RsrcID) info.hIcon);
::DestroyIcon(info.hIcon);
where, SetResrcID ,PMString are the InDesing API and iconView is the controlView of the Tree, I am not getting what's going wrong, if anyone has idea please suggest.
Thanks,
Praveen Mamdge
Here is the codes what I'm using in my application, you should change the icon to a bitmap.
PMString ucPath("C:\\path\\to\\file.extension");
SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
ICONINFO stIconInfo;
GetIconInfo(s_sfi.hIcon, &stIconInfo);
HBITMAP hBmp = stIconInfo.hbmColor;
DestroyIcon(s_sfi.hIcon) ;
The best way to do it is using the system icon index with SHGFI_SYSICONINDEX.
Some thing like this,
Extract icon from file first.
SHFILEINFO stFileInfo;
SHGetFileInfo( file,
FILE_ATTRIBUTE_NORMAL,
&stFileInfo,
sizeof( stFileInfo ),
SHGFI_ICON | SHGFI_LARGEICON );
Then add to imagelist and use the index to set icon.
m_nIndex = m_ilLargeIcons.Add( stFileInfo.hIcon );
This is your code snippet, observe line by line:
PMString ucPath("C:\path\to\file.extension"); SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
SHGFI_ICON |
SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
iconView->SetRsrcID((RsrcID) info.hIcon);
::DestroyIcon(info.hIcon);
After this line: iconView->SetRsrcID((RsrcID) info.hIcon);, you called ::DestroyIcon that destroyed that icon you stored.