使用自定义模板显示GetOpenFileName函数

时间:2015-12-05 16:28:18

标签: c winapi rc

当我尝试将一些自定义控件添加到“打开文件”对话框时,我遇到了问题。我使用下面显示的资源文件,但它无法正常工作。我认为某处有错误,但我无法纠正。 如何在“打开文件”对话框中显示自定义控件?

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

生成文件

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

1 个答案:

答案 0 :(得分:0)

还要为标志添加OFN_ENABLEHOOK。注意,这种看起来很丑的Windows版本比XP更新。使用CHAR szFile[MAX_PATH];                      - 巴马克·谢米拉尼