- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题已经让我抓狂了
我已经阅读了关于这个主题的每一个问答,每个人都有相同的解决方案,但它永远不会为我工作。我得出的结论是,因为我是新手,所以我一定遗漏了一些其他答案中没有提到的非常明显的东西,所以请尽可能详细地回答。
我有一个使用 WinAPI 的 C++ 程序。我有一个名为 hwnd 的窗口,该窗口有 2 个按钮和 2 个编辑控件文本框。此时我要做的就是将文本框中输入的文本保存到 .txt 文件中。
各种回答(还有微软官网)都说要用
SendMessage(editcontroltag,WM_GETTEXT,0,LPARAM 缓冲区)
或GetWindowText(editcontroltag, buffer, size)
等等。我已经尝试了所有这些,并且代码编译没有问题,但是我永远无法真正检索文本框中的文本。它要么是空的,要么是一些乱码(我试过 unicode 和 ansi,两种方式都会发生同样的事情)
乱码通常看起来像 ')6 或它的一些变体(它对我没有意义,所以我无法确定它的来源。
我尝试将缓冲区转换为几乎所有允许的数据类型,我尝试检索缓冲区及其地址(地址显示正常)。仍然没有运气。
我会在这里为您粘贴代码,很抱歉它非常困惑,您会注意到我已经注释掉了许多不同的检索文本的尝试。无论哪种方式都没有结果。每次都出现同样的错误
谢谢
#include <windows.h>
#include <iostream>
#include <fstream>
#include <TCHAR.H>
#include <stdio.h>
//#include "C:\Users\Eric\Desktop\Documentation\resource.h"
//the following defines are for adding menus without headers or rc files
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define IDC_MAIN_EDIT 101
#define ID_BUTTON 9003
#define ID_EDITCHILD1 100
#define ID_EDITCHILD2 102
#define BUFFER_SIZE 256
using namespace std;
const char g_szClassName[] = "myWindowClass";
BOOL authenticate(char, string);
string bufferToString(char* , int );
BOOL doit = TRUE;
//Step 4: the Window Procedure
/*
Procedure to add items to the main window
you create the item inside the WM_CREATE function thingy
look it up online, there are many examples
This will draw the item in the window, but will not do anything
next you will need to go to the WM_COMMAND switch statement
and add a case for the appropriate LWORD of your item
kapish?
*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEDUSER;
static HWND hwndEDPASS;
//TCHAR lpszLatin[] = "Lorem ipsum dolor sit amet, consectetur ";
UpdateWindow(hwnd);
switch(msg)
{
case WM_SIZE:
{
HWND hEdit;
RECT rcClient;
GetClientRect(hwnd, &rcClient);
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
}
break;
case WM_CREATE:
{
HMENU hMenu, hSubMenu;
HICON hIcon, hIconSm;
HINSTANCE hInstance;
if(doit)
{
HWND hwndEDUSER = CreateWindowEx(
0,"EDIT", // predefined class
NULL, // no window title
WS_CHILD | WS_VISIBLE |
ES_LEFT | ES_MULTILINE ,
60, 60, 100, 25, // set size in WM_SIZE message
hwnd, // parent window
(HMENU) ID_EDITCHILD1, // edit control ID
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // pointer not needed
// Add text to the window.
SendMessage(hwndEDUSER, WM_SETTEXT, 0, (LPARAM)("username"));
//SendMessage(hwndEdit, EM_SETLIMITTEXT, 3, NULL);
HWND hwndEDPASS = CreateWindow(
"EDIT", // predefined class
NULL, // no window title
WS_CHILD | WS_VISIBLE |
ES_LEFT,
60, 90, 100, 25, // set size in WM_SIZE message
hwnd, // parent window
(HMENU) ID_EDITCHILD2, // edit control ID
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // pointer not needed
// Add text to the window.
SendMessage(hwndEDPASS, WM_SETTEXT, 0, (LPARAM)("password"));
//SendMessage(hwndEdit, EM_SETLIMITTEXT, 3, NULL);
HWND hwndButton = CreateWindow(
"BUTTON", // Predefined class; Unicode assumed
"Commit", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
200, // x position
10, // y position
200, // Button width
25, // Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
HWND boobs = CreateWindow(
"BUTTON", // Predefined class; Unicode assumed
"LOG IN", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
200, // x position
35, // y position
200, // Button width
25, // Button height
hwnd, // Parent window
(HMENU)ID_BUTTON, // PREVIOUS COMMENT SAID NO MENU, HOWEVER THIS IS WHERE I ADD THE ID FOR THE BUTTON BEING PRESED
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
}
// HWND hWndExample = CreateWindow("EDIT", "Text Goes Here", WS_VISIBLE | WS_CHILD | ES_CENTER, 10,10,100,100, hwnd, NULL, hInstance, NULL);
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
SetMenu(hwnd, hMenu);
hIcon = static_cast<HICON>(LoadImage(NULL, "C:\menu_one.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE));
if(hIcon)
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
else
MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);
hIconSm = static_cast <HICON>(LoadImage(NULL, "C:\menu_two.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE));
if(hIconSm)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
elsehttp://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=VS.85).aspx
MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
}
break;
/*case WM_LBUTTONDOWN: //mouse click
case BN_CLICKED:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, "The document has been requested, and will be available shortly", "Document Requested", MB_OK | MB_ICONINFORMATION);
}*/
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_STUFF_GO:
MessageBox(hwnd, "Unavailable", "title", MB_OKCANCEL | MB_ICONINFORMATION );
break;
case ID_BUTTON:
{
//TCHAR user[BUFFER_SIZE];
//string user ="";
//static char user[256];
//char id[256];
//((WORD*)user)[0] = BUFFER_SIZE; //cast variable user of type TCHAR into WORD. Set position 0 of said WORD variable to BUFFER_SIZE (needed by EM_GETLINE)
// SendMessage(hwndEDUSER, EM_GETLINE, 0, (LPARAM)user);
// Edit_GetText(hwndEDUSER,(LPTSTR) user,BUFFER_SIZE);
//GetWindowText(hwndEDUSER, (LPSTR)user, 256);
//GetDlgItemText(hwndEDUSER, ID_EDITCHILD1, (LPTSTR)user,BUFFER_SIZE);
//const char id= *user;
int len = SendMessage(hwndEDUSER, WM_GETTEXTLENGTH, 0, 0);
char* buffer = new char[len];
UpdateWindow(hwnd);
SendMessage(hwndEDUSER, WM_GETTEXT, 0, (LPARAM)buffer);
//return buffer;
string user(buffer);
fstream users;
users.open("C:\\users\\Eric\\Desktop\\Documentation\\user1.txt");
users << user;
users.close();
doit = FALSE;
//PostMessage(hwnd, WM_PAINT, 0, 0);
UpdateWindow(hwnd);
//char id = (char)user;
/* Edit_GetText(hwndEDUSER, (LPTSTR)eyeD,50);
string id(eyeD);
if(id=="2"){MessageBox(hwnd, "BITTCH", "Document Requested", MB_OK | MB_ICONINFORMATION);} */
//string id = (string)user;
// authenticate(id, "");
//char szFileName[MAX_PATH];
//HINSTANCE hInstance = GetModuleHandle(NULL);
//GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd,buffer, "Title", MB_OK | MB_ICONINFORMATION);
}
break;
/* case BN_CLICKED:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, "Commit", "Document Requested", MB_OK | MB_ICONINFORMATION);
}
break;*/
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
/*case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}*/
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); //comment this out for header and rc
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_INACTIVEBORDER);//(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //comment this out for header and rc
//wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU); //comment this out for header and rc
//wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
//wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"nameofwin",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 480,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0) //this piece of code is responsible for collecting events (clicks.. etc) and reporting them to the window
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
BOOL authenticate(char uid, string pwd)
{
//string boobs=uid;
fstream users;
users.open("C:\\users\\Eric\\Desktop\\Documentation\\user1.txt");
users << uid;
users.close();
return TRUE;
}
最佳答案
您正在为句柄声明一个静态变量,HWND hwndEDUSER
。但是在创建窗口时,您没有使用该变量。您正在声明一个新的,它超出了范围并且丢失了。稍后,您使用静态变量...其中没有任何内容。
关于c++ - WinAPI 从编辑控件中检索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35213529/
我想实现自定义搜索,但遇到了一个麻烦。我需要将 UIButton、SearchBar 组合在一个控件中,以便我可以通过指针引用它。然后我将向该组合控件动态添加更多 UIbutton。最重要的是,我想将
它没有在我的方法中执行 if block 中的语句 母版页:- 页面加载事件:- Control c = new Control(); DoSomething(c); 我的方法:- protecte
ComboBox 控件有一个 setConverter 方法,请参阅 JavaFX ComboBox - Display text but return ID on selection举个例子。我正在
我没有找到任何包含用于标记化(标记)文本输入的控件的 wpf 库。也许我找不到库,因为我错误地调用了这个组件。怎么叫或者哪里有这样的库? 最佳答案 DevExpress WPF 库包含多个数据编辑控件
是否有 Silverlight 控件允许您输入文本并将其突出显示为代码? 例如: foreach (client in Clients){ client.Save();} would become
我有以下用户控件 a) Panel.ZIndex="99999999" 是否是将此控件设置为该控件中 TopMost 的正
是否可以在 Windows 窗体中使用 C# 在窗体加载时隐藏所有特定控件,例如标签或按钮,然后选择显示我不想显示的那些? 我有一个包含很多按钮和标签的程序,但我只想在加载时显示一两个,我觉得对每个标
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Duplicating components at Run-Time 我有一个TMyControl ( Contro
我正在尝试在 Delphi 中编写一个 dll 库,其中包含一个创建 TFrame 后代实例并返回它的函数。但是当我在应用程序中导入这个函数时,每次调用它时,我都会得到一个异常,例如“'xxx'控件没
是否有 Win32 API 调用来确定哪些窗口和/或控件在特定坐标和/或鼠标下可见? 最佳答案 您可以使用GetWindowFromPoint 。它将返回窗口句柄,以便您可以使用 GetClassNa
我需要在编辑控件中输入以下公式: 公式是在 MS Word 中制作的。尝试将其复制/粘贴到编辑控件(单行或多行)后,我得到 M 0.33 Q10T9.1-9.7。 当我输入这个时,我正在研究 Rich
我只是想成功地将它添加到我的窗口中,但这却出奇地困难。 我已经尝试过 #include "windef.h" #include "winbase.h" #include "initguid.h" #i
我希望能够使用 google maps api v3 拥有自己的“街景”按钮。单击按钮时,我希望它根据我的标记经纬度加载街景。然后我希望按钮更改为“返回 map ”,然后再次加载默认 map View
我目前正在用 PHP 开发(另一个)开源 CMS,我想使用 javascript 控件,尤其是管理面板。问题是,是否有任何具有 PHP 接口(interface)的开源、可自由分发的控件(用于创建 j
我为其编写软件的产品之一是会计类应用程序。它用 C++ 编写,使用 C++ Builder 和 VCL 控件,连接到运行在 Linux 上的 PostgreSQL 数据库。 PostgreSQL 数据
我使用 Key Listener 来读取用户的输入,但我遇到了问题。首先,我读到 JTextField“请输入您的姓名”。如果用户输入一个名字,例如 John,它将更改为 John。但是,如果用户输入
我正在尝试对齐数据表列中的复选框(h=center,v=middle) ... 但复选框仍然显示在错误的位置(见附图)
我有一个包含统计信息的 JSON 数据树: { prefix: "a", count: 20, children: [ { prefix: "a:b", c
我在 Photoshop 中设计了一个模型,我打算将它应用到我的产品目录的 ListView 控件中,但它似乎没有正确显示(未对齐?),我希望这里的人可以像我一样指出我的错误试图弄清楚无济于事。 预期
您是使用 ASP.NET 控件还是仅使用带有 CSS 的 HTML? 我在 TextBox 和 DropDownList 的宽度方面遇到了一些问题。在不同的浏览器中,宽度会有所不同,控件的大小也不会相
我是一名优秀的程序员,十分优秀!