C++ 和 WinApi - 使用 GetWindowText() 获取 C++ 代码的参数
C++ and WinApi - usage of GetWindowText() to get parameters for C++ code
我编写了一些 C++ 控制台程序,可以将一种文件类型转换为另一种文件类型。我需要将控制台应用程序变成 window 应用程序。我从未提交过 window 申请。我选择了 WinApi,因为我没有太多时间也不想更改 IDE (DEV C++ 5.10)、compiler/linker 设置等。我需要一个小而简单的 window。我需要从用户那里得到的参数是:输入文件的路径,输出文件的名称和路径,以及两个double类型的参数。我已经开始试验,想把 window 中的文本放到一个 .txt 文件中。问题是我在 .txt 文件中得到了什么。我的代码:
#include <windows.h>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
HWND g_hText;
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE:
{
DestroyWindow( hwnd );
}
break;
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
//here I create the file and write to it
case WM_COMMAND:
{ if(( HWND ) lParam == g_hText )
{ DWORD dlugosc = GetWindowTextLength( g_hText );
LPSTR Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
GetWindowText( g_hText, Bufor, dlugosc + 1 );
ofstream out("some_file.txt",ios_base::app);
out<<Bufor;
out.close();
}
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
MSG msg;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
memset(&wc,0,sizeof(wc));//sprawdzic co to
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
g_hText = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
200, 0, 150, 30, hwnd, NULL, hInstance, NULL );
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
例如,当我在文本 window 中键入 "qwerty" 时,我在文本文件中得到 "qqqwqwqweqweqwerqwerqwertqwertqwertyqwertyqwerty"。来自 window 的文本正在被持续阅读。我试图在按下 "Enter"
后让它被阅读
case WM_COMMAND:
{ if(( HWND ) lParam == g_hText )
{ if(WM_KEYDOWN)
{ if(VK_RETURN)
{ DWORD dlugosc = GetWindowTextLength( g_hText );
LPSTR Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
GetWindowText( g_hText, Bufor, dlugosc + 1 );
ofstream out("some_file.txt",ios_base::app);
out<<Bufor;
out.close();
}
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
}
}
但没有效果。应该怎么做才合适?
您需要正确处理 WM_KEYDOWN
,而不是 WM_COMMAND
,因为 windows 在按键按下后收到 WM_KEYDOWN
,在各种事件后收到 WM_COMMAND
。
case WM_KEYDOWN:
{
if(wParam == VK_RETURN)
{
DWORD dlugosc = GetWindowTextLength( g_hText );
LPSTR Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
GetWindowText( g_hText, Bufor, dlugosc + 1 );
ofstream out("some_file.txt",ios_base::app);
out<<Bufor;
out.close();
}
break;
}
此外,
if(WM_KEYDOWN)
if(VK_RETURN)
这两行基本上是if (true)
并且不要忘记释放分配的内存。
我编写了一些 C++ 控制台程序,可以将一种文件类型转换为另一种文件类型。我需要将控制台应用程序变成 window 应用程序。我从未提交过 window 申请。我选择了 WinApi,因为我没有太多时间也不想更改 IDE (DEV C++ 5.10)、compiler/linker 设置等。我需要一个小而简单的 window。我需要从用户那里得到的参数是:输入文件的路径,输出文件的名称和路径,以及两个double类型的参数。我已经开始试验,想把 window 中的文本放到一个 .txt 文件中。问题是我在 .txt 文件中得到了什么。我的代码:
#include <windows.h>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
HWND g_hText;
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE:
{
DestroyWindow( hwnd );
}
break;
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
//here I create the file and write to it
case WM_COMMAND:
{ if(( HWND ) lParam == g_hText )
{ DWORD dlugosc = GetWindowTextLength( g_hText );
LPSTR Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
GetWindowText( g_hText, Bufor, dlugosc + 1 );
ofstream out("some_file.txt",ios_base::app);
out<<Bufor;
out.close();
}
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
MSG msg;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
memset(&wc,0,sizeof(wc));//sprawdzic co to
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
g_hText = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
200, 0, 150, 30, hwnd, NULL, hInstance, NULL );
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
例如,当我在文本 window 中键入 "qwerty" 时,我在文本文件中得到 "qqqwqwqweqweqwerqwerqwertqwertqwertyqwertyqwerty"。来自 window 的文本正在被持续阅读。我试图在按下 "Enter"
后让它被阅读case WM_COMMAND:
{ if(( HWND ) lParam == g_hText )
{ if(WM_KEYDOWN)
{ if(VK_RETURN)
{ DWORD dlugosc = GetWindowTextLength( g_hText );
LPSTR Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
GetWindowText( g_hText, Bufor, dlugosc + 1 );
ofstream out("some_file.txt",ios_base::app);
out<<Bufor;
out.close();
}
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
}
}
但没有效果。应该怎么做才合适?
您需要正确处理 WM_KEYDOWN
,而不是 WM_COMMAND
,因为 windows 在按键按下后收到 WM_KEYDOWN
,在各种事件后收到 WM_COMMAND
。
case WM_KEYDOWN:
{
if(wParam == VK_RETURN)
{
DWORD dlugosc = GetWindowTextLength( g_hText );
LPSTR Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
GetWindowText( g_hText, Bufor, dlugosc + 1 );
ofstream out("some_file.txt",ios_base::app);
out<<Bufor;
out.close();
}
break;
}
此外,
if(WM_KEYDOWN)
if(VK_RETURN)
这两行基本上是if (true)
并且不要忘记释放分配的内存。