Code:
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include "Shlwapi.h" // necessary for PathFileExists()
#include <shlobj.h> // necessary for SHGetFolderPath()
using namespace std;
#define ID_FOLDERPATH1 1
#define ID_FOLDERPATH2 2
#define ID_FOLDERPATH3 3
#define ID_GETPATH 4
static HINSTANCE gInstance;
UINT controlMsgs[] = {ID_FOLDERPATH1,ID_FOLDERPATH2,ID_FOLDERPATH3};
// Functions List //
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void displayRecentPaths(HWND hwnd, UINT controlMsg[]);
void recordRecentPaths(HWND hwnd, UINT controlMsg[]);
string getFolderPath(HWND hwnd);
vector<string> readFile(string fileName);
void writeFile(string fileName, vector<string> content);
int WINAPI WinMain(HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = gInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "My Class";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// if registration of main class fails
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"My Class",
"Find Application Data",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,
NULL, NULL, gInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
HWND hEdit = CreateWindowEx(0,"Edit","path captured here...",WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,
10,10,170,20,hwnd,(HMENU)ID_FOLDERPATH1,gInstance,NULL);
HWND hEdit1 = CreateWindowEx(0,"Edit","path captured here...",WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,
10,35,170,20,hwnd,(HMENU)ID_FOLDERPATH2,gInstance,NULL);
HWND hEdit2 = CreateWindowEx(0,"Edit","path captured here...",WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,
10,60,170,20,hwnd,(HMENU)ID_FOLDERPATH3,gInstance,NULL);
HWND hButton = CreateWindowEx(0,"Button","Random",WS_VISIBLE|WS_CHILD|WS_BORDER|BS_PUSHBUTTON,
60,85,120,20,hwnd,(HMENU)ID_GETPATH,gInstance,NULL);
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam)) {
case ID_GETPATH:
{ // Find the local computers path to the APP DATA folder regardless of
// of which version of windows the local system is using
string folderPath;
if( SHGetFolderPath(NULL,CSIDL_APPDATA,NULL,
0,(LPTSTR)folderPath.c_str()) == S_OK )
{
HWND hEdit = GetDlgItem(hwnd,ID_FOLDERPATH1);
SetWindowText(hEdit,folderPath.c_str());
// The below uses the APP DATA path create & store a file in it.
// PathAppend(szPath, TEXT("New Doc.txt"));
// HANDLE hFile = CreateFile(szPath, ...);
}
}
break;
default:
break;
}
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default: return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
}
void displayRecentPaths(HWND hwnd,UINT controlMsg[])
{
// Post: Read data file for most recent directories accessed. If data file doesn't exist
// then we create it.
string appDataFile = getFolderPath(hwnd); // get file path
// if APP DATA file exists: write paths to text file; ELSE create APP DATA file
if (PathFileExists((LPTSTR)appDataFile.c_str()) == true) {
vector <string> paths = readFile(appDataFile);
// display most recent accessed directories in edit boxes
for (int i=0; i<paths.size(); i++) {
HWND hEdit = GetDlgItem(hwnd,controlMsg[i]);
SetWindowText(hEdit,(LPTSTR)paths.at(i).c_str());
}
paths.clear(); // erase paths data (VERY IMPORTANT)
}
else {
//HANDLE hFile = CreateFile((LPTSTR)appDataFile.c_str(), ...); // create file
}
}
void recordRecentPaths(HWND hwnd, UINT controlMsg[])
{
// Post: Saves the most recent folder directories accessed by application
HWND hEdit;
vector<string>paths; // SHOULD BE GLOBAL
// Get folder paths
for (int i=0; i<3; i++) {
int len = GetWindowTextLength(GetDlgItem(hwnd,controlMsg[i]));
// if window has text in it
if (len > 0) {
char buffer[len+2];
GetDlgItemText(hwnd,controlMsg[i],buffer,sizeof(buffer));
paths.push_back((string)buffer);
}
}
string appDataFile; // = getFolderPath;
// Save paths to folder
if (paths.size() > 0) {
writeFile(appDataFile,paths);
}
}
string getFolderPath(HWND hwnd)
{
// Post: Returns the path to specified folder
string folderPath;
if( SHGetFolderPath(NULL,CSIDL_APPDATA,NULL,0,(LPTSTR)folderPath.c_str()) == S_OK )
{
PathAppend((LPTSTR)folderPath.c_str(),("RandomPlaylistPathData.txt"));
return folderPath;
}
else {
MessageBox(hwnd,"Failed to retrieve path to APP DATA folder","Error",MB_OK|MB_ICONERROR);
}
}
vector<string> readFile(string fileName)
{
// Post: Reads a file & stores contents line by line in vector
ifstream infile;
vector<string> result; // vector to store contents of file in
string tempStr;
infile.open(fileName.c_str());
if (!infile) {
MessageBox(NULL,"Failed to read randomPlaylistPathData.txt file","Error",
MB_OK|MB_ICONERROR);
}
while (!infile.eof()) {
getline(infile,tempStr,'\n');
result.push_back(tempStr);
}
infile.close();
return result;
}
void writeFile(string fileName, vector<string> content)
{
// Post: Write vector to text file(fileName) line by line
ofstream outfile;
outfile.open((LPTSTR)fileName.c_str());
if (!outfile) {
MessageBox(NULL,"Failed to write to randomPlaylistPathData.txt file","Error",
MB_OK|MB_ICONERROR);
}
for (int i=0; i<content.size(); i++) {
outfile << content.at(i) << "\n";
}
outfile.close();
}