0
我嘗試將某些自定義控件添加到打開文件對話框時遇到問題。我使用下面顯示的資源文件,但它不工作。我認爲某處存在錯誤,但我無法糾正它。 如何在打開文件對話框中顯示自定義控件?使用自定義模板顯示GetOpenFileName函數
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
這就是你如何在XP上做到這一點。但在Vista和更高版本中有一個不同的界面。使用IFileDialog和IFileDialogCustomize。 –
**猜測**:對話框本身不可見嗎? **不猜測**:結合'WS_CHILD'和'WS_CAPTION'沒有任何意義,因爲你沒有製作MDI子窗口;你應該使用'DS_CONTROL'而不是'WS_CAPTION'。 – andlabs
還要將'OFN_ENABLEHOOK'添加到標誌。請注意,這種看起來很醜的Windows版本比XP更新。使用'CHAR szFile [MAX_PATH];' –