[英]Win32 C++ GUI - take inputed text and show it in another window
我在Win32 C ++ GUI上有問題。 我正在創建一個程序,並且要執行以下步驟之一:1.讓用戶在文本框中鍵入內容,2.在另一個框/區域中顯示它,3.從文本框中清除鍵入的文本。
就像聊天一樣-在一個窗口中鍵入消息,然后在第二個窗口中顯示。
到目前為止,我有2個窗口的代碼(我知道不多),並且我知道在WM_COMMAND內部我應該有一些代碼,這些代碼將接收輸入的文本,然后將其傳遞到另一個窗口(我想在其中顯示消息)。
這是我的代碼(主要是CodeBlocks的Win32 Application默認代碼,我剛剛添加了方框和按鈕):
#include <tchar.h>
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
HWND TextBox, SendButton, TextField;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Messages"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
500, /* The programs width */
370, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
TextBox = CreateWindow("EDIT",
"",
WS_BORDER | WS_CHILD | WS_VISIBLE,
10, 300, 390, 20,
hwnd, (HMENU) 1, NULL, NULL);
SendButton = CreateWindow("BUTTON",
"Send",
WS_VISIBLE | WS_CHILD | WS_BORDER,
410, 300, 65, 20,
hwnd, (HMENU) 2, NULL, NULL);
TextField = CreateWindow("EDIT",
"",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_READONLY,
10, 90, 465, 200,
hwnd, (HMENU) 3, NULL, NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case 1: // when button is clicked, this will happen:
// what code should go here??
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
我的問題是:我應該使用哪些功能(以及如何使用)來接受用戶輸入並將其顯示在另一個窗口中。
提前謝謝你的幫助。
這有點不直觀。
您希望能夠獲得兩個編輯控件的窗口文本。 當前輸入到編輯控件中的文本是窗口文本。 那些使用通用WM_GETTEXTLENGTH
和WM_GETTEXT
消息。 同樣, WM_SETTEXT
設置窗口文本。
現在,這里的地方變得有點混亂: 有在編輯(甚至是豐富的編輯)控制一個給定的位置插入文本的方式。 但是沒有EM_INSERTTEXT
或EM_REPLACETEXT
或其他任何東西。 然而, 有 ,一個EM_REPLACESEL
,它取代為新文本當前選擇 。
如果沒有選擇,則替換文本將插入在插入符號中。
那么如何移動插入符號? 好了,回到我們的選擇隱喻, 即EM_GETSEL
的文檔,該消息可以找出當前選中的文本 ,
如果沒有選擇,則起始值和終止值均為插入號的位置。
因此,要添加文本,您必須
EM_REPLACESEL
添加文本。 祝好運!
您的代碼處理WM_COMMAND消息時存在一些問題。
首先,使用案例1來檢測單擊按鈕的時間,但是按鈕的控件ID是2而不是1。
其次,您無需檢查按鈕發送的是哪種通知消息。 按鈕可以(並且確實)使用WM_COMMAND消息來通知程序除單擊之外的其他操作,因此,如果僅在選中按鈕時才想執行某些操作,則必須確保WM_COMMAND消息用於BN_CLICKED通知(通過選中wParam參數的高位字)。
您可能將switch語句嵌套了三層(用一個開關標識消息,用一個開關標識控件,然后用一個開關標識通知代碼),但是如果我是我,則將代碼用於在單獨的函數中處理來自按鈕的消息,然后在WM_COMMAND處理代碼中調用該函數。 我在想這樣的事情。
case WM_COMMAND:
switch(LOWORD(wParam))
{
case 2: // when button is clicked, this will happen:
ProcessButtonMessage(HIWORD(wParam)); //Pass the notification code to the processing function.
break;
}
break;
處理功能可能看起來像這樣
static void ProcessButtonMessage(WORD wNotification)
{
switch(wNotification)
{
case BN_CLICKED:
//Add code to process button click here
break;
}
}
現在就使用什么功能來傳輸文本。 通過將WM_GETTEXT消息發送到接收用戶輸入的文本框中,可以得到用戶輸入的文本,然后按照上一個答案的建議使用EM_REPLACESEL將其傳輸到另一個文本框中,然后可以發送WM_SETTEXT消息(帶有空字符串)到第一個文本框,以擦除用戶輸入的文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.