EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Customized open file dialog

File open dialog is a part of windows subsystem and comes with commdlg32.dll package. In normal senarios we pass the file path buffer and it returns the path which is selected by the user. However there are additional facilities can be given with this dialog. Additional GUI controls can be added using a resource template. A hook procedure can be added to handle the events from these custom controls.

Uses

Customization of file open dialog is helpful for applications like Photo editors, audio editors etc. Here are some common usage of custom dialogs.

  1. An audio application can add a play sound file for preview before opening
  2. Photo or graphics editor applications can add a thumnail preview of the file
  3. User can display metadata information like author, revision etc

Demo Application

Here is the demo and we have mainly these steps to make a custom open file dialog.

  1. Add a dialg bar child resource in the project
  2. Add necessary button, text, ActiveX or other GUI controls
  3. Give ID for each controls
  4. Add OFN_ENABLETEMPLATE and OFN_ENABLEHOOK for OPENFILENAME.Flags
  5. Add lpTemplateName = MAKEINTRESOURCE(<DIALOGBAR resource ID>)
  6. Add hook procedure and implement all the notify events needed
  7. Update the lpfnHook = hook procedure address

Here is the C++ source file followed by the resource script.

#include <windows.h>
#include <commdlg.h>
#include "resource.h"
#include <mmsystem.h>
UINT CALLBACK OfnHookProc(HWND hDlg, UINT uMsg, UINT wParam, LONG lParam)
{
  OFNOTIFY* pofNotify;
  HWND hTrueDlg;
10    TCHAR szPathName[_MAX_PATH] = { 0 };
11 
12    switch (uMsg)
13    {
14      case WM_COMMAND:
15      {
16        /* Play button is pressed */
17        if(LOWORD(wParam) == IDC_PLAYPREVIEW) { 
18          GetDlgItemText(hDlg,IDC_STATIC_HINT, szPathName,_MAX_PATH);
19          PlaySound(szPathName, NULL,SND_SYNC|SND_FILENAME); /*Play sound file */
20          return (TRUE);
21        }
22      }
23      case WM_NOTIFY:
24        pofNotify = (OFNOTIFY*)lParam;
25        switch (pofNotify->hdr.code)
26        {
27          /* Init open dialog */
28          case CDN_INITDONE:
29            return (TRUE);
30          /* File selection changed */
31          case CDN_SELCHANGE:
32            hTrueDlg = GetParent(hDlg);
33            SendMessage(hTrueDlg, CDM_GETFILEPATH, _MAX_PATH, (LONG)szPathName);
34            SetDlgItemText(hDlg, IDC_STATIC_HINT, szPathName);
35            return (TRUE);
36          /* Folder selection changed */
37          case CDN_FOLDERCHANGE:
38            hTrueDlg = GetParent(hDlg);
39            SendMessage(hTrueDlg, CDM_GETFOLDERPATH, _MAX_PATH, (LONG)szPathName);
40            SetDlgItemText(hDlg, IDC_STATIC_HINT, szPathName);
41            return (TRUE);
42          /* User selected open */
43          case CDN_FILEOK:
44            return TRUE;
45          default:
46            return FALSE;
47        }
48      default:
49        return (FALSE);
50    }
51  }
52  int APIENTRY WinMain(HINSTANCE hInstance,
53                       HINSTANCE hPrevInstance,
54                       LPSTR     lpCmdLine,
55                       int       nCmdShow)
56  {
57    OPENFILENAME ofn;
58    TCHAR szFilePath[260];
59    memset(&ofn,0,sizeof(ofn));
60    ofn.lStructSize = sizeof(ofn);
61    ofn.hwndOwner = NULL;
62    ofn.lpstrFilter = "Wave Audio*.wav";
63    ofn.Flags = OFN_EXPLORER |  /* chicago-style dialog box */
64      OFN_FILEMUSTEXIST |       /* can't choose non-existing file */
65      OFN_HIDEREADONLY |        /* hide "read-only" checkbox */
66      OFN_ENABLEHOOK |          /* use hook function */
67      OFN_ENABLETEMPLATE;       /* custom template resource */
68              
69    ofn.lpTemplateName = MAKEINTRESOURCE(IDD_DIALOGBAR);
70    ofn.hInstance = hInstance;
71    ofn.lpfnHook = OfnHookProc;
72    ofn.lpstrFile = szFilePath;
73    ofn.lpstrFile[0] = 0;
74    ofn.nFilterIndex = 1;
75    ofn.lpstrFileTitle = NULL;
76    ofn.nMaxFileTitle = 0;
77    ofn.lpstrInitialDir = NULL;
78    ofn.nMaxFile = 260;
79    ofn.lpstrTitle = "open a wave audio file";
80 
81    if(GetOpenFileName(&ofn)){
82      MessageBox(NULL,ofn.lpstrFile,"Your file", MB_OK);
83    }
84    return 0;
85  }

Resource template

customized file open resource template

/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DIALOGBAR DIALOGEX 0, 0, 269, 26
STYLE WS_CHILD
FONT 8, "MS Sans Serif"
BEGIN
    LTEXT           "Customized File open",IDC_STATIC_HINT,7,7,222,12,0,
                    WS_EX_STATICEDGE
    PUSHBUTTON      "Play",IDC_PLAYPREVIEW,231,7,31,12
END

Output

customized file open

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 14:48:57(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.