0

I have a problem when I tried to add some custom controls to the Open File Dialog. I used the resource file shown below but it is not working. I think that there is an error somewhere but I can't correct it. How to show custom controls on the Open File Dialog ?

program.c

#include "resource.h"

int WINAPI WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow )
{
    OPENFILENAME ofn;
    CHAR szFile[50];

    ZeroMemory( &ofn , sizeof(ofn));

    ofn.lStructSize = sizeof ( ofn );
    ofn.hwndOwner = NULL;
    ofn.hInstance = hInstance;
    ofn.lpstrFile = szFile ;
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = "All\0*.*\0Text\0*.txt\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL ;
    ofn.nMaxFileTitle = 0 ;
    ofn.lpstrInitialDir = NULL ;
    ofn.Flags = OFN_ENABLETEMPLATE | OFN_EXPLORER; 
    ofn.lpfnHook = NULL;
    ofn.lpTemplateName = MAKEINTRESOURCE( MY_DIALOG );

    GetOpenFileName( &ofn );

    return 0;
}

dialog.rc

#include "resource.h"

MY_DIALOG  DIALOG   0, 0, 260, 30
STYLE WS_CHILD | WS_CAPTION
CAPTION "My Dialog"
FONT 8, "MS Sans Serif"
{
   CONTROL "", 1119, "STATIC", SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 0, 0, 265, 10 
   CONTROL "Items:", 3221, "STATIC", SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 5, 13, 45, 9 
   CONTROL "", 3202, "COMBOBOX", CBS_DROPDOWN | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL | WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP, 54, 11, 218, 63 
   CONTROL "Current index:", 3221, "STATIC", SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 5, 31, 45, 9 
   CONTROL "", 3203, "COMBOBOX", CBS_DROPDOWN | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL | WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP, 54, 29, 204, 63 
   CONTROL "...", 3204, "BUTTON", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 260, 29, 12, 12 
}

resource.h

#ifndef RESOURCE_H
#define RESOURCE_H

#include <windows.h>
#define MY_DIALOG  120

#endif //RESOURCE_H

Makefile

build:
    windres -i dialog.rc -o dialog.o
    gcc -c program.c -o program.o
    gcc -o program.exe dialog.o program.o -lcomdlg32 -lgdi32
Salah Alami
  • 33
  • 2
  • 8
  • This is how you do it on XP. But on Vista and later there is a different interface. Use IFileDialog and IFileDialogCustomize. – David Heffernan Dec 05 '15 at 16:57
  • 1
    **Guessing**: Doesn't the dialog itself have to be visible? **Not guessing**: combining `WS_CHILD` and `WS_CAPTION` doesn't make any sense since you're not making an MDI child window; you should just use `DS_CONTROL` instead of `WS_CAPTION`. – andlabs Dec 05 '15 at 17:46
  • 1
    Also add `OFN_ENABLEHOOK` to flags. Note, this kind of looks ugly on windows versions which are newer than XP . Use `CHAR szFile[MAX_PATH];` – Barmak Shemirani Dec 05 '15 at 17:55
  • Yes It worked now, I forgot the OFN_ENABLEHOOK Flag. Thank you guys. – Salah Alami Dec 05 '15 at 18:10
  • Do you notice how odd the dialog looks? Compare with the dialog from other programs. Don't you want that dialog? – David Heffernan Dec 06 '15 at 04:49

1 Answers1

0

Also add OFN_ENABLEHOOK to flags. Note, this kind of looks ugly on windows versions which are newer than XP . Use CHAR szFile[MAX_PATH]; – Barmak Shemirani

Armali
  • 18,255
  • 14
  • 57
  • 171