-
Problem with text field.
How do I change my code so that after typing in the text field, when I press enter, it has the same effect as pressing the "Translate!" button.
Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define compare(a1,b1) !stricmp(a1,b1)
#define ID_OF_BUTTON 1101
#define ID_MYEDIT 3460
const char g_szClassName[] = "myWindowClass";
HWND hWndMyEdit;
int doSwilly(char *buf)
{
// this what happens when you click translate
return 0;
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
char buf[500];
switch(msg)
{
case WM_CLOSE:
DeleteFile("temp.t");
DestroyWindow(hwnd);
break;
case WM_DESTROY:
DeleteFile("temp.t");
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_OF_BUTTON:
GetWindowText(hWndMyEdit, buf, 500);
doSwilly(buf);
break;
}
break;
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);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
srand(time(NULL));
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,
"Swilly translator, version 2.",
WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 580, 50,
NULL, NULL, hInstance, NULL);
hWndMyEdit = CreateWindow("EDIT",
"", WS_CHILD | WS_VISIBLE,
0, 0, 489,20, hwnd,
(HMENU) ID_MYEDIT,
hInstance, NULL);
CreateWindow(
"button",
"Translate!",
WS_CHILD | WS_VISIBLE,
489, 0,
80, 20,
hwnd,
(HMENU)ID_OF_BUTTON,
0,
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;
}
-
Add this to your windows procedure;
Code:
case WM_KEYDOWN:
if((int) wParam == VK_RETURN){
GetWindowText(hWndMyEdit, buf, 500);
doSwilly(buf);
}
else return DefWindowProc(hwnd, msg, wParam, lParam);
break;
-
That works, cheers :D
Oh, actually it doesn't
Damn my premptive posting.
It works as long as the text field aint selected
-
Whoa....I see...when you are typing the focus is on the edit so it wont work....give me a minute
-
...and maybe you could tell me how I make it so the text field is automatically in focus? please?
-
I guess the easyies way would be to subclass the edit window......
I dont think standard controls send parent notifications...
I have something I have to do right now, but if nobody has answered before I get back I will look at it again...sorry..
-
Sorry for the delay mate...had to go online to do my weekly shop or I'd be eating carrots all damn week :D
Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define compare(a1,b1) !stricmp(a1,b1)
#define ID_OF_BUTTON 1101
#define ID_MYEDIT 3460
const char g_szClassName[] = "myWindowClass";
HWND hWndMyEdit;
WNDPROC MainWindProc;
int doSwilly(char *buf)
{
// this what happens when you click translate
MessageBox(0,"","",MB_OK);
return 0;
}
LRESULT CALLBACK EditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
char buf[500];
switch(msg){
case WM_CHAR:
if((int) wParam == VK_RETURN){
GetWindowText(hwnd, buf, 500);
doSwilly(buf);
break;
}
else return CallWindowProc(MainWindProc,hwnd, msg, wParam, lParam);
break;
}
return CallWindowProc(MainWindProc,hwnd, msg, wParam, lParam);
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
char buf[500];
switch(msg)
{
case WM_KEYDOWN:
if((int) wParam == VK_RETURN){
GetWindowText(hWndMyEdit, buf, 500);
doSwilly(buf);
break;
}
else return DefWindowProc(hwnd, msg, wParam, lParam);
break;
case WM_CLOSE:
DeleteFile("temp.t");
DestroyWindow(hwnd);
break;
case WM_DESTROY:
DeleteFile("temp.t");
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_OF_BUTTON:
GetWindowText(hWndMyEdit, buf, 500);
doSwilly(buf);
break;
}
break;
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);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
srand(time(NULL));
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,
"Swilly translator, version 2.",
WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 580, 50,
NULL, NULL, hInstance, NULL);
hWndMyEdit = CreateWindow("EDIT",
"", WS_CHILD | WS_VISIBLE,
0, 0, 489,20, hwnd,
(HMENU) ID_MYEDIT,
hInstance, NULL);
CreateWindow(
"button",
"Translate!",
WS_CHILD | WS_VISIBLE ,
489, 0,
80, 20,
hwnd,
(HMENU)ID_OF_BUTTON,
0,
NULL
);
MainWindProc = (WNDPROC)SetWindowLong(hWndMyEdit,GWL_WNDPROC,(DWORD)EditProc);
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;
}
I have used SetWindowLong to sublass your edit box and filter out all of the enter key presses........have a go and see if its ok for you
-
Thanks very much, that does work.
Now how can I make it automatically focus the text field when the program starts?
-
Code:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
char buf[500];
switch(msg)
{
case WM_ACTIVATE:
SetFocus(hWndMyEdit);
break;
case WM_KEYDOWN:
if((int) wParam == VK_RETURN){
GetWindowText(hWndMyEdit, buf, 500);
doSwilly(buf);
break;
}
else return DefWindowProc(hwnd, msg, wParam, lParam);
break;
case WM_CLOSE:
DeleteFile("temp.t");
DestroyWindow(hwnd);
break;
case WM_DESTROY:
DeleteFile("temp.t");
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_OF_BUTTON:
GetWindowText(hWndMyEdit, buf, 500);
doSwilly(buf);
break;
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
-
Thanks again, you've been a great help. :)