I recently found some code on here which shows us how to let users choose the directory in C++ which isn't as simple as C#. I keep getting three errors I barley understand. And i want to know how to store the path onto a string.
ERRORS:
argument of type "char *" is incompatible with parameter of type "LPWSTR"
a value of type "const char *" cannot be assigned to an entity of type "LPCWSTR"
no suitable constructor exists to convert from "TCHAR [260]" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>"
and here is the code i'm using:
int CALLBACK BrowseForFolderCallback(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
{
char szPath[MAX_PATH];
switch (uMsg)
{
case BFFM_INITIALIZED:
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
break;
case BFFM_SELCHANGED:
if (SHGetPathFromIDList((LPITEMIDLIST)lp, szPath))
{
SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szPath);
}
break;
}
return 0;
}
static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED)
{
std::string tmp = (const char*)lpData;
std::cout << "path: " << tmp << std::endl;
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
return 0;
}
std::string BrowseFolder(std::string saved_path)
{
TCHAR path[MAX_PATH];
const char* path_param = saved_path.c_str();
BROWSEINFO bi = { 0 };
bi.lpszTitle = ("Browse for folder...");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM)path_param;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl != 0)
{
//get the name of the folder and put it in path
SHGetPathFromIDList(pidl, path);
//free memory used
IMalloc* imalloc = 0;
if (SUCCEEDED(SHGetMalloc(&imalloc)))
{
imalloc->Free(pidl);
imalloc->Release();
}
return path;
}
return "";
}
The First Error occurs in: case BFFM_SELCHANGED
The Second Error occurs in: bi.lpszTitle = ("Browse for folder...");
The Final Error occurs in: return path;
EDIT: The second and third error are fixed. However, the first one still remains and i have no clue how to fix it
try to add a L in front of the strings, like that :
bi.lpszTitle = (L"Browse for folder...");
this will turn the string into a Wide String
in order to make this code compile with Visual Studio :
Project -> Properties -> Advanced -> Character Set = Use Multi-Byte ChararacterSet
I've created a dialogbox with progress bar and a cancel button using CreateDialogParam to show status while copying several files (using CopyFileEx).
How do I cancel the process using CopyFileEx correctly, starting from pressing the cancel button in dialogbox? Is there anyway I can do it without using global variable? And How do I correctly handle the returned PROGRESS_CANCEL? I have provided questions in the code below to make clearer what help do I need.
//copy function
BOOL copy(HWND &hWnd, std::vector <FILECONSOLIDATEPARAMS> &vec)
{
//pass vector as lparam to dialogbox proc
LPARAM lp = reinterpret_cast<LPARAM>(&vec);
HWND hCopy = CreateDialogParam(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_DIALOG1),
hwndmain, (DLGPROC)dlgboxcopyproc, lp);
static HWND hIDC_STATIC, hIDC_STATIC4;
hIDC_STATIC = GetDlgItem(hCopy, IDC_STATIC);
hIDC_STATIC4 = GetDlgItem(hCopy, IDC_STATIC4);
LPBOOL pbCancel = FALSE;
size_t s;
for (s = 0; s != vec.size(); s++)
{
SendMessage(hIDC_STATIC, WM_SETTEXT, 0, (LPARAM)vec[s].filename);
SendMessage(hIDC_STATIC4, WM_SETTEXT, 0,(LPARAM)vec[s].destination);
BOOL b = CopyFileEx(vec[s].filename, vec[s].destination,
&CopyProgressRoutine,(LPVOID)hCopy,pbCancel, NULL);
//how to catch and process PROGRESS_CANCEL?
if (!b)
{
DWORD dw = GetLastError();
ShowErrMsg(dw);
}
}
PostMessage(hCopy, WM_DESTROY, 0, 0);
return TRUE;
}
//dialogbox procedure
INT_PTR CALLBACK dlgboxcopyproc(HWND hWndDlg,UINT Msg,WPARAM wParam,LPARAM
lParam)
{
//translate passed lparam back to vector
std::vector<FILECONSOLIDATEPARAMS>& vect =
*reinterpret_cast<std::vector<FILECONSOLIDATEPARAMS>*>(lParam);
INITCOMMONCONTROLSEX _icex;
_icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
_icex.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&_icex);
static HWND hParent;
static HWND hIDCancel;
static HWND hIDC_PROGRESS1;
static HWND hIDC_STATIC;
hParent = GetParent(hWndDlg);
hIDCancel = GetDlgItem(hWndDlg, IDCANCEL);
hIDC_PROGRESS1 = GetDlgItem(hWndDlg, IDC_PROGRESS1);
switch (Msg)
{
case WM_INITDIALOG:
{
SendMessage(hIDC_PROGRESS1, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
}
return (INT_PTR)TRUE;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDCANCEL:
EndDialog(hWndDlg, FALSE); //how to make pbCancel = TRUE?
return (INT_PTR)TRUE;
}
}
break;
case WM_DESTROY:
{
DestroyWindow(hWndDlg);
}
}
return FALSE;
}
//copyprogressroutine callback function
DWORD CALLBACK CopyProgressRoutine(LARGE_INTEGER TotalFileSize,
LARGE_INTEGER TotalBytesTransferred, LARGE_INTEGER StreamSize,
LARGE_INTEGER StreamBytesTransferred, DWORD dwStreamNumber, DWORD
dwCallbackReason, HANDLE hSourceFile, HANDLE hDestinationFile, LPVOID
lpData)
{
HWND hWndDlg = (HWND)lpData;
static HWND hwndIDC_PROGRESS1;
hwndIDC_PROGRESS1 = GetDlgItem(hWndDlg, IDC_PROGRESS1);
DOUBLE Percentage = ((DOUBLE)TotalBytesTransferred.QuadPart /
(DOUBLE)TotalFileSize.QuadPart) * 100;
switch (dwCallbackReason)
{
case CALLBACK_CHUNK_FINISHED:
SendMessage(hwndIDC_PROGRESS1, PBM_SETPOS, (WPARAM) Percentage, 0);
break;
case CALLBACK_STREAM_SWITCH:
Percentage = 0;
break;
}
return PROGRESS_CONTINUE; //how to make conditional return PROGRESS_CANCEL?
}
code like
LPBOOL pbCancel = FALSE;
CopyFileEx(, pbCancel, )
senseless. really we simply pass 0 in place pbCancel and have no ability cancel operation.
we need alocate some variable (let name it bCancel) of type BOOL and pass pointer of this variable to CopyFileEx
something like this:
BOOL bCancel = FALSE;
CopyFileEx(, &bCancel, )
the CopyFileEx will be periodically query value of this variable by passed pointer and if it became true - break operation and return ERROR_REQUEST_ABORTED error.
the next - CopyFileEx not return until operation is complete - this is synchronous api - as result it can not be called from GUI thread (or it simply block GUI). need call it from separate thread.
so basic solution - allocate some data structure, here place BOOL bCancel, and other data, which we will be use during copy. we will access this data structure from 2 threads - gui thread (from dialog procedure) and from separate thread, which will call CopyFileEx. for correct implement this - need use reference counting on structure. the lpProgressRoutine need post messages to gui thread for notify it about progress (gui thread can move progress bars on this notification). dialog can containing for example Cancel button. when user click it - GUI thread simply set bCancel = TRUE, as result CopyFileEx abort operation on next chunk. for start copy - need start new thread, which call CopyFileEx. based on required logic - can do this from WM_INITDIALOG or say when user press some button in dialog
basic code:
struct CopyDlg
{
enum {
WM_COPYRESULT = WM_APP, WM_PROGRESS
};
struct ProgressData {
LARGE_INTEGER TotalFileSize;
LARGE_INTEGER TotalBytesTransferred;
LARGE_INTEGER StreamSize;
LARGE_INTEGER StreamBytesTransferred;
DWORD dwStreamNumber;
};
PCWSTR m_lpExistingFileName, m_lpNewFileName;
HWND m_hwnd, m_hwndFile, m_hwndStream;
ULONG m_shift;
LONG m_dwRefCount;
LONG m_hwndLock;
BOOL m_bCancel;
CopyDlg()
{
m_dwRefCount = 1;
}
void AddRef()
{
InterlockedIncrement(&m_dwRefCount);
}
void Release()
{
if (!InterlockedDecrement(&m_dwRefCount))
{
delete this;
}
}
void LockWnd()
{
InterlockedIncrement(&m_hwndLock);
}
void UnlockWnd()
{
if (!InterlockedDecrement(&m_hwndLock))
{
// want m_hwnd be valid at PostMessage(p->m_hwnd,) time
EndDialog(m_hwnd, 0);
}
}
void OnProgress(DWORD dwCallbackReason, ProgressData* p)
{
if (dwCallbackReason == CALLBACK_STREAM_SWITCH)
{
if (p->dwStreamNumber == 1)
{
m_shift = 0;
LONGLONG QuadPart = p->TotalFileSize.QuadPart;
while (QuadPart > MAXLONG)
{
m_shift++;
QuadPart >>= 1;
}
SendMessage(m_hwndFile, PBM_SETRANGE32, 0, (LPARAM)QuadPart);
SendMessage(m_hwndFile, PBM_SETPOS, 0, 0);
}
SendMessage(m_hwndStream, PBM_SETRANGE32, 0, (LPARAM)(p->StreamSize.QuadPart >> m_shift));
SendMessage(m_hwndStream, PBM_SETPOS, 0, 0);
}
else
{
SendMessage(m_hwndStream, PBM_SETPOS, (LPARAM)(p->StreamBytesTransferred.QuadPart >> m_shift), 0);
SendMessage(m_hwndFile, PBM_SETPOS, (LPARAM)(p->TotalBytesTransferred.QuadPart >> m_shift), 0);
}
}
ULONG StartCopy(PCWSTR lpExistingFileName, PCWSTR lpNewFileName)
{
m_bCancel = FALSE;
m_lpExistingFileName = lpExistingFileName;
m_lpNewFileName = lpNewFileName;
LockWnd();
AddRef();
if (HANDLE hThread = CreateThread(0, 0, reinterpret_cast<PTHREAD_START_ROUTINE>(CopyThread), this, 0, 0))
{
CloseHandle(hThread);
return NOERROR;
}
Release();
UnlockWnd();
return GetLastError();
}
static INT_PTR CALLBACK _DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_INITDIALOG)
{
SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)lParam);
}
if (CopyDlg* p = reinterpret_cast<CopyDlg*>(GetWindowLongPtrW(hwndDlg, DWLP_USER)))
{
p->AddRef();
INT_PTR r = p->DialogProc(hwndDlg, uMsg, wParam, lParam);
p->Release();
return r;
}
return 0;
}
INT_PTR DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_NCDESTROY:
Release();
break;
case WM_DESTROY:
m_bCancel = TRUE;
break;
case WM_COMMAND:
switch (wParam)
{
case MAKEWPARAM(IDCANCEL, BN_CLICKED):
m_bCancel = TRUE;
break;
}
break;
case WM_CLOSE:
m_bCancel = TRUE;
UnlockWnd();
ShowWindow(hwndDlg, SW_HIDE);//for not get wm_close twice
break;
case WM_COPYRESULT:
UnlockWnd();
// lParam == error code from CopyFileExW
DbgPrint("CopyFileExW=%u\n", (ULONG)lParam);
break;
case WM_PROGRESS:
OnProgress((DWORD)wParam, reinterpret_cast<ProgressData*>(lParam));
delete (void*)lParam;
break;
case WM_INITDIALOG:
AddRef();
m_hwnd = hwndDlg;
m_hwndStream = GetDlgItem(hwndDlg, IDC_PROGRESS1);
m_hwndFile = GetDlgItem(hwndDlg, IDC_PROGRESS2);
m_hwndLock = 1;
StartCopy(L"**", L"**");
break;
}
return 0;
}
static ULONG CALLBACK CopyThread(CopyDlg* p)
{
PostMessage(p->m_hwnd, WM_COPYRESULT, 0, CopyFileExW(
p->m_lpExistingFileName, p->m_lpNewFileName,
reinterpret_cast<LPPROGRESS_ROUTINE>(CopyProgressRoutine),
p, &p->m_bCancel, 0) ? NOERROR : GetLastError());
p->Release();
return 0;
}
static DWORD CALLBACK CopyProgressRoutine(
__in LARGE_INTEGER TotalFileSize,
__in LARGE_INTEGER TotalBytesTransferred,
__in LARGE_INTEGER StreamSize,
__in LARGE_INTEGER StreamBytesTransferred,
__in DWORD dwStreamNumber,
__in DWORD dwCallbackReason,
__in HANDLE /*hSourceFile*/,
__in HANDLE /*hDestinationFile*/,
__in_opt CopyDlg* p
)
{
switch(dwCallbackReason)
{
case CALLBACK_CHUNK_FINISHED:
case CALLBACK_STREAM_SWITCH:
if (ProgressData* data = new ProgressData)
{
data->TotalFileSize = TotalFileSize;
data->TotalBytesTransferred = TotalBytesTransferred;
data->StreamSize = StreamSize;
data->StreamBytesTransferred = StreamBytesTransferred;
data->dwStreamNumber = dwStreamNumber;
if (!PostMessage(p->m_hwnd, WM_PROGRESS, dwCallbackReason, (LPARAM)data))
{
delete data;
return PROGRESS_CANCEL;
}
}
break;
}
// for debugging
//Sleep(3000);
//MessageBoxW(0,0,0,0);
return PROGRESS_CONTINUE;
}
};
if (CopyDlg* p = new CopyDlg)
{
DialogBoxParamW((HINSTANCE)&__ImageBase, MAKEINTRESOURCE(IDD_DIALOG1), HWND_DESKTOP, CopyDlg::_DialogProc, (LPARAM)p);
p->Release();
}
I'm trying to use a Property Sheet in my Win32 DialogBox application so that I can get user input first, have it apply to my classes and then run the program with that user entered specfication.
Property Page seems good but I'm not sure if I'm mistaken.
Regardless, I'm trying to implement it and I'm having some trouble. I read the documentation but still I'm not getting.
I've managed to make the property pages (2 of them) first page has an edit box and a few combo boxes with OK, Cancel and a disabled Apply button. What I'm trying to do is..
A. Have the apply button enable when I add an int to the edit box
B. Figure out how to have that data get put into a variable.
I know how it works with my DialogBox window. I have WM_Command for all my IDC_ stuff I've put in it. But the property page, I don't know what the IDC are for it or how to call the EDIT box and listboxes I've put in it. Or how to have it recognize that they've been used to enable the apply button.
here is my properypage setup method
void propertyPages(HINSTANCE hInstance){
memset(m_psp, 0, sizeof(m_psp));
memset(&m_PropSheet, 0, sizeof(m_PropSheet));
m_psp[0].dwSize = sizeof(PROPSHEETPAGE);
m_psp[0].dwFlags = PSH_WIZARD;
m_psp[0].hInstance = hInstance;
m_psp[0].pszTemplate = (LPCWSTR) IDD_PROPPAGE_LARGE;
m_psp[0].pszTitle = L"Champ 1 Scenario";
m_psp[1].dwSize = sizeof(PROPSHEETPAGE);
m_psp[1].dwFlags = PSP_USETITLE;
m_psp[1].hInstance = hInstance;
m_psp[1].pszTemplate = (LPCWSTR) IDD_PROPPAGE_LARGE1;
m_psp[1].pszTitle = L"Champ 2 Scenario";
m_PropSheet.dwSize = sizeof(PROPSHEETHEADER);
m_PropSheet.dwFlags = PSH_PROPSHEETPAGE;
m_PropSheet.hInstance = hInstance;
m_PropSheet.pszCaption = L"Champion Level/Runes/Masteries";
m_PropSheet.nPages = 2;
m_PropSheet.nStartPage = 0;
m_PropSheet.ppsp = (LPCPROPSHEETPAGE) m_psp;
//SendMessage(GetParent(hDlg), PSM_CHANGED, IDD_PROPPAGE_LARGE, 0);
//PropSheet_Changed(PROPSHEETPAGE,IDD_PROPPAGE_LARGE);
PropertySheet(&m_PropSheet);
}
I call it first in
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
Any tips, tricks, pointers or advice? Maybe on the best way to get user data before the main application launches? I'm finding it tricky to have values set by user.
You can set the dialog procedure for a page using the pfnDlgProc member:
m_psp[0].dwSize = sizeof(PROPSHEETPAGE);
m_psp[0].dwFlags = PSH_WIZARD;
m_psp[0].hInstance = hInstance;
m_psp[0].pszTemplate = (LPCWSTR) IDD_PROPPAGE_LARGE;
m_psp[0].pszTitle = L"Champ 1 Scenario";
m_psp[0].pfnDlgProc = MyDialogProc;
where MyDialogProc is just a normal dialog procedure.
To set the state of the apply button, use the PropSheet_Changed/PropSheet_UnChanged macros.
for each page you need to create a diagloproc:
LRESULT CALLBACK IntPage1DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
UNREFERENCED_PARAMETER(wParam);
BOOL myCondition = 0;
LPNMHDR lpnm;
switch (uMsg) {
case WM_INITDIALOG:
break;
case WM_NOTIFY:
lpnm = (LPNMHDR)lParam;
switch (lpnm->code) {
case PSN_SETACTIVE:
if (myCondition) {
PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK | PSWIZB_NEXT);
}
else {
PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK);
}
break;
case PSN_WIZFINISH:
break;
case PSN_WIZBACK:
break;
case PSN_RESET:
break;
default:
break;
}
break;
}
return 0;
}
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.
In MFC, is there an Open Folder Dialog? That is, rather than choosing a filename, it chooses a folder name? Ideally, I'd like it to be the way Visual Studio does it when navigating for a "Project Location" (when creating a new project), which looks very much like a normal file dialog. But I could make do with one of the vertical tree sort of interfaces if the former doesn't exist.
This code will get you a open folder dialog (this was taken from somewhere on the web but I don't really know where).
CString szSelectedFolder = _T("");
// This is the recommended way to select a directory
// in Win95 and NT4.
BROWSEINFO bi;
memset((LPVOID)&bi, 0, sizeof(bi));
TCHAR szDisplayName[_MAX_PATH];
szDisplayName[0] = '\0';
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = _T("Select a folder");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
// Set the callback function
bi.lpfn = BrowseCallbackProc;
LPITEMIDLIST pIIL = ::SHBrowseForFolder(&bi);
TCHAR szReturnedDir[_MAX_PATH];
BOOL bRet = ::SHGetPathFromIDList(pIIL, (TCHAR*)&szReturnedDir);
if (bRet)
{
if (szReturnedDir != _T(""))
{
szSelectedFolder = szReturnedDir;
}
LPMALLOC pMalloc;
HRESULT HR = SHGetMalloc(&pMalloc);
pMalloc->Free(pIIL);
pMalloc->Release();
}
you'll also have to implement this callback function:
TCHAR szInitialDir[_MAX_PATH];
// Set the initial path of the folder browser
int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
// Look for BFFM_INITIALIZED
if (uMsg == BFFM_INITIALIZED)
{
SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)szInitialDir);
}
return 0;
}