We are going to go a step or two further with populating our
combo box. In our last tutorial we populated our combo box when
we initialized our dialog box with an already existing group of names
and then we used our message box to display the name selected.
You can use the previous tutorial code and tweak it to the following
code or create a new dialog box with a fresh Combo Box. Good practice :)
Oh and by the way:
Message boxes and printf's (for console apps) are an excellent
way to trouble shoot your code. You can place them anywhere in your code
to test your output.
Here we will take a name entered by the user and save it to the hard drive
so that the name is populated in the combo box the next time the user runs the
program.
There will be a number of new additions here but by now you should be able to
follow the code and read the comments to understand :)
First a new thing - called #defines.
here is an example
#define SELECTION_Location "C:\\OldSchoolNames\\"
what this does is tells the compiler, everywhere SELECTION_Location is
- put "C:\\OldSchoolNames\\" there before compiling.
the compiler will replace SELECTION_Location with C:\\OldSchoolNames\\
before it compiles (all tho you will never see that reflected in code).
The benefit of this is, say you have 1000 lines of code - and you decide
the location for names should be C:\\Programs\\OldSchoolNames\\ instead of
C:\\OldSchoolNames\\. Instead of searching (and possibly missing) all your
functions that use C:\\OldSchoolNames\\ you could just change the
SELECTION_Location define to be C:\\Programs\\OldSchoolNames\\ .
#define SELECTION_Location "C:\\Programs\\OldSchoolNames\\"
make sense?
another use for them is for clarity in code.
#define SELECTION_START 1
#define SELECTION_QUIT 2
EndDialog( hwnd, SELECTION_START);
EndDialog( hwnd, SELECTION_QUIT );
Ok, follow the sample code starting at WinMain.
You see a new function, CreateDirectory( SELECTION_Location, NULL );
This will create a new directory at the location we specified with our
define above. If by chance you already have a directory using the name
"C:\\OldSchoolNames\\" then you should change the define to a safer
path :)
Below that function you can see we are going to use a loop now. Now when you enter
a name into the combo box and click OK the program will create a new folder with the
name entered and save it in our OldSchoolNames folder. The dialog box will then
reload and you will see the name you entered in the drop down :)
Moving up to our Dialog CALLBACK you will see that when the dialog box initializes
we are calling a function to enumerate through the name directory to populate
our Combo Box drop down.
I commented the code to help you follow along and you should make good use
of your help files !! Place the curser in front of FindFirstFile and FindNextFile and click
the F1 key to learn more about the functions used. And all the other functions :)
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#define SELECTION_Location "C:\\OldSchoolNames\\"
#define SELECTION_START 1
#define SELECTION_QUIT 2
#include <stdio.h>
#include <windows.h>
#include "resource.h"
void ListNames( char *pDirectory ,HWND passhwnd )
{
//this is a struct which is defined in the window header files
WIN32_FIND_DATA findFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char files[ 260 ];
char fullPath[ 260 ];
sprintf( files, "%s\\*", pDirectory );
//start searching this directory
//for any file or folder
hFind = FindFirstFile( files, &findFileData);
//Check to see if this is a
//file or a directory
if ( findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
//if it is a directory we want to
//ignore the "." and the ".." directory
//those both stand for current and previous
//directory and are used in DOS
if ( 0 != strcmp( findFileData.cFileName, "." ) )
{
if ( 0 != strcmp( findFileData.cFileName, ".." ) )
{
sprintf( fullPath, "%s\\%s", pDirectory, findFileData.cFileName );
//get the first name directory
SendDlgItemMessage(passhwnd, IDC_COMBO1, CB_ADDSTRING,
0,(LPARAM)findFileData.cFileName);
}
}
}
else
{
//if we had files inside our name directories
//we would get them here. Later perhaps :)
}
//search through the rest of the files
while (FindNextFile(hFind, &findFileData) != 0)
{
//if it is a directory print out
//in the IF statement
if ( findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//if it is a directory we want to
//ignore the "." and the ".." directory
//those both stand for current and previous
//directory and are used in DOS
if ( 0 != strcmp( findFileData.cFileName, "." ) )
{
if ( 0 != strcmp( findFileData.cFileName, ".." ) )
{
sprintf( fullPath, "%s\\%s", pDirectory, findFileData.cFileName );
//get the next and so on until !=0
SendDlgItemMessage(passhwnd, IDC_COMBO1, CB_ADDSTRING,
0,(LPARAM)findFileData.cFileName);
}
}
}
else
{
//if we had files inside our name directories
//we would get them here. Later perhaps :)
}
}
}
INT_PTR CALLBACK DialogMessages(
HWND hwnd,
UINT uMsg,
WPARAM wparam,
LPARAM lparam
)
{
if( uMsg == WM_INITDIALOG )
{
//call our new function to retrieve
//the name previously entered
ListNames( SELECTION_Location ,hwnd );
return TRUE;
}
if ( uMsg == WM_COMMAND )
{
if ( HIWORD(wparam) == BN_CLICKED )
{
if (LOWORD(wparam) == IDOK)
{
char name [260];
//this will check to see if
//you entered anything into the combo
//box before you pressed the Ok button
int result = GetDlgItemText( hwnd, IDC_COMBO1 ,name,260);
//if result is = to 0
//than you did not
if( result==0 )
{
//you will be yelled at here
EndDialog( hwnd, SELECTION_START );
MessageBox( NULL, "enter or choose a name",
"Operator Error", MB_ICONEXCLAMATION );
return TRUE;
}
char newName[260];
//take "C:\\OldSchoolNames\\" and add
//what the user entered to it
sprintf( newName, SELECTION_Location"%s", name );
//create a new directory inside the
//OldSchoolNames directory with the
//name entered
CreateDirectory( newName ,NULL );
//return to the dialog box
//remember when we return to the dialog box
//we call our ListNames function to
//populate the combo box with the new
//name entered
EndDialog( hwnd, SELECTION_START);
return TRUE;
}
else if (LOWORD(wparam) == IDCANCEL)
{
EndDialog( hwnd, SELECTION_QUIT );
return TRUE;
}
}
}
return FALSE;
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR pCmdLine,
int iCmdShow
)
{
//create a directory. We define SELECTION_Location
//above as "C:\\OldSchoolNames\\"
CreateDirectory( SELECTION_Location, NULL );
__w64 int selection;
//default our program to always start here
selection =SELECTION_START;
//anytime you click the ok button the program
//will return to the dialogbox
do
{
if ( selection == SELECTION_START )
{
selection = DialogBox(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_SECOND),NULL, DialogMessages);
}
}
//when the cancel button is clicked we
//break out of our loop and exit
while ( selection != SELECTION_QUIT );
return 0;
}