我正在尝试通过单击按钮来运行简单事件,但我的代码似乎不起作用。有两个问题。首先,当我点击按钮时,我需要运行int nmr()
。其次,我需要在string inFile()
中插入由OpenFileDialog选择的文件路径。这是我的代码:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hWndButton
switch (message)
{
case WM_CREATE:
CreateMenubar(hWnd);
hWndButton = CreateWindow("button", "Go!",WS_VISIBLE|WS_CHILD,15,45,60,25,hWnd,
(HMENU) ID_BUTTON,NULL,NULL);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_FILE_OPEN:
openFileDialog1;
if (openFileDialog1->ShowDialog())
{
MessageBox(0, openFileDialog1->FileName, _T("File name"),
MB_OK | MB_ICONINFORMATION);
}
openFileDialog1->FilterIndex = 1;
openFileDialog1->Flags |= OFN_SHOWHELP;
openFileDialog1->InitialDir = _T("C:\\Windows\\");
openFileDialog1->Title = _T("Open Text File");
break;
case ID_BUTTON:
int nmr();
break;
case IDM_FILE_QUIT:
SendMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void CreateMenubar(HWND hwnd)
{
HMENU hMenubar;
HMENU hMenu;
hMenubar = CreateMenu();
hMenu = CreateMenu();
AppendMenuW(hMenu, MF_STRING, IDM_FILE_OPEN, L"&Open");
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenuW(hMenu, MF_STRING, IDM_FILE_QUIT, L"&Quit");
AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenu, L"&File");
SetMenu(hwnd, hMenubar);
}
OpenFileDialog::OpenFileDialog(void)
{
this->DefaultExtension = 0;
this->FileName = new TCHAR[MAX_PATH];
this->Filter = 0;
this->FilterIndex = 0;
this->Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
this->InitialDir = 0;
this->Owner = 0;
this->Title = 0;
}
bool OpenFileDialog::ShowDialog()
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = this->Owner;
ofn.lpstrDefExt = this->DefaultExtension;
ofn.lpstrFile = this->FileName;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = this->Filter;
ofn.nFilterIndex = this->FilterIndex;
ofn.lpstrInitialDir = this->InitialDir;
ofn.lpstrTitle = this->Title;
ofn.Flags = this->Flags;
GetOpenFileName(&ofn);
if (_tcslen(this->FileName) == 0) return false;
return true;
}
int nmr()
{
string inFile(openFileDialog1->FileName);
string outFile("C:\\Users\\admin\\Desktop\\outT1.txt");
int numline = 0;
double v1;
string s;
string v2, v3, v4;
string line_keep, line_avoid;
ifstream in_stream;
ofstream out_stream;
in_stream.open(inFile.c_str());
out_stream.open(outFile.c_str());
if (in_stream.fail() || out_stream.fail())
{
cout << "Error ocured during oppening file!" << endl;
return 1;
}
任何建议都将不胜感激。
答案 0 :(得分:1)
int nmr();
这个声明一个名为nmr
的函数。这不是你想要的。您希望调用您在其他地方定义的同名函数。你的意思是简单地写:
int retval = nmr();
调用函数。
如果您不关心返回值,您可以这样做:
nmr();
您需要在调用之上定义函数nmr
,或使用前向声明。
将文件名作为参数传递给nmr()
会更有意义。
int nmr(const string& inFileName)
使用int
返回值来表示成功与C ++中的习惯相去甚远。如果必须使用返回值来表示成功,那么bool
会更合适。较旧的C变体没有布尔类型,这可能是您学习返回int
以表示成功的概念。但是,在编写新代码时,是时候忘掉这个概念了。在与现有代码连接时,您仍需要了解它。