Chủ đề: cần hỏi về lits file
-
21-08-2015, 12:12 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Đọc mà mình không rõ mục đích của bạn, đưa ra vài cách mà có thể bạn cần.
Nếu là liệt kê các file có trong 1 thư mục thì bạn tham khảo mã sau :
Mã:#include<shlwapi.h> // Window Shell#pragma comment(lib,"shlwapi.lib") // Phân giải cho Shell// szFolder : Chuỗi đường dẫn thư mục// hList : Thẻ cửa sổ listbox để hiển thị các tập tinvoid EnumFile(PCHAR szFolder, HWND hList){ WIN32_FIND_DATA wfd; CHAR szFind[MAX_PATH * 2]; HANDLE hFind; wsprintf(szFind, "%s\\*.*", szFolder); // Tìm tất cả if (INVALID_HANDLE_VALUE != (hFind = FindFirstFile(szFind, &wfd))) { do { wsprintf(szFind, "%s\\%s", szFolder, wfd.cFileName); if (!PathIsDirectory(szFind)) // Chuỗi tìm thấy không phải là thư mục { // Cộng và điểm sáng tên tập tin vừa tìm được trong listbox SendMessage(hList, LB_SETCURSEL, SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)wfd.cFileName), 0); // Nếu muốn hiển thỉ đầy đủ có cả đường dẫn thì thay dòng trên bằng dòng bên dưới // SendMessage(hList, LB_SETCURSEL, SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)szFind), 0); } } while (FindNextFile(hFind, &wfd)); // Tìm tên kế tiếp FindClose(hFind); // Chấm dứt tìm kiếm }}
Mã:void DoFileOpen(HWND hwnd, HWND hEdit){ OPENFILENAME ofn; char szFileName[MAX_PATH]; szFileName[0] = '/0'; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrDefExt = "txt"; if (GetOpenFileName(&ofn)) { HANDLE hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE != hFile) { DWORD dwFileSize = GetFileSize(hFile, NULL); if (dwFileSize != INVALID_FILE_SIZE) { PBYTE psz = (PBYTE)malloc(dwFileSize + 1); if (psz) { DWORD dwRead; BOOL bFlag = ReadFile(hFile, psz, dwFileSize, &dwRead, NULL); if (bFlag && dwFileSize == dwRead) { psz[dwFileSize] = '\0'; SetWindowText(hEdit, (LPSTR)psz); } free(psz); } } CloseHandle(hFile); } }}
Về mã của bạn, mình vẫn thấy bạn chưa sửa lại lỗi bước qua trong switch-case đâu nhé. [IMG]images/smilies/clap_grin.gif[/IMG][IMG]images/smilies/clap_grin.gif[/IMG][IMG]images/smilies/clap_grin.gif[/IMG]View more random threads:
- Đưa chương trình lên man hình nền
- capCaptureStop VFW ghi hình camera trong MFC?
- cách bắt phím nhấn trên windows
- Chỉ lệnh #ifdef WINNT trên VC++
- mọi người cho em hỏi với ạ hook messagebox
- inject DLL bằng SetWindowHookEx
- Làm sao để hook khi đã có hWnd?
- convert một mảng sang LPCWSTR trong API
- Làm sao capture message của listview bên ngoài từ ứng dụng của mình ?
- Cách Trình Bày code Theo Chuẩn Charles Simonyi
-
21-08-2015, 02:27 PM #2Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Gửi bởi MHoang
ý mình là nếu mình muốn sử dụng các thư viện của "stdio.h" như fopen(), fread() thì hàm void DoFileOpen(HWND) củmififnh nên chỉnh lại như thế nào? [IMG]images/smilies/tongue.png[/IMG]
-
21-08-2015, 04:39 PM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
cần hỏi về lits file
chào mn. mình đang code 1 chương trình đơn giản:
liệt kê các file có trong 1 thư mục:
đoạn code mình đây:
Mã:#include <Windows.h> #define IDC_MAIN_EDIT 101 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void AddMenus(HWND); #define IDM_FILE_open 1 #define IDM_FILE_exit 2 void DoFileOpen(HWND hwnd) { OPENFILENAME ofn; char szFileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrDefExt = "txt"; if (GetOpenFileName(&ofn)) { HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); } } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc = { 0 }; HWND hwnd; MSG msg; wc.lpszClassName = TEXT("Window"); wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(NULL, IDC_ARROW); RegisterClass(&wc); hwnd = CreateWindow(wc.lpszClassName, TEXT("HUY"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 150, NULL, NULL, hInstance, NULL); while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: { AddMenus(hwnd); break; } case WM_COMMAND: { switch (LOWORD(wParam)) { case IDM_FILE_open: DoFileOpen(hwnd); break; case IDM_FILE_exit: PostQuitMessage(0); break; } } case WM_DESTROY: { PostQuitMessage(0); break; } } return DefWindowProc(hwnd, msg, wParam, lParam); } void AddMenus(HWND hwnd) { HMENU hMenubar; HMENU hMenu; hMenubar = CreateMenu(); hMenu = CreateMenu(); AppendMenu(hMenu, MF_STRING, IDM_FILE_open, TEXT("open")); AppendMenu(hMenu, MF_STRING, IDM_FILE_exit, TEXT("exit")); AppendMenu(hMenu, MF_SEPARATOR, 0, NULL); AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, TEXT("file")); SetMenu(hwnd, hMenubar); }
nếu có ví dụ cụ thể thì tốt quá
thanks
-
22-08-2015, 06:57 PM #4Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Vài giả sử cho bạn :
Mã:if (GetOpenFileName(&ofn)) { FILE * fp = fopen(szFileName, "r"); /* Thực hiện các công việc nào đó mà bạn muốn như : fread, fgetc, feof, v.v... */ fclose(fp); }
_ Trong C : printf, scanf, các hàm nhập xuất tập tin,...
_ Trong C++ : cout, cin, ...
Windows API thừa đồ chơi cho mấy việc này và nó còn được viết để tạo nhiều thuận lợi cho bạn hết mức.
Bạn nên tham khảo trong MSDN để biết thêm, từ khóa "Windows API". Chúc bạn học tốt.
-
23-08-2015, 05:48 PM #5Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Gửi bởi MHoang
kết quả như sau:
cụ thể: mình nhập thư mục vào ô path:
kích vào button "start" để tiến hành chạy.
nhưng khi nhập "c:" thì kết quả không chạy ở ô bên dưới
đoạn code mình đây:
Mã:#include <Windows.h> #include <stdio.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HWND TEXTBOX1; HWND TEXTBOX2; void list_files(const char* parent) { WIN32_FIND_DATA fd; char folder[MAX_PATH]; wsprintf(folder, "%s\\*.*", parent); HANDLE hFind = FindFirstFile(folder, &fd); if (hFind != INVALID_HANDLE_VALUE) { do { if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")) { wsprintf("%s\\%s ", parent, fd.cFileName); char child[MAX_PATH]; wsprintf(child, "%s\\%s", parent, fd.cFileName); list_files(child); } } else { wsprintf("%s ", fd.cFileName); } } while (FindNextFile(hFind, &fd)); FindClose(hFind); } } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hwnd; WNDCLASS wc; wc.lpszClassName = TEXT("HUY"); wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.style = CS_HREDRAW | CS_VREDRAW; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpszMenuName = NULL; RegisterClass(&wc); hwnd = CreateWindow(wc.lpszClassName, TEXT("HUY"), WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_MAXIMIZE, 300, 300, 750, 700, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: { CreateWindow(TEXT("STATIC"), TEXT("Path:"), WS_CHILD | WS_VISIBLE, 450, 50, 100, 25, hwnd, NULL, NULL, NULL); TEXTBOX1= CreateWindow(TEXT("edit"), TEXT(""), WS_BORDER | WS_CHILD | WS_VISIBLE, 500, 45, 120, 25, hwnd, (HMENU)1, NULL, NULL); CreateWindow(TEXT("STATIC"), TEXT("List File"), WS_CHILD | WS_VISIBLE, 400, 150, 100, 25, hwnd, NULL, NULL, NULL); TEXTBOX2= CreateWindow(TEXT("EDIT"), TEXT(""), WS_CHILD | WS_BORDER | WS_VISIBLE, 400, 170, 500, 500, hwnd, (HMENU)2, NULL, NULL); CreateWindow(TEXT("BUTTON"), TEXT("START"), WS_CHILD | WS_BORDER | WS_VISIBLE, 500, 100, 100, 25, hwnd, (HMENU)3, NULL, NULL); return 0; } case WM_COMMAND: { if (LOWORD(wParam) == 3) { char szText[MAX_PATH]; int iLength = GetWindowTextLength(TEXTBOX1); if (iLength <= 0) { MessageBox(hwnd, TEXT("Error or text is NULL"), TEXT("Huy"), MB_OK | MB_ICONEXCLAMATION); return 0; } else { GetWindowText(TEXTBOX1, szText, iLength + 1); // Nhận chuỗi vào buffer list_files(szText); SetWindowText(TEXTBOX2,szText); } return 0; } } case WM_CLOSE: { DestroyWindow(0); break; } case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hwnd, msg, wParam, lParam); }
thank
-
24-08-2015, 12:32 PM #6Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
helpppppppppppppppppppppppppppppppppp
-
24-08-2015, 02:33 PM #7Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Mã:void list_files(const char* parent){ WIN32_FIND_DATA fd; char folder[MAX_PATH]; wsprintf(folder, "%s\\*.*", parent); HANDLE hFind = FindFirstFile(folder, &fd); if (hFind != INVALID_HANDLE_VALUE) { do { if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")) { wsprintf("%s\\%s ", parent, fd.cFileName); // Lỗi ở đây //////////////////////////// char child[MAX_PATH]; wsprintf(child, "%s\\%s", parent, fd.cFileName); list_files(child); } } else { wsprintf("%s ", fd.cFileName); // Lỗi ở đây ///////////////////////////// } } while (FindNextFile(hFind, &fd)); FindClose(hFind); }}
-
25-08-2015, 04:14 PM #8Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Gửi bởi MHoang
Mã:#include <Windows.h> #include <stdio.h> #include <CommCtrl.h> #include<shlwapi.h> // Window Shell #pragma comment(lib,"shlwapi.lib") // Phân giải cho Shell HINSTANCE g_hInst; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HWND TEXTBOX1; HWND hwndlistview; PCHAR szFolder; HWND hList; HWND CreateListView(HWND hwndParent); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hwnd; WNDCLASS wc; wc.lpszClassName = TEXT("HUY"); wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.style = CS_HREDRAW | CS_VREDRAW; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpszMenuName = NULL; RegisterClass(&wc); hwnd = CreateWindow(wc.lpszClassName, TEXT("HUY"), WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_MAXIMIZE, 300, 300, 750, 700, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: { hwndlistview = CreateListView(hwnd); CreateWindow(TEXT("STATIC"), TEXT("Path:"), WS_CHILD | WS_VISIBLE, 450, 50, 100, 25, hwnd, NULL, NULL, NULL); TEXTBOX1= CreateWindow(TEXT("edit"), TEXT(""), WS_BORDER | WS_CHILD | WS_VISIBLE, 500, 45, 120, 25, hwnd, (HMENU)1, NULL, NULL); CreateWindow(TEXT("STATIC"), TEXT("List File"), WS_CHILD | WS_VISIBLE, 400, 150, 100, 25, hwnd, NULL, NULL, NULL); CreateWindow(TEXT("BUTTON"), TEXT("START"), WS_CHILD | WS_BORDER | WS_VISIBLE, 500, 100, 100, 25, hwnd, (HMENU)3, NULL, NULL); return 0; } case WM_COMMAND: { if (LOWORD(wParam) == 3) { int iLength = GetWindowTextLength(TEXTBOX1); WIN32_FIND_DATA wfd; CHAR szFind[MAX_PATH * 2]; HANDLE hFind; wsprintf(szFind, "%s\\*.*", szFolder); GetWindowText(TEXTBOX1, szFolder, iLength + 1); if (INVALID_HANDLE_VALUE != (hFind = FindFirstFile(szFind, &wfd))) { if (!PathIsDirectory(szFind)) // Chuỗi tìm thấy không phải là thư mục { SendMessage(hList, LB_SETCURSEL, SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)szFind), 0); } } FindClose(hFind); // Chấm dứt tìm kiếm SetWindowText(hwndlistview, szFolder); } } case WM_CLOSE: { DestroyWindow(0); break; } case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hwnd, msg, wParam, lParam); } HWND CreateListView(HWND hwndParent) { RECT rcClient; //dimensions of client area HWND hwndLV; //handle to list-view control //Get the dimensions of the parent window's client area, and create the list-view control GetClientRect(hwndParent, &rcClient); hwndLV = CreateWindow(WC_LISTVIEW, TEXT(""), WS_CHILD | LVS_REPORT | LVS_EDITLABELS|WS_VISIBLE|WS_BORDER, 150,180, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, hwndParent, (HMENU)3, g_hInst, NULL); return hwndLV; }
đề bài hiển thị tất cả các file trong 1 thư mục. lần trước mình nhầm sang đọc file .TXT hì
Rocket 1h là thực phẩm chức năng giúp tăng cường sinh lý nam có nguồn gốc hoàn toàn từ dược liệu tự nhiên. Rất nhiều nam giới đã tuyển lựa Rocket 1h như một phương thuốc hữu hiệu để cải thiện tình...
Nguyên nhân của cặp đôi nam nữ mua...