PDA

View Full Version : GetOpenFileName không hoạt động!



thuongshoo
09-03-2008, 18:32
Chào!
Tui nay rảnh rang làm lại cái hàm mở hộp thoai File. Bất ngờ là làm từ trưa tới giờ mà nó 0 hiện. Định xài các hằng lỗi để xem nó lỗi gì thì VC 6.0 không biết mấy hằng đó định nghĩa ở đâu! Thiệt tình phải nói học Vc++.60 dễ thiệt :gun_banda
Hàm mở hộp thoại File GetOpenFileName và các hằng lỗi đều nằm chung file .h nhưng Vc hiểu cái này mà không hiểu cái kia.
Mình có upload file source lên ! Bạn coi dùm mình nhe!
http://www.box.net/shared/9jvzms6gw4

pinochu
10-03-2008, 10:08
--------------------Configuration: filedialog - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
filedialog.cpp
D:\temp\filedialog\filedialog\filedialog.cpp(224) : error C2065: 'CDERR_FINDRESFAILURE' : undeclared identifier
D:\temp\filedialog\filedialog\filedialog.cpp(224) : error C2051: case expression not constant
D:\temp\filedialog\filedialog\filedialog.cpp(225) : error C2065: 'sprintf' : undeclared identifier
D:\temp\filedialog\filedialog\filedialog.cpp(227) : error C2065: 'CDERR_DIALOGFAILURE' : undeclared identifier
D:\temp\filedialog\filedialog\filedialog.cpp(227) : error C2051: case expression not constant
D:\temp\filedialog\filedialog\filedialog.cpp(230) : error C2065: 'CDERR_INITIALIZATION' : undeclared identifier
D:\temp\filedialog\filedialog\filedialog.cpp(230) : error C2051: case expression not constant
D:\temp\filedialog\filedialog\filedialog.cpp(235) : warning C4065: switch statement contains 'default' but no 'case' labels
Error executing cl.exe.

filedialog.exe - 7 error(s), 1 warning(s)

pinochu
10-03-2008, 10:14
bác chép đoạn code này ở đâu thế?

bác vô file filedialog.cpp, thêm vào 2 dòng include là nó chạy được

#include "stdio.h"
#include "cderr.h"

thuongshoo
10-03-2008, 10:15
Cảm ơn Pinochu đã test thử!
Thì như tui đã nói! Mấy cái hằng đó và hàm GetOpenFileName nằm chung 1 file #include <Commdlg.h>. Khổ là cái hàm GetOpenFileName thì nó hiểu còn các hằng thì không hiểu.
Pinochu thử bỏ nhóm lệnh switch rồi chạy thử chương trình thì cũng không thấy FileDialog đâu hết trơn. Tui cũng chả hiểu. Tiếc là mấy chương trình hồi xưa làm bị xóa hết trơn.


À! à! thì ra nó nằm trong #include "cderr.h". Sao tui coi MSDN thì nói là các hằng lỗi nằm trong <Commdlg.h>?
Pinochu coi cái hàm GetOpenFileName dùm nhe! Nguyên đống code liên quan thì tui chép từ MSDN 2001 ra đó!

pinochu
10-03-2008, 10:19
he he, đoạn code đó bị gì đó, chạy lên được cái cửa sổ, sau đó máy đứng luôn, và lỗi tùm lum, để tui coi thử.

thuongshoo
10-03-2008, 10:21
Đây là đống code từ msdn 2005(cũng không biết! đang xài trực tuyến)


OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
//
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//
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_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box.

if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);

pinochu
10-03-2008, 10:32
bên máy tui thì mở được windows, chạy tốt, có điều bấm vô exit thì bị treo. bác debug lại xem thử, thả breakpoint vô chỗ case IDM_EXIT: rồi bấm F5 để chạy debug.

thuongshoo
10-03-2008, 10:50
Kì vậy ta?máy tui là máy xp, bấm vô thì chỉ thấy màn hình chớp chớp rồi thôi!
Vậy pinochu post lên phần code dùm nhe! :)

pinochu
10-03-2008, 10:57
phần code là tui thêm vô 2 dòng include đó, vậy là xong, đâu có thêm gì nữa đâu. Mà bác biết debug không? Tui xài win2k server.

thuongshoo
10-03-2008, 11:06
vậy hả? tui hình như chưa khi nào debug với thằng VC. Đâu kì này làm thử xem :D

thuongshoo
11-03-2008, 08:15
Tối về coi lại MSDN kĩ hơn thì biết rồi!
Phải đặt 0 cho char szFile[260];

pinochu
11-03-2008, 10:40
uh, thực ra cũng hông cần, cứ gán đại 1 tên đường dẫn mặc định hay gì đó