//////.rc
MAIN MENU DISCARDABLE
BEGIN
POPUP "File "
BEGIN
MENUITEM "&Open",101
END
MENUITEM "&Chart", 102
MENUITEM "&About", 103
MENUITEM "&Quit", 104
END
////
#include <windows.h>
#include <windowsx.h>
#include<stdlib.h>
#define MI_OPEN 101
#define MI_CHART 102
#define MI_ABOUT 103
#define MI_QUIT 104
float nCurrentScales=1;
short nBuf[500];
short nBufScaled[500];
BOOL bDataOK=FALSE;
char szFile[128];
char szFilter[]="Data Files(*.DAT)\0*.dat\0All Files(*.*)\0*.*\0";
OPENFILENAME ofn;
DWORD nCnt;
void OnDestroy(HWND hwnd)
{
// MessageBox(hwnd,"The end","Exit",MB_OK);
PostQuitMessage(0);
}
void OnCommand(HWND hwnd, int id, HWND, UINT)
{
InvalidateRect(hwnd,NULL,TRUE);
switch(id)
{
case MI_OPEN:
// MessageBox(hwnd,"Opening File ","Command",MB_OK);
szFile[0] = 0;
ZeroMemory(&ofn,sizeof(OPENFILENAME));
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hwnd;
ofn.lpstrCustomFilter=szFilter;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
if(GetOpenFileName(&ofn))
{
HANDLE hFile=CreateFile(szFile,GENERIC_READ,0,0,OPEN_EXISTING,0,NULL);
if(hFile==INVALID_HANDLE_VALUE)break;
ReadFile(hFile,nBuf,2*500,&nCnt,NULL);
CloseHandle(hFile);
bDataOK=TRUE;
for(int i=0;i<500;i++)
nBufScaled=nBuf*nCurrentScales;
InvalidateRect(hwnd,NULL,TRUE);
break;
}
// else
break;
////
case MI_CHART:
MessageBox(hwnd,"Chart","Command",MB_OK);
break;
case MI_about :
MessageBox(hwnd," 1 ","About",MB_OK);
break;
case MI_QUIT:
DestroyWindow(hwnd);
break;
}
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
HANDLE_MSG(hwnd,WM_COMMAND,OnCommand);
HANDLE_MSG(hwnd,WM_DESTROY,OnDestroy);
default:
return(DefWindowProc(hwnd,msg,wParam,lParam));
}
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int)
{
char szClassName[]="MainWindow";
char szTitle[]="1";
MSG Msg;
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockBrush(LTGRAY_BRUSH);
wc.lpszMenuName = "MAIN";
wc.lpszClassName = szClassName;
RegisterClass(&wc);
HWND hwnd=CreateWindow(szClassName, szTitle,WS_OVERLAPPEDWINDOW,300,200,600,500,HWND_DESKTOP,NULL,hInst,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
Диалог открытия файла
//////.rc
MAIN MENU DISCARDABLE
BEGIN
POPUP "File "
BEGIN
MENUITEM "&Open",101
END
MENUITEM "&Chart", 102
MENUITEM "&About", 103
MENUITEM "&Quit", 104
END
////
#include <windows.h>
#include <windowsx.h>
#include<stdlib.h>
#define MI_OPEN 101
#define MI_CHART 102
#define MI_ABOUT 103
#define MI_QUIT 104
float nCurrentScales=1;
short nBuf[500];
short nBufScaled[500];
BOOL bDataOK=FALSE;
char szFile[128];
char szFilter[]="Data Files(*.DAT)\0*.dat\0All Files(*.*)\0*.*\0";
OPENFILENAME ofn;
DWORD nCnt;
void OnDestroy(HWND hwnd)
{
// MessageBox(hwnd,"The end","Exit",MB_OK);
PostQuitMessage(0);
}
void OnCommand(HWND hwnd, int id, HWND, UINT)
{
InvalidateRect(hwnd,NULL,TRUE);
switch(id)
{
case MI_OPEN:
// MessageBox(hwnd,"Opening File ","Command",MB_OK);
ZeroMemory(&ofn,sizeof(OPENFILENAME));
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hwnd;
ofn.lpstrCustomFilter=szFilter;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST;
if(GetOpenFileName(&ofn))
{
HANDLE hFile=CreateFile(szFile,GENERIC_READ,0,0,OPEN_EXISTING,0,NULL);
if(hFile==INVALID_HANDLE_VALUE)break;
ReadFile(hFile,nBuf,2*500,&nCnt,NULL);
CloseHandle(hFile);
bDataOK=TRUE;
for(int i=0;i<500;i++)
nBufScaled=nBuf*nCurrentScales;
InvalidateRect(hwnd,NULL,TRUE);
break;
}
// else
break;
////
case MI_CHART:
MessageBox(hwnd,"Chart","Command",MB_OK);
break;
case MI_about :
MessageBox(hwnd," 1 ","About",MB_OK);
break;
case MI_QUIT:
DestroyWindow(hwnd);
break;
}
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
HANDLE_MSG(hwnd,WM_COMMAND,OnCommand);
HANDLE_MSG(hwnd,WM_DESTROY,OnDestroy);
default:
return(DefWindowProc(hwnd,msg,wParam,lParam));
}
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int)
{
char szClassName[]="MainWindow";
char szTitle[]="1";
MSG Msg;
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockBrush(LTGRAY_BRUSH);
wc.lpszMenuName = "MAIN";
wc.lpszClassName = szClassName;
RegisterClass(&wc);
HWND hwnd=CreateWindow(szClassName, szTitle,WS_OVERLAPPEDWINDOW,300,200,600,500,HWND_DESKTOP,NULL,hInst,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
См. выделенное.
Почему GetOpenFileName(&ofn) возвращает FALSE и не входит в if?
Код:
OPENFILENAME ofn;
TCHAR file_name[MAX_PATH];
TCHAR full_file_path[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = full_file_path; // полный путь
ofn.lpstrFile[0] = '\0';
ofn.lpstrFileTitle = file_name;
ofn.lpstrFileTitle[0] = '\0';
ofn.nMaxFile = sizeof(full_file_path);
ofn.nMaxFileTitle = sizeof(file_name);
ofn.lpstrFilter = "Файлы(*.txt)\0*.txt\0";
ofn.nFilterIndex = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn) == TRUE) {...}
TCHAR file_name[MAX_PATH];
TCHAR full_file_path[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = full_file_path; // полный путь
ofn.lpstrFile[0] = '\0';
ofn.lpstrFileTitle = file_name;
ofn.lpstrFileTitle[0] = '\0';
ofn.nMaxFile = sizeof(full_file_path);
ofn.nMaxFileTitle = sizeof(file_name);
ofn.lpstrFilter = "Файлы(*.txt)\0*.txt\0";
ofn.nFilterIndex = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn) == TRUE) {...}
Я убьюсь скоро с ним:(
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
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);