如題,GetOpenFileName 偶爾不彈檔案選擇框,程式卡死;
代碼如下:
OPENFILENAME ofn = { 0 };
ZeroMemory(&ofn,sizeof(ofn));
TCHAR strOpenFileNames[80*MAX_PATH] = { 0 };//用于接收檔案名
TCHAR szPath[MAX_PATH];
TCHAR szFileName[80*MAX_PATH];
ofn.lStructSize = sizeof(OPENFILENAME);//結構體大小
ofn.hwndOwner = GetForegroundWindow();
char type[MAX_PATH];
memset(type,0,sizeof(type));
string tmpType = "File Type";
memcpy(type,tmpType.c_str(),tmpType.length());
memcpy(type+tmpType.length()+1,fileType.c_str(),fileType.length());
ofn.lpstrFilter = TEXT("All File\0*.*\0Image
Files\0*.jpg;*.JPG;*.jpeg;*.JPEG;*.BMP;*.bmp;*.PNG;*.png;*.docx;*.xlsx;*.xls;*.txt;*.ppt;*.pptx\0\0");//設定過濾
ofn.lpstrFile = strOpenFileNames;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(strOpenFileNames);
ofn.lpstrInitialDir = NULL;
char buf[MAX_PATH] = "file selection";
ofn.lpstrTitle = TEXT(buf);//使用系統默認標題留空即可
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER |
OFN_ALLOWMULTISELECT;
if (GetOpenFileName(&ofn)) //偶爾運行到此處,不彈檔案選擇框,程式卡在這里
{}
uj5u.com熱心網友回復:
頂一下、表沉
uj5u.com熱心網友回復:
改用CFileDialog咋樣?GetOpenFileName這個函式用的人好像不是很多。uj5u.com熱心網友回復:
GetOpenFileNameThe GetOpenFileName function creates an Open common dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open.
BOOL GetOpenFileName(
LPOPENFILENAME lpofn // address of structure with initialization
// data
);
Parameters
lpofn
Pointer to an OPENFILENAME structure that contains information used to initialize the dialog box. When GetOpenFileName returns, this structure contains information about the user's file selection.
Return Values
If the user specifies a filename and clicks the OK button, the return value is nonzero. The buffer pointed to by the lpstrFile member of the OPENFILENAME structure contains the full path and filename specified by the user.
If the user cancels or closes the Open dialog box or an error occurs, the return value is zero. To get extended error information, call the CommDlgExtendedError function, which can return one of the following values:
CDERR_FINDRESFAILURE CDERR_NOHINSTANCE
CDERR_INITIALIZATION CDERR_NOHOOK
CDERR_LOCKRESFAILURE CDERR_NOTEMPLATE
CDERR_LOADRESFAILURE CDERR_STRUCTSIZE
CDERR_LOADSTRFAILURE FNERR_BUFFERTOOSMALL
CDERR_MEMALLOCFAILURE FNERR_INVALIDFILENAME
CDERR_MEMLOCKFAILURE FNERR_SUBCLASSFAILURE
Remarks
Beginning with Windows 95 and Windows NT version 4.0, the default Open dialog box provides user-interface features that are similar to the Windows Explorer. You can provide an OFNHookProc hook procedure for an Explorer-style Open dialog box. To enable the hook procedure, set the OFN_EXPLORER and OFN_ENABLEHOOK flags in the Flags member of the OPENFILENAME structure and specify the address of the hook procedure in the lpfnHook member.
Windows 95 and Windows NT continue to support the old-style Open dialog box for applications that want to maintain a user-interface consistent with the Windows 3.1 or Windows NT 3.51 user-interface. To display the old-style Open dialog box, enable an OFNHookProcOldStyle hook procedure and ensure that the OFN_EXPLORER flag is not set.
To display a dialog box that allows the user to select a directory instead of a file, call theSHBrowseForFolder function.
Windows CE: Not all members of the OPENFILENAME structure are defined in Windows CE. For information about the defined members, see the reference topic for the OPENFILENAME structure.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in commdlg.h.
Import Library: Use comdlg32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
See Also
Common Dialog Box Library Overview, Common Dialog Box Functions, CommDlgExtendedError, GetSaveFileName, OFNHookProc , OFNHookProcOldStyle, OPENFILENAME,SHBrowseForFolder
uj5u.com熱心網友回復:
留意下 ofn.hwndOwner = GetForegroundWindow();GetForegroundWindow 不一定是你的程式視窗
uj5u.com熱心網友回復:
這個是MFC的方法、我要用標準C/C++
uj5u.com熱心網友回復:
這個會影像檔案選擇框的彈出么?
我這個程式是一直在后臺運行的,運行了一段時間后,偶爾出現不彈檔案選擇框。
uj5u.com熱心網友回復:
我這呼叫沒有報錯、else的邏輯我這沒粘貼。錯誤碼也沒法獲取。
uj5u.com熱心網友回復:
GDI或句柄泄露?在任務管理器 行程 查看 選擇列 里面選擇:記憶體使用、虛擬記憶體大小、句柄數、執行緒數、USER物件、GDI物件
讓你的程式(行程)不退出,回圈執行主流程很多遍,越多越好,比如1000000次甚至無限回圈,記錄以上各數值,再隔至少一小時,越長越好,比如一個月,再記錄以上各數值。如果以上兩組數值的差較大或隨時間流逝不斷增加,則鐵定有對應資源的資源泄漏!
uj5u.com熱心網友回復:
你說的問題我也考慮到了,但是出問題的時候、我查看過GDI和句柄數,大概就是幾百,應該不是這個問題吧?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/12632.html
標籤:C++ 語言
下一篇:VS2019開發C語言的問題
