Từ 1 tới 8 trên tổng số 8 kết quả

Đề tài: Tạo hộp thoại open file trong VC++

  1. #1
    Ngày gia nhập
    09 2011
    Bài viết
    10

    Mặc định Tạo hộp thoại open file trong VC++

    Chào cả nhà, mình vừa mới bước chân vào con đường lập trình hướng đối tượng, nên còn gà hơn con gà.hic.
    Mình muốn hỏi các bạn là làm sao mà có thể đọc file .txt trong bộ nhớ nhưng ko dùng cách truyền thống là nhập tên file hay dùng đường dẫn đến file đó, mà dùng 1 hộp thoại để chọn file đó.
    Mình đã lên mạng tìm hiểu nhưng chỉ có trên MFC ( cái này mình ko biết về nó nên tịt mù luôn).
    ah project type của mình là win32 console app.. ak.
    mong các bạn chỉ giáo. thanks

  2. #2
    Ngày gia nhập
    09 2006
    Nơi ở
    /usr/share/.hack@
    Bài viết
    1,433

    Gợi ý: CFileDialog, còn lại tự tìm hiểu nốt
    None!

  3. #3
    Ngày gia nhập
    09 2011
    Bài viết
    10

    CFileDialog chỉ dùng trong MFC thôi mà. còn mình đang dùng là win32 console application mà.

  4. #4
    Ngày gia nhập
    07 2010
    Nơi ở
    chỗ kín
    Bài viết
    446

    msdn mới đổi giao diện, nhìn thích mắt thật.
    http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

  5. #5
    Ngày gia nhập
    09 2011
    Bài viết
    10

    Giao diện đó đổi lâu rồi mà.
    hic. có ai giúp mình với.

  6. #6
    Ngày gia nhập
    07 2010
    Nơi ở
    chỗ kín
    Bài viết
    446

    Mặc định Tạo hộp thoại open file trong VC++

    mình đưa link bạn không đọc à:

    Note Starting with Windows Vista, the Common File Dialog has been superseded by the Common Item Dialog when used to open a file. We recommend that you use the Common Item Dialog API instead of the Common File Dialog API. For more information, see Common Item Dialog.
    This topic describes sample code that displays an Open dialog box so that a user can specify the drive, directory, and name of a file to open. The sample code first initializes an OPENFILENAME structure, and then calls the GetOpenFileName function to display the dialog box.
    In this example, the lpstrFilter member is a pointer to a buffer that specifies two file name filters that the user can select to limit the file names that are displayed. The buffer contains a double-null terminated array of strings in which each pair of strings specifies a filter. The nFilterIndex member specifies that the first pattern is used when the dialog box is created.
    This example sets the OFN_PATHMUSTEXIST and OFN_FILEMUSTEXIST flags in the Flags member. These flags cause the dialog box to verify, before returning, that the path and file name specified by the user actually exist.
    The GetOpenFileName function returns TRUE if the user clicks the OK button and the specified path and file name exist. In this case, the buffer pointed to by the lpstrFile member contains the path and file name. The sample code uses this information in a call to the function to open the file.
    Although this example does not set the OFN_EXPLORER flag, it still displays the default Explorer-style Open dialog box. However, if you want to provide a hook procedure or a custom template and you want the Explorer user interface, you must set the OFN_EXPLORER flag.
    Note In the C programming language, a string enclosed in quotation marks is null-terminated.

    Visual C++ Code:
    1. OPENFILENAME ofn;       // common dialog box structure
    2. char szFile[260];       // buffer for file name
    3. HWND hwnd;              // owner window
    4. HANDLE hf;              // file handle
    5.  
    6. // Initialize OPENFILENAME
    7. ZeroMemory(&ofn, sizeof(ofn));
    8. ofn.lStructSize = sizeof(ofn);
    9. ofn.hwndOwner = hwnd;
    10. ofn.lpstrFile = szFile;
    11. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
    12. // use the contents of szFile to initialize itself.
    13. ofn.lpstrFile[0] = '\0';
    14. ofn.nMaxFile = sizeof(szFile);
    15. ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    16. ofn.nFilterIndex = 1;
    17. ofn.lpstrFileTitle = NULL;
    18. ofn.nMaxFileTitle = 0;
    19. ofn.lpstrInitialDir = NULL;
    20. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    21.  
    22. // Display the Open dialog box.
    23.  
    24. if (GetOpenFileName(&ofn)==TRUE)
    25.     hf = CreateFile(ofn.lpstrFile,
    26.                     GENERIC_READ,
    27.                     0,
    28.                     (LPSECURITY_ATTRIBUTES) NULL,
    29.                     OPEN_EXISTING,
    30.                     FILE_ATTRIBUTE_NORMAL,
    31.                     (HANDLE) NULL);


    //nhầm, ko phải đổi giao diện, đấy là trang khác.

  7. #7
    Ngày gia nhập
    09 2011
    Bài viết
    10

    oh thanks, nhưng mấy cái code này mình chưa nghe qua lần nào cả, thầy cũng chưa nói qua lần nào lun.hic/.
    chắc phải twj mò thôi. thanks

    ủa, làm sao mà vận dụng đc nhỉ. ai có ý tưởng gì ko.
    yêu cầu là, đọc file (.txt) rồi đưa vào chuỗi dùng giao diện đồ họa.hic.

    Trích dẫn Nguyên bản được gửi bởi pkthanh92 Xem bài viết
    mình đưa link bạn không đọc à:

    Note Starting with Windows Vista, the Common File Dialog has been superseded by the Common Item Dialog when used to open a file. We recommend that you use the Common Item Dialog API instead of the Common File Dialog API. For more information, see Common Item Dialog.
    This topic describes sample code that displays an Open dialog box so that a user can specify the drive, directory, and name of a file to open. The sample code first initializes an OPENFILENAME structure, and then calls the GetOpenFileName function to display the dialog box.
    In this example, the lpstrFilter member is a pointer to a buffer that specifies two file name filters that the user can select to limit the file names that are displayed. The buffer contains a double-null terminated array of strings in which each pair of strings specifies a filter. The nFilterIndex member specifies that the first pattern is used when the dialog box is created.
    This example sets the OFN_PATHMUSTEXIST and OFN_FILEMUSTEXIST flags in the Flags member. These flags cause the dialog box to verify, before returning, that the path and file name specified by the user actually exist.
    The GetOpenFileName function returns TRUE if the user clicks the OK button and the specified path and file name exist. In this case, the buffer pointed to by the lpstrFile member contains the path and file name. The sample code uses this information in a call to the function to open the file.
    Although this example does not set the OFN_EXPLORER flag, it still displays the default Explorer-style Open dialog box. However, if you want to provide a hook procedure or a custom template and you want the Explorer user interface, you must set the OFN_EXPLORER flag.
    Note In the C programming language, a string enclosed in quotation marks is null-terminated.

    Visual C++ Code:
    1. OPENFILENAME ofn;       // common dialog box structure
    2. char szFile[260];       // buffer for file name
    3. HWND hwnd;              // owner window
    4. HANDLE hf;              // file handle
    5.  
    6. // Initialize OPENFILENAME
    7. ZeroMemory(&ofn, sizeof(ofn));
    8. ofn.lStructSize = sizeof(ofn);
    9. ofn.hwndOwner = hwnd;
    10. ofn.lpstrFile = szFile;
    11. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
    12. // use the contents of szFile to initialize itself.
    13. ofn.lpstrFile[0] = '\0';
    14. ofn.nMaxFile = sizeof(szFile);
    15. ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    16. ofn.nFilterIndex = 1;
    17. ofn.lpstrFileTitle = NULL;
    18. ofn.nMaxFileTitle = 0;
    19. ofn.lpstrInitialDir = NULL;
    20. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    21.  
    22. // Display the Open dialog box.
    23.  
    24. if (GetOpenFileName(&ofn)==TRUE)
    25.     hf = CreateFile(ofn.lpstrFile,
    26.                     GENERIC_READ,
    27.                     0,
    28.                     (LPSECURITY_ATTRIBUTES) NULL,
    29.                     OPEN_EXISTING,
    30.                     FILE_ATTRIBUTE_NORMAL,
    31.                     (HANDLE) NULL);


    //nhầm, ko phải đổi giao diện, đấy là trang khác.
    Bạn có thể chú thích cho mình kỹ hơn về các dòng lênh đc ko. thanks

  8. #8
    Ngày gia nhập
    07 2010
    Nơi ở
    chỗ kín
    Bài viết
    446

    nói sơ sơ cho cậu hiểu thôi nhé,

    mấy cái hộp thoại chọn font, chọn mầu, hay là mở file save file gì đó , người ta gọi chung là common dialog, những dialog này dc xây dựng sẵn rồi, mình chỉ việc dùng thôi.

    muốn dùng nó thì bạn phải học cách dùng, mỗi dialog có các kiểu tham số riêng, muốn thành thạo thì bạn phải nhớ hết.

    lấy ví dụ là cái open dialog:
    Visual C++ Code:
    1. OPENFILENAME ofn;       // biến để quản lý open dialog của bạn
    2. char szFile[260];       // buffer để chứa tên file
    3. HWND hwnd;              // cửa sổ cha
    4. HANDLE hf;              // handle của file
    5.  
    6. // khởi tạo các giá trị
    7. ZeroMemory(&ofn, sizeof(ofn)); // đầu tiên cho biến này thành 0 hết
    8.  
    9. // dưới đây là các đối số bạn phải truyền trước khi mở hộp thoại open
    10. ofn.lStructSize = sizeof(ofn);
    11. ofn.hwndOwner = hwnd;
    12. ofn.lpstrFile = szFile;
    13. ofn.lpstrFile[0] = '\0';
    14. ofn.nMaxFile = sizeof(szFile);
    15.  
    16. // dòng này để quyết định bạn muốn hiện file gì trong dialog, ở đây là .TXT
    17. ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    18. ofn.nFilterIndex = 1;
    19. ofn.lpstrFileTitle = NULL;
    20. ofn.nMaxFileTitle = 0;
    21. ofn.lpstrInitialDir = NULL;
    22. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    23.  
    24. // Khởi tạo xong rồi, giờ hiển thị dialog
    25.  
    26. if (GetOpenFileName(&ofn)==TRUE)
    27.     hf = CreateFile(ofn.lpstrFile,
    28.                     GENERIC_READ,
    29.                     0,
    30.                     (LPSECURITY_ATTRIBUTES) NULL,
    31.                     OPEN_EXISTING,
    32.                     FILE_ATTRIBUTE_NORMAL,
    33.                     (HANDLE) NULL);

Các đề tài tương tự

  1. Lập trình C Lỗi C1083: Cannot open include file: 'complex.h': No such file or directory trong lập trình C?
    Gửi bởi nguyen_ndd trong diễn đàn Thắc mắc lập trình C/C++/C++0x
    Trả lời: 4
    Bài viết cuối: 30-03-2012, 12:13 PM
  2. Tạo hộp thoại open file trong MFC
    Gửi bởi ngochan011290 trong diễn đàn Thắc mắc lập trình Visual C++
    Trả lời: 10
    Bài viết cuối: 17-11-2010, 11:05 AM
  3. Làm sao để open file bằng cách double click vào file trong windows explorer?
    Gửi bởi Batchuoc_09 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 11
    Bài viết cuối: 18-07-2010, 01:12 PM
  4. Lỗi Open File trong lập trình C++?
    Gửi bởi rox_rook trong diễn đàn Nhập môn lập trình C/C++
    Trả lời: 2
    Bài viết cuối: 06-03-2008, 09:01 AM
  5. Mở Hộp Thoại Open File trong lập trình C#
    Gửi bởi thienthanit trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 8
    Bài viết cuối: 22-07-2007, 09:36 AM

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn