VC2008 建立Windows API程序,怎么把已有的txt文件的内容全部显示在messagebox里?
展开全部
给你个命令行版的例子,可直接复制过去使用。
#include <windows.h>
#include <tchar.h>
int main()
{
HANDLE hFile;
TCHAR szPath[MAX_PATH] = TEXT("E:\\info.txt");
hFile = CreateFile(szPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
DWORD dwSize;
dwSize = GetFileSize(hFile, NULL);
if (dwSize > 0) {
LPTSTR lpStr;
lpStr = (LPTSTR)GlobalAlloc(GPTR, dwSize + 1);
if (lpStr != NULL) {
DWORD dwRead;
if (ReadFile(hFile, lpStr, dwSize, &dwRead, NULL)) {
MessageBoxA(GetActiveWindow(), lpStr, "提示", MB_OK);
}
GlobalFree(lpStr);
}
}
CloseHandle(hFile);
}
return 0;
}
#include <windows.h>
#include <tchar.h>
int main()
{
HANDLE hFile;
TCHAR szPath[MAX_PATH] = TEXT("E:\\info.txt");
hFile = CreateFile(szPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
DWORD dwSize;
dwSize = GetFileSize(hFile, NULL);
if (dwSize > 0) {
LPTSTR lpStr;
lpStr = (LPTSTR)GlobalAlloc(GPTR, dwSize + 1);
if (lpStr != NULL) {
DWORD dwRead;
if (ReadFile(hFile, lpStr, dwSize, &dwRead, NULL)) {
MessageBoxA(GetActiveWindow(), lpStr, "提示", MB_OK);
}
GlobalFree(lpStr);
}
}
CloseHandle(hFile);
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询