运行 单击按钮事件 c++
Running event by clicking button c++
我正在尝试通过单击按钮来 运行 简单事件,但我的代码似乎不起作用。有两个问题。首先,我需要 运行 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] = '[=11=]';
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;
}
如有任何建议,我们将不胜感激。
int nmr();
这个声明了一个名为nmr
的函数。那不是你想要的。您想要 调用 您在别处定义的同名函数。你的意思是简单地写:
int retval = nmr();
即调用函数。
如果您不关心 return 值,您可以简单地执行以下操作:
nmr();
您需要在调用上方定义函数 nmr
,或使用前向声明。
将文件名作为参数传递给 nmr()
会更有意义。
int nmr(const string& inFileName)
使用 int
return 值来指示成功在 C++ 中远非惯用。如果您必须使用 return 值来表示成功,那么 bool
会更合适。 C 的较旧变体没有布尔类型,这可能是您学习 returning 一个 int
以指示成功的概念的地方。但现在是时候在编写新代码时忘掉这个概念了。在与现有代码交互时,您仍然需要了解它。
我正在尝试通过单击按钮来 运行 简单事件,但我的代码似乎不起作用。有两个问题。首先,我需要 运行 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] = '[=11=]';
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;
}
如有任何建议,我们将不胜感激。
int nmr();
这个声明了一个名为nmr
的函数。那不是你想要的。您想要 调用 您在别处定义的同名函数。你的意思是简单地写:
int retval = nmr();
即调用函数。
如果您不关心 return 值,您可以简单地执行以下操作:
nmr();
您需要在调用上方定义函数 nmr
,或使用前向声明。
将文件名作为参数传递给 nmr()
会更有意义。
int nmr(const string& inFileName)
使用 int
return 值来指示成功在 C++ 中远非惯用。如果您必须使用 return 值来表示成功,那么 bool
会更合适。 C 的较旧变体没有布尔类型,这可能是您学习 returning 一个 int
以指示成功的概念的地方。但现在是时候在编写新代码时忘掉这个概念了。在与现有代码交互时,您仍然需要了解它。