edit control in dialog box problems
I still think I may be using the GetDlgItem API incorrectly.
1. I select the popup 'Sections' from my menu
2. Then I select the actual cross section i.e. a 'circle' from the popup (ID_SECTIONS_CIRCLE)
3. This in turn runs my dialog (ID_CIRCLE_PROPERTIES) that allows me to enter a radius into the (IDC_EDIT_RADIUS) field.
4. The prog. then crashes... and says it must be closed. So I am obviously doing something wrong.
All I want to do is get this info. stored into a variable so I can check it. Anyone see what I did wrong?
Code:
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD (wParam))
{
//case IDM_FILE_NEW:
//case IDM_FILE_OPEN:
//case IDM_FILE_SAVE:
//case IDM_FILE_SAVE_AS:
MessageBeep (0) ;
return 0 ;
case ID_EXIT:
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
return 0 ;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case ID_SECTIONS_CIRCLE:
{
int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_CIRCLE_PROPERTIES), hwnd, AboutDlgProc);
//Test return value of Dialog....
if(ret == IDCANCEL){
MessageBox(hwnd, "Dialog exited with IDCANCEL.", "Notice", MB_OK | MB_ICONINFORMATION);
}
else if(ret == -1){
MessageBox(hwnd, "Dialog failed!", "Error", MB_OK | MB_ICONINFORMATION);
}
//Dialog must have exited with IDOK. ToDo.Check for valid input.??? Return to label if incorrect???
// End test return value of Dialog..
int nLen = GetWindowTextLength(hwnd);
LPTSTR buffer = 0;
buffer[nLen] = NULL;
{
HWND hdlg;
////////////////What am I doing wrong here//////////////////
// program crashes when I select Sections | Circle and then enter the radius in the edit field
hdlg = GetDlgItem(hwnd, IDC_EDIT_RADIUS);
//GetWindowText(hwnd, buffer, nLen);
GetDlgItemText(hdlg, // handle to dialog box
IDC_EDIT_RADIUS, // control identifier
buffer, // pointer to buffer for text
nLen // maximum size of string
);
MessageBox(hwnd, "Dialog!", "I'd like to print buffer here", MB_OK | MB_ICONINFORMATION);
}
}
break;
Thanks so much for your help!
Thanks everyone for your help I am greatfull. lvk I will try what you suggested.
Still problems understanding.
Apparently the light hasn't dawned yet. Everything in the menu and dialogs work except for the ID_CIRCLE_PROPERTIES (The most important part, of course). It doesn't even display the dialog now so that I could even try to enter a value. I thought that I understood what you had directed me to do. I must still be doing something incorrectly, here is what I think is the most logical process I have yet tried:
// Moments of Inertia Program.
Code:
#include <windows.h>
#include <math.h>
#include <winuser.h>
#include "resource.h"
#define PI 4 * atan(0.0);
HINSTANCE hInstance;
//HWND hdlg;
HMENU hMenu ;
char* radius = 0;
int ret = 0;
///////////////////////////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutDlgProc(HWND , UINT, WPARAM, LPARAM);
///////////////////////////////////////////////////////////////////////////////////////////////////
TCHAR szAppName[] = TEXT ("Moment of Inertia Program") ;
///////////////////////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_CROSS) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hMenu = LoadMenu (hInstance, MAKEINTRESOURCE(IDR_MENU1));
hwnd = CreateWindow (szAppName, TEXT ("Moment of Inertia Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, hMenu, hInstance, NULL) ;
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)
{
//int ret;
switch (message)
{
case WM_COMMAND:
switch (LOWORD (wParam))
{
//case IDM_FILE_NEW:
case IDM_FILE_OPEN:
case IDM_FILE_SAVE:
//case IDM_FILE_SAVE_AS:
MessageBeep (0) ;
return 0 ;
case IDM_EXIT:
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
// return 0 ;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case ID_SECTIONS_CIRCLE:
ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_CIRCLE_PROPERTIES), hwnd, AboutDlgProc);
return ret ;
case ID_SECTIONS_RECTANGLE:
MessageBox (hwnd, TEXT ("To Do!"),
szAppName, MB_ICONEXCLAMATION | MB_OK) ;
return 0 ;
case ID_HELP_HELP:
MessageBox (hwnd, TEXT ("Help not yet implemented!"),
szAppName, MB_ICONEXCLAMATION | MB_OK) ;
return 0 ;
case ID_ABOUT:
MessageBox (hwnd, TEXT ("Moments of Inertia\n")
TEXT ("Me, 2002"),
szAppName, MB_ICONINFORMATION | MB_OK) ;
return 0 ;
}
break ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HWND hdlg;
//int ret = 0;
int nLen = 0;
LPTSTR buffer = NULL;
buffer[nLen] = NULL;
switch(Message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
{
hdlg = GetDlgItem(hwnd, IDC_EDIT_RADIUS); // handle to dialog box edit field???
nLen = GetWindowTextLength(hdlg);
GetDlgItemText(hdlg, // handle to dialog box
IDC_EDIT_RADIUS, // control identifier
buffer, // pointer to buffer for text
nLen // maximum size of string
);
radius = buffer; // No error checking yet! atod!
MessageBox(hwnd, "Dialog!", "I'd like to print buffer here", MB_OK | MB_ICONINFORMATION);
}
EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}