I wrote a c++ app using SDL2 to simulate an editBox. It worked ok untill i added a function to open and select a file on Windows explorer.
Precisely after i click "Open" on the file browser, i can not use TTF_OpenFont() anymore...
I am still able to use TextSprites i have declared at the initialisation but i can't change the string associate to them no more. And that's really annoying because my editBox have to display a string var in my main loop.
I've already checked my font path with debug break points and it didn't change (still the same absolute path) nor the font size.
I've tryed many things to solve this : use another .ttf, use another TTF_Font *var, ect
Also tryed to put my openfiledialog function in a separate thread, that didn't change anything, so i tryed to control the new thread with windows Events and then with Sdl_Event but had no luck.
I obviously spent hours and hours of testing searching the web for similar issues and found nothing but unsolved posts.
Here is the funtion that allows me to get the name of the file opened :
void CMain::changeDirectoryPath()
{
OPENFILENAME ofn;
TCHAR szFile[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.hwndOwner = NULL;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = TEXT("Text Files\0*.txt\0Any File\0*.*\0");
ofn.nFilterIndex = 1;
ofn.lpstrTitle = TEXT("Select dictionary");
ofn.lpstrInitialDir = L"data\\dictionary";
ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
if(GetOpenFileName(&ofn))
{
OutputDebugString(ofn.lpstrFile);
int cSize = WideCharToMultiByte (CP_ACP, 0, ofn.lpstrFile, wcslen(ofn.lpstrFile), NULL, 0, NULL, NULL);
string output(static_cast<size_t>(cSize), '\0');
WideCharToMultiByte (CP_ACP, 0, ofn.lpstrFile, wcslen(ofn.lpstrFile), reinterpret_cast<char*>(&output[0]), cSize, NULL, NULL);
cout<<output<<endl;
}
cdpOn = false;
}
And the one that used to change the text displayed on my TextSprite :
bool CDictionary::loadFromRenderedText(std::string textureText)
{
if(Message!=NULL)
{
SDL_DestroyTexture(Message);
Message = NULL;
TTF_CloseFont(font);
}
font = TTF_OpenFont(filePath.c_str(), policeSize);
if(!font)
{
cout<<"TTF_OpenFont: "<<TTF_GetError()<<endl;
return 0;
}
textSurface = TTF_RenderText_Solid(font, textureText.c_str(), textColor);
if(textSurface != NULL)
{
Message = SDL_CreateTextureFromSurface(renderer, textSurface);
if(Message==NULL)
{
printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
}
else
{
position.x=50;
position.y=50;
position.w=textSurface->w;
position.h=textSurface->h;
}
SDL_FreeSurface(textSurface);
}
else
{
printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
}
return Message != NULL;
}
At last i thought to add WxWidget in my project and use wxFileDialog to see if it solve the problem but i'm affraid mixing SDL2 and wxWidget will resort in a savage mess :-(
Does anybody knows why i can not reopen a tt_font after i select and open a file with GetOpenFileName()?
Or have suggestion to possibly solve this?
Thanks in advance
Comment under this functions MSDN page says "Current Working Directory is altered when a file is opened", which is exactly what you're describing. Revert it back with SetCurrentDirectory (query at launch with GetCurrentDirectory, once).
Another way would be not closing the font since you're using it quite often.
Related
There are many similar questions, but mine is a bit different. Mine involves a Windows GUI. I am using an open file dialog or an "OPENFILENAME". I want to get the dialog result as OK when the user clicks the OK button and then open a text encoded file. I have done it in Java, but the UI looks weird. So I am not sure whether or not people would like it. I want to learn C++ as well, so I need some help. I have a text box called "hWndEdit" in the WM_COMMAND message inside my program. After opening, the text in the file is supposed to be displayed in the textBox I have specified. I have defined the function in a header file with the following code:
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
void OpenFile()
{
OPENFILENAME ofn; // common dialog box structure
HWND hwnd = nullptr; // owner window
HANDLE hf; // file handle
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"NoteRecorder Notes\0(*.recnote)\0Text\0(*.txt)\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
if (GetOpenFileName(&ofn) == TRUE)
{
hf = CreateFile(ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE)NULL);
}
}
Then I call it from my WM_COMMAND message:
case WM_COMMAND:
switch (wParam)
{
case 12:
OpenFile();
break;
}
But when the user presses the OK button in the OPENFILENAME, it doesn't read any text from the file. How can I accomplish this?
And I want to write files with a save file dialog. Tell me how to do that as well.
The main purpose of the functions GetOpenFileName and GetSaveFileName is to provide you with the filename that the user selected. However, doing the actual File Input and Output is a different issue. You can use the standard C/C++ library for that or you can use the Windows API functions, such as CreateFile, ReadFile and WriteFile.
Since you seem to already be using the function CreateFile, it would make sense to call ReadFile after verifying that the function call to CreateFile succeeded (i.e. that it did not return INVALID_HANDLE_VALUE). After reading the data, you can add a terminating null character to the data (you should make sure that the memory buffer is large enough) and then pass it to the SetDlgItemText function (if the text box is in a dialog box) or SetWindowText function.
In my app (MFC, C++) I have a button that creates a PDF file in a path.
Now I want to create another button that will print the pdf starting from the path and choosing some option like orientation and number of copies... but I'm not able to do that...
I saw that CPrintDialog shows the default dialog of the printer but I'm not able to attach the PDF file using the path.
I saw also the
ShellExecute(NULL, L"print", L"C:\\Documents\\1.pdf", NULL, NULL, SW_SHOWNORMAL);
that works but in this way I cannot choose any parameter...
How I can use the CPrintDialog to print an existing PDF that is in a path?
You have to use ShellExecuteEx and verb printto to get more control over printing:
SHELLEXECUTEINFO ShellInfo;
ZeroMemory(&ShellInfo, sizeof(SHELLEXECUTEINFO));
ShellInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShellInfo.lpVerb = L"printto";
ShellInfo.lpFile = L"C:\\Documents\\1.pdf";
ShellInfo.lpParameters = szPrinter;
ShellInfo.nShow = SW_SHOWNORMAL;
ShellInfo.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS;
if(::ShellExecuteEx(&ShellInfo))
{
if((int)ShellInfo.hInstApp > 32)
{
if(ShellInfo.hProcess != NULL)
{
DWORD dwExitCode = STILL_ACTIVE;
while(dwExitCode == STILL_ACTIVE)
{
if(!::GetExitCodeProcess(ShellInfo.hProcess, &dwExitCode))
{
dwExitCode = 0;
}
}
::CloseHandle(ShellInfo.hProcess);
}
}
}
To get the printer name:
CPrintDialog dlg(TRUE);
if (dlg.DoModal() == IDOK)
{
CString sPrinterName = dlg.GetDeviceName();
}
I have solved with a workaround. Instead of use the ShellExecute, I draw all the thing that I want to print using a CDC object attached to the hDC of a CPrintDialog class. Rember to manage the size of the draw depending on printer DPI like here.
A snippet only to have an idea:
CPrintDialog printDialog(FALSE);
printDialog.GetDefaults();
printDialog.m_pd.Flags &= ~PD_RETURNDEFAULT;
DEVMODE* pDevMode = printDialog.GetDevMode();
pDevMode->dmFields = DM_ORIENTATION | DM_PAPERSIZE | DM_PRINTQUALITY ;
pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
pDevMode->dmPaperSize = DMPAPER_A4;
::GlobalUnlock(printDialog.m_pd.hDevMode);
if (printDialog.DoModal() == IDOK)
{
CDC* pDC = new CDC;
pDC->Attach(printDialog.m_pd.hDC);
pDCPDF->StartDoc(_T(""));
pDCPDF->StartPage();
// ...
//draw what you want
// ...
pDCPDF->EndPage();
pDCPDF->EndDoc(); //this starts the printer
pDCPDF->DeleteDC();
}
Hope to reach soon the reputation of 15 to vote the answer of other questions.
Thanks to Andrew Komiagin answers.
I have a windows application written in C++.
The application generates certain configuration files in a hidden directory.
I want to give user an option to open that directory from my application.
Clicking that option should open a windows explorer like dialog with an input directory location.
I spend time searching for a similar api, but end up with certain dialogs like "DlgDirListComboBoxW" or "GetOpenFileName" or "GetSaveFileName".
I am looking for an api to open normal Windows explorer like Dialog with an input directory location.
It would be really helpful if the api belongs to CommonDialogs section.
You can use the SHBrowseForFolder
It shows a dialog similar to this:
This is a example for how to use it:
BOOL GetFolder(LPCSTR folderpath,
LPCSTR szCaption,
HWND hOwner /*= NULL*/)
{
BOOL retVal = FALSE;
// The BROWSEINFO struct tells the shell
// how it should display the dialog.
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.ulFlags = BIF_USENEWUI;
bi.hwndOwner = hOwner;
bi.lpszTitle = szCaption;
// must call this if using BIF_USENEWUI
::OleInitialize(NULL);
// Show the dialog and get the itemIDList for the
// selected folder.
LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);
if(pIDL != NULL)
{
// Create a buffer to store the path, then
// get the path.
char buffer[_MAX_PATH] = {'\0'};
if(::SHGetPathFromIDList(pIDL, buffer) != 0)
{
// Set the string value.
folderpath = buffer;
retVal = TRUE;
}
// free the item id list
CoTaskMemFree(pIDL);
}
::OleUninitialize();
return retVal;
}
How about:
HWND hWndOwner = NULL;
ShellExecute(
hWndOwner,
_T("explore"),
_T("c:\\some\\path"),
NULL,
NULL,
SW_SHOWNORMAL);
You can set hWndOwner to your main window handle if you're so inclined and can choose from a variety of other options.
For more information and usage details, check out the MSDN page on ShellExecute.
I'm using the following function to open a dialog box, using the OPENFILENAME. Problem is, after opening the dialog box, and closing it, everything works fine, but when I exit my application then Windows says it crashed.
bool openDialog()
{
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd = NULL; // owner window
HANDLE hf; // file handle
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
//
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrTitle = "Open File";
ofn.lpstrFilter = "Custom File\0*.Cus\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
if (GetOpenFileName(&ofn) == true)
{
path = ofn.lpstrFile;
return true;
}
else
{
return false;
}
}
I noticed it was the dialog box because the crash only happens if I use it at runtime, meaning that when I close the application without opening the dialog box at some point, it exits successfully.
Anyways, as far as I researched, it is probably caused my a 'heap corruption' or something similar that I don't have much knowledge of, so when my application tries to close, it doesn't release memory as it should (?). If anyone could figure out a solution it'd be greatly appreciated.
Most likely the problem is to do with path if this is simply a const char * or char * declared outside the function.
When the openDialog function returns, both the ofn structure of type OPENFILENAME and character array szFile go out of scope (along with all the other local, stack-allocated variables) and are no longer valid. As a result, the character array that ofn.lpstrFile and consequently path point to is no longer valid.
You should allocate space for the file name outside the function and perform a copy with strcpy, strncpy or equivalent before returning from it. Another approach would be to move the szFile character array out of the function and into the containing class as a member variable. Either way, the scope of the character array containing the file name returned needs to extend beyond the lifetime of the execution of the openDialog function.
I have a program and when they drop files into it I want it to get the path show a messagebox "of the path" then delete it. Can anyone shed some light on how to do this?
First of all, you'll need a window that can accept dropped files. This is accomplished by setting the ExStyle of your window to WS_EX_ACCEPTFILES:
//Create a window.
hWnd = CreateWindowEx
WS_EX_ACCEPTFILES, // Extended possibilites for variation.
gsClassName, // Classname.
gsTitle, // Title caption text.
WS_OVERLAPPEDWINDOW, // Default window.
CW_USEDEFAULT, // X Position.
CW_USEDEFAULT, // Y position.
230, // Window starting width in pixils.
150, // Window starting height in pixils.
HWND_DESKTOP, // The window is a child-window to desktop.
(HMENU)NULL, // No menu.
hInstance, // Program Instance handler.
NULL // No Window Creation data.
);
Second, you'll need to handle the WM_DROPFILES message in your WinProc() callback.
if(uMessage == WM_DROPFILES)
{
HDROP hDropInfo = (HDROP)wParam;
char sItem[MAX_PATH];
for(int i = 0; DragQueryFile(hDropInfo, i, (LPSTR)sItem, sizeof(sItem)); i++)
{
//Is the item a file or a directory?
if(GetFileAttributes(sItem) &FILE_ATTRIBUTE_DIRECTORY)
{
//Delete all of the files in a directory before we can remove it.
DeleteDirectoryRecursive(sItem);
}
else {
SetFileAttributes(sItem, FILE_ATTRIBUTE_NORMAL); //Make file writable
//DeleteFile(sItem);
}
}
DragFinish(hDropInfo);
}
Third, you'll need a function that can remove all of the sub-directories and files from any directories that are dropped onto your dialog:
bool DeleteDirectoryRecursive(const char *sDir)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
char sPath[2048];
//Specify a file mask. *.* = We want everything!
sprintf(sPath, "%s\\*.*", sDir);
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
printf("Path not found: [%s]\n", sDir);
return false;
}
do
{
//Find first file will always return "."
// and ".." as the first two directories.
if(strcmp(fdFile.cFileName, ".") != 0
&& strcmp(fdFile.cFileName, "..") != 0)
{
//Build up our file path using the passed in
// [sDir] and the file/foldername we just found:
sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName);
//Is the entity a File or Folder?
if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
DeleteDirectoryRecursive(sPath); //Recursive call.
}
else{
printf("File: %s\n", sPath);
SetFileAttributes(sPath, FILE_ATTRIBUTE_NORMAL);
//DeleteFile(sPath);
}
}
}
while(FindNextFile(hFind, &fdFile)); //Find the next file.
FindClose(hFind); //Always, Always, clean things up!
SetFileAttributes(sDir, FILE_ATTRIBUTE_NORMAL);
//RemoveDirectory(sDir); //Delete the directory that was passed in.
return true;
}
Lastly, you'll need to be VERY CAREFUL with this snippet - it deletes files after all.