Open a file dialog in c and win32

This simple function shows how to open a file with file dialog, the code have comments for easy undestand, in case you don’t get it please leave comment and i will get back at you asap.

void openFileName(HWND hwnd) //parameter from the caller our window 
{
   OPENFILENAME ofn; //struct openfilename
   char *filter; // filters for thr file dialog
   char *filename; // to old the filename
    
    ofn.lStructSize	   = sizeof(OPENFILENAME); //reserv memory
    ofn.hwndOwner	   = hwnd; //owner of this file opener (main window)
    ofn.nMaxCustFilter = filter; // filters if you need them
    ofn.nFilterIndex   = 1; //index 
    ofn.lpstrFile	   = file_name; //filename
    ofn.nMaxFile = BUFF_MAX; // 256
    ofn.lpstrInitialDir   = NULL; //no iitial dir but it could be one
    ofn.lpfnHook	   = NULL;
    ofn.lpstrFileTitle = NULL; //
    ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR //Flags
						| OFN_HIDEREADONLY;

if (GetOpenFileName(&ofn)==TRUE) { //if everything went fine this should be true and if so it opens the file dialog
       HANDLE hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
            0, (LPSECURITY_ATTRIBUTES) NULL,
            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
            (HANDLE) NULL);
}
 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s