C语言怎么调用windows,windows下C语言调用系统文件选择对话框

代码片段,在windows下用C语言调用文件选择对话框,以备忘

#define DEFAULT_DIR ""

char extraction_path[MAX_PATH] = DEFAULT_DIR;

/*

* Browse for a folder and update the folder edit box

* Will use the newer IFileOpenDialog if *compiled* for Vista or later

*/

void browse_for_folder(void) {

BROWSEINFOW bi;

LPITEMIDLIST pidl;

#if (_WIN32_WINNT >= 0x0600)// Vista and later

WCHAR *wpath;

size_t i;

HRESULT hr;

IShellItem *psi = NULL;

IShellItem *si_path = NULL;// Automatically freed

IFileOpenDialog *pfod = NULL;

WCHAR *fname;

char* tmp_path = NULL;

// Even if we have Vista support with the compiler,

// it does not mean we have the Vista API available

INIT_VISTA_SHELL32;

if (IS_VISTA_SHELL32_AVAILABLE) {

hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC,

&IID_IFileOpenDialog, (LPVOID)&pfod);

if (FAILED(hr)) {

dprintf("CoCreateInstance for FileOpenDialog failed: error %X", hr);

pfod = NULL;// Just in case

goto fallback;

}

hr = pfod->lpVtbl->SetOptions(pfod, FOS_PICKFOLDERS);

if (FAILED(hr)) {

dprintf("Failed to set folder option for FileOpenDialog: error %X", hr);

goto fallback;

}

// Set the initial folder (if the path is invalid, will simply use last)

wpath = utf8_to_wchar(extraction_path);

// The new IFileOpenDialog makes us split the path

fname = NULL;

if ((wpath != NULL) && (wcslen(wpath) >= 1)) {

for (i=wcslen(wpath)-1; i!=0; i--) {

if (wpath[i] == L'\\') {

wpath[i] = 0;

fname = &wpath[i+1];

break;

}

}

}

hr = (*pSHCreateItemFromParsingName)(wpath, NULL, &IID_IShellItem, (LPVOID)&si_path);

if (SUCCEEDED(hr)) {

if (wpath != NULL) {

hr = pfod->lpVtbl->SetFolder(pfod, si_path);

}

if (fname != NULL) {

hr = pfod->lpVtbl->SetFileName(pfod, fname);

}

}

safe_free(wpath);

hr = pfod->lpVtbl->Show(pfod, hMain);

if (SUCCEEDED(hr)) {

hr = pfod->lpVtbl->GetResult(pfod, &psi);

if (SUCCEEDED(hr)) {

psi->lpVtbl->GetDisplayName(psi, SIGDN_FILESYSPATH, &wpath);

tmp_path = wchar_to_utf8(wpath);

CoTaskMemFree(wpath);

if (tmp_path == NULL) {

dprintf("Could not convert path");

} else {

safe_strcpy(extraction_path, MAX_PATH, tmp_path);

safe_free(tmp_path);

}

} else {

dprintf("Failed to set folder option for FileOpenDialog: error %X", hr);

}

} else if ((hr & 0xFFFF) != ERROR_CANCELLED) {

// If it's not a user cancel, assume the dialog didn't show and fallback

dprintf("could not show FileOpenDialog: error %X", hr);

goto fallback;

}

pfod->lpVtbl->Release(pfod);

return;

}

fallback:

if (pfod != NULL) {

pfod->lpVtbl->Release(pfod);

}

#endif

INIT_XP_SHELL32;

memset(&bi, 0, sizeof(BROWSEINFOW));

bi.hwndOwner = hMain;

bi.lpszTitle = L"Please select the installation folder:";

bi.lpfn = browseinfo_callback;

// BIF_NONEWFOLDERBUTTON = 0x00000200 is unknown on MinGW

bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS |

BIF_DONTGOBELOWDOMAIN | BIF_EDITBOX | 0x00000200;

pidl = SHBrowseForFolderW(&bi);

if (pidl != NULL) {

CoTaskMemFree(pidl);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,打开文件选择对话框并读取文件可以使用以下步骤: 1. 引入头文件:`#include <stdio.h>`和`#include <windows.h>`。 2. 定义一个`OPENFILENAME`结构体,用于存储文件选择对话框的参数。 ``` OPENFILENAME ofn; char szFile[260]; ``` 3. 初始化`OPENFILENAME`结构体,设置文件选择对话框的参数。 ``` ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = NULL; ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = "All Files (*.*)\0*.*\0"; ofn.nFilterIndex = 1; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = NULL; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; ``` 4. 调用`GetOpenFileName`函数显示文件选择对话框,并获取用户选择文件名。 ``` if (GetOpenFileName(&ofn) == TRUE) { printf("Selected file: %s\n", szFile); } ``` 完整的代码示例: ``` #include <stdio.h> #include <windows.h> int main() { OPENFILENAME ofn; char szFile[260]; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = NULL; ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = "All Files (*.*)\0*.*\0"; ofn.nFilterIndex = 1; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = NULL; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if (GetOpenFileName(&ofn) == TRUE) { printf("Selected file: %s\n", szFile); FILE* fp = fopen(szFile, "r"); if (fp == NULL) { printf("Failed to open file!\n"); return 1; } char buffer[1024]; while (fgets(buffer, sizeof(buffer), fp) != NULL) { printf("%s", buffer); } fclose(fp); } else { printf("No file selected!\n"); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值