-
problem with editctl
Hello every1;
im just playing with this code(calculator).its not a calculator
yet (its a test thing) .for now i have a dlg and on it a editbox to display numbers.and 3 buttons:
+ = and ok to end it;
when i input the number and push (+) button , to clear
the editbox ,i use EM_UNDO(i could have used WM_CLEAR) . so i can input the other number.
after the 2nd input when i push (=) button the answer
is correct and it will show on edit.
my problem is , i cant use EM_UNDO in (=)button ID"12"
to clear .because of the obvious reason that it will
clear the edit before the answer is shown.
what i need is a way to tell the edit to clear the window
after (answer is displayed && first char is punched)
i subclassed edit proc to modify the WM_CHAR , but that
seems to give me errors at times.
what can i do?
Code:
BOOL CALLBACK AboutDlg2Proc (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hedit;
static HDC hdc;
static int m, p;
static int n[3];
switch (message)
{
case WM_INITDIALOG :
hedit = GetDlgItem(hDlg, 9); //get a handle on id "9" the editctl
SetFocus(hedit);
return FALSE ;
case WM_COMMAND :
switch (LOWORD (wParam))
{
case IDOK:
EndDialog (hDlg, 0) ;
break;
case 10: //plus sign on dlg
n[0] = GetDlgItemInt(hDlg, 9, NULL, FALSE);
SendMessage(hedit,EM_UNDO, 0, 0);
SetFocus(hedit);
break;
case 12: //equal sign on dlg
n[1] = GetDlgItemInt(hDlg, 9, NULL, FALSE);
p = n[1] + n[0];
SetDlgItemInt(hDlg,9, p, FALSE);
SetFocus(hedit);
break;
}
break;
}
return FALSE ;
}
-
Not being able to see your calc dlg I can't be 100% sure but,
I would 'collect' the first number from the user (until they use an non digit button).
Save the operator they want used.
Collect the second number (until they use an non digit button). At this point I would perform the first operation, display the result.
If the last button was not '=' then save the result as the first number, remember the operator to use and repeat.
there are two cases if a digit button was pressed;
If the last button pressed was a digit;
read the contents of the edit to a string,
sprintf() the digit to the end of the string
and SetDlgItemText() to display it.
If the last button pressed was not a digit then this is hte first in a new number so clear the edit and display the digit.
-
--
| --------------------------
| editbox |
|-------------------------
|
| | |
|
|
|------------------------------------------------ | | | |
| + | - | = |
iykes
thx novacain.
please dont call it a calculator. its embarrasing .
right now its a addculator :p . i just
want to get pass the edit problem first .then i will initialize the logic later.
now since my problem is what happens after we push (=)
button (to tell edit , to clear the content upon the first
entry of a character after (=)). i came up with this solution
i will put a boolian value in case#12(= button) as bool12.
set it as TRUE within case 12 every time = button is chosen.
and wont clear the edit content from within case 12(to be
able to display the result).
then i will subclass edit procedure and in its WM_CHAR
part i will use this code:
WM_CHAR:
if(bool12 == TRUE){
SendMessage(hedit, EM_GETSEL, 0, 0);
SendMessage(hedit, WM_CLEAR, 0, 0);
or
SendMessage(hedit, EM_UNDO, 0, 0);
bool12 = FALSE;
}
break;
i am at work , i thought about it .i dont know if it works.
i try it when i get home.
cheers!
-
It works fine, so i guess sub was the answer.
nova;
now im gone add more feature to it in my (test workspace)
and then add it to my main program as an extra feature
(this prog has 140 menu items(i know its dumb) and its
all in one program ,that i have added features to it leisurleythrough the last few months,)like simple word process
, opengl graphic window(its nested in 2 menu item), musical keyboard notes, simple games & cartoons, clip& bm viewer,
sys info, syscolor change , font &&&ollaaalaaa,,).
now here is the added code with basecode added for
my unfinished cal. i dont know. should i use keyboard
buttons or dlg buttons for digital input .i guess the norm
is dlg,
Code:
/*------------------------------------------------------------
Sia k
------------------------------------------------------------*/
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
static BOOL bbutton = FALSE;
WNDPROC ep;
BOOL CALLBACK AboutDlg2Proc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK editProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static CHAR szAppName[] = TEXT ("Hello Lovely Sara") ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The, Hello Lovely Sara"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HBRUSH hbrush;
static HDC hdc;
PAINTSTRUCT ps ;
static int cxchar, cychar, cxclient, cyclient;
TEXTMETRIC tm;
static HINSTANCE hInstance;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxchar = tm.tmAveCharWidth;
cychar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
hInstance = ((LPCREATESTRUCT) lParam) -> hInstance;
DialogBox (hInstance, TEXT("cal"), hwnd, AboutDlg2Proc) ;
return 0;
case WM_SIZE:
cxclient = LOWORD(lParam);
cyclient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
BOOL CALLBACK AboutDlg2Proc (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hedit;
static int m, p;
static int n[3];
static HDC hdc;
PAINTSTRUCT ps ;
switch (message)
{
case WM_INITDIALOG :
hedit = GetDlgItem(hDlg, 9); //get a handle on id "9" the editctl
ep = (WNDPROC)SetWindowLong(hedit, GWL_WNDPROC, (LONG)editProc);
SetFocus(hedit);
return FALSE ;
case WM_COMMAND :
switch (LOWORD (wParam))
{
case IDOK:
EndDialog (hDlg, 0) ;
break;
case 10: //plus sign on dlg
n[0] = GetDlgItemInt(hDlg, 9, NULL, FALSE);
SendMessage(hedit, EM_SETSEL, (WPARAM)0, (LPARAM) 11);
SendMessage(hedit, WM_CLEAR, 0, 0);
SetFocus(hedit);
break;
case 12: //equal sign on dlg
bbutton = TRUE;
n[1] = GetDlgItemInt(hDlg, 9, NULL, FALSE);
p = n[1] + n[0];
SetDlgItemInt(hDlg,9, p, FALSE);
SetFocus(hedit);
break;
}
break;
}
return FALSE ;
}
LRESULT CALLBACK editProc (HWND hwedit, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps ;
static char pass2[50];
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwedit, &ps);
EndPaint(hwedit, &ps);
break;
case WM_CHAR:
if(bbutton == TRUE){
SendMessage(hwedit, EM_SETSEL, (WPARAM)0, (LPARAM) 11);
SendMessage(hwedit, WM_CLEAR, 0, 0);
bbutton = FALSE;
break;
}
}
return CallWindowProc (ep, hwedit, message, wParam, lParam) ;
}