Hello
I have made a simple win32 program that displays a window that has a button & a text area.
When I click the button, my program grabs the text from the text area & displays the grabbed text in a MessageBox.
My Problem:
When my program grabs the text from the text area, the text comes out as jobberish or nothing at all?
I believe the problem occurs with the code to send a message to the text area window (known as hEdit) to grab the text in it & store it in a char array.
Here is the simple code snippets where the problems occur & below that is the full program:
Window Proceedure: Creation of the controls - button & text area
case WM_CREATE: {
hEdit = CreateWindowEx(
WS_EX_CLIENTEDGE,
"Edit",
"edit box example",
WS_BORDER | WS_CHILD | WS_VISIBLE,
10, 10,
200, 100,
hwnd, (HMENU)IDM_TEXT,
gInstance,
NULL);
button = CreateWindowEx(
0,
"Button",
"Capture Text",
WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
110,120,
100,20,
hwnd,
(HMENU)IDM_BUTTON, // this is the custom made message sent when button is pushed
gInstance,
NULL);
}
break;
code to grab the text from text area: this is from the windows proceedure:
case WM_COMMAND:
switch(LOWORD(wParam)) {
// Grab text from text area if button is pushed
case IDM_BUTTON:
{
char buffer[300];
SendMessage(hEdit,WM_GETTEXT,sizeof(buffer),(LPARAM)buffer);
MessageBox(NULL,buffer,"Captured Text",MB_OK);
}
break;
default:
break;
}
break;
Full Program:
#include <windows.h>
#include <stdio.h>
// Define custom messages
#define IDM_BUTTON 1
#define IDM_TEXT 2
const char g_szClassName[] = "myWindowClass";
static HINSTANCE gInstance;
// Functions List //
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
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 = gInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// if registration of main class fails
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,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
NULL, NULL, gInstance, 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)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND button, hEdit;
switch(msg)
{
case WM_CREATE: {
hEdit = CreateWindowEx(
WS_EX_CLIENTEDGE,
"Edit",
"edit box example",
WS_BORDER | WS_CHILD | WS_VISIBLE,
10, 10,
200, 100,
hwnd, (HMENU)IDM_TEXT,
gInstance,
NULL);
button = CreateWindowEx(
0,
"Button",
"Capture Text",
WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
110,120,
100,20,
hwnd,
(HMENU)IDM_BUTTON, // this is the custom made message sent when button is pushed
gInstance,
NULL);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(LOWORD(wParam)) {
// Grab text from text area if button is pushed
case IDM_BUTTON:
{
HWND findWindow = GetWindow(hwnd,GW_CHILD);
char buffer[300];
SendMessage(findWindow,WM_GETTEXT,sizeof(buffer),(LPARAM)buffer);
MessageBox(NULL,buffer,"Captured Text",MB_OK);
}
break;
default:
break;
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}