killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Fri Feb 15, 2008 4:52 pm Post subject: OpenFileDialog win32 c++ |
|
|
Recently a friend wanted to learn how to use the openfiledialog in win32. Since I had some free time, I've decided to make him a little program that demonstrates how to use it. Since there are people in this section of the forums learning c++, might as well share it here also. The goal of this program is to rename a file that a user selects. It is dialog based to keep the learning to a minimum and not to distract from the main point. Also, I was lazy when writing this and didn't feel like creating the window and other controls manually.
As you can see it isn't commented. This is because the MSDN does a better job explaining than me. I've provided links to the info this program pertains to. If you have any questions, feel free to ask. Especially if you don't understand my algorithm. Project is attached (you will need Visual Studio 2008).
| Code: |
#include <windows.h>
#include "resource.h"
WORD IndexExt = 0;
WORD IndexFile = 0;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
bool BOpenFile(char* FileDir, char* FileExt, char* FileName);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1), NULL, reinterpret_cast<DLGPROC>(DlgProc));
return 0;
}
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_INITDIALOG:
SetFocus(hWnd);
return true;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
switch (LOWORD(wParam))
{
case IDC_BTN_BROWSE:
{
char fileDir[MAX_PATH] = { 0 };
char ext[11] = { 0 };
char fileName[MAX_PATH] = { 0 };
if (BOpenFile(fileDir, ext, fileName))
{
SetDlgItemText(hWnd, IDC_EDIT_BROWSE, fileDir);
SetDlgItemText(hWnd, IDC_EDIT_EXT, ext);
SetDlgItemText(hWnd, IDC_EDIT_NAME, fileName);
}
else
{
}
return true;
}
case IDC_BTN_CHANGE:
{
char temp[MAX_PATH] = { 0 };
char fileDir[MAX_PATH] = { 0 };
char ext[11] = { 0 };
char fileName[MAX_PATH] = { 0 };
GetDlgItemText(hWnd, IDC_EDIT_BROWSE, fileDir, MAX_PATH);
GetDlgItemText(hWnd, IDC_EDIT_BROWSE, temp, MAX_PATH);
GetDlgItemText(hWnd, IDC_EDIT_EXT, ext, MAX_PATH);
GetDlgItemText(hWnd, IDC_EDIT_NAME, fileName, MAX_PATH);
if (fileDir[0] != 0 || ext[0] != 0 || fileName[0] != 0)
{
for (int i = IndexFile, x = 0;;i++,x++)
{
if (fileName[x] != 0)
fileDir[i] = fileName[x];
else
{
IndexExt = i;
break;
}
}
for (int i = IndexExt,x = 0;;i++,x++)
{
if (ext[x] != 0)
fileDir[i] = ext[x];
else
{
fileDir[i] = 0;
break;
}
}
if (MoveFile(temp, fileDir))
{
SetDlgItemText(hWnd, IDC_EDIT_BROWSE, fileDir);
MessageBox(hWnd, "Done", "Success", MB_ICONINFORMATION | MB_OK);
}
}
return true;
}
}
}
return true;
case WM_CLOSE:
EndDialog(hWnd, 0);
return true;
case WM_DESTROY:
PostQuitMessage(0);
return true;
}
return false;
}
bool BOpenFile(char* FileDir, char* FileExt, char* FileName)
{
OPENFILENAME ofdOpen;
char cFile[MAX_PATH] = { 0 };
HWND hOwner = GetForegroundWindow();
ZeroMemory(&ofdOpen, sizeof(OPENFILENAME));
ofdOpen.lStructSize = sizeof(OPENFILENAME);
ofdOpen.hwndOwner = hOwner;
ofdOpen.lpstrFilter = "Any\0*.*\0\0";
ofdOpen.lpstrFile = cFile;
ofdOpen.nMaxFile = MAX_PATH;
ofdOpen.lpstrTitle = "Browse";
ofdOpen.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofdOpen))
{
lstrcpy(FileDir, cFile);
IndexExt = ofdOpen.nFileExtension - 1;
IndexFile = ofdOpen.nFileOffset;
for (int i = IndexExt, x = 0;; i++, x++)
{
if (cFile[i] != 0)
FileExt[x] = cFile[i];
else
break;
}
for (int i = IndexFile, x = 0; i < IndexExt; i++,x++)
{
FileName[x] = cFile[i];
}
return true;
}
else
return false;
}
|
OPENFILENAME
GetOpenFileName
|
|