win32api呼叫開啟/儲存檔案對話方塊
阿新 • • 發佈:2019-02-17
演示:開啟一個文字檔案讀取其中的第一個整數
#include <Windows.h> #include <iostream> #include <fstream> using namespace std; int main() { TCHAR szFileName[MAX_PATH] = {}; OPENFILENAMEW openFileName = {}; openFileName.lStructSize = sizeof(OPENFILENAMEW); openFileName.nMaxFile = MAX_PATH;//這個必須設定,不設定的話不會出現開啟檔案對話方塊 openFileName.lpstrFilter = L"文字檔案(*.txt*)\0*.txt\0任何檔案(*.*)\0*.*\0"; openFileName.lpstrFile = szFileName; openFileName.nFilterIndex = 1; openFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if (GetOpenFileName(&openFileName))// 如果儲存則用GetSaveFileName() { MessageBox(nullptr, openFileName.lpstrFile, L"", MB_OK); std::ifstream fin(openFileName.lpstrFile); int a; fin >> a; fin.close(); wchar_t buffer[32] = {}; wsprintf(buffer, L"%d", a); MessageBox(nullptr, buffer, L"", MB_OK); } return 0; }