Struggling to read text from Edit Control (box)

This is a discussion on Struggling to read text from Edit Control (box) within the Windows Programming forums, part of the Platform Specific Boards category; Tried few attempts but not winning.. My code creates an edit box and a button. I expect to type some ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Struggling to read text from Edit Control (box)

    Tried few attempts but not winning.. My code creates an edit box and a button. I expect to type some text on the edit box then after pressing the button the text should be displayed on the frame to show that it was read successfully... This is with no avail...

    here's my code
    Code:
    // Creating Controls...
    case WM_CREATE:
    {
    	editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    	CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
    			IDBC_DEFPUSHBUTTON,_T("DISPLAY"));
            return 0;
    }
    
    //after button press 
    case WM_COMMAND:
    	if (LOWORD(wParam) == IDBC_DEFPUSHBUTTON) 
    	{
    		GetDlgItemText(hwnd, IDCE_DLG, szString, 80);
    		printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    	}
    
    // Function to display text
    void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
    {
    	HDC hdc = GetDC(NULL);
    	long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    	ReleaseDC(NULL, hdc);
    
    	HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
    	SelectObject(hDC, font);
    	SetBkColor(hDC, g_rgbBackground);
    	TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));
    	DeleteObject(font);
    }
    I suspect it may have to do with the first argument of GetDlgItemText()... the handle

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    TextOut does not create a static control with text in it. It simply draws that thing on the screen once. And you are using the global device context for drawing...

    You are doing pretty much everything wrong here, I think you should read a tutorial.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by maxorator View Post
    TextOut does not create a static control with text in it. It simply draws that thing on the screen once. And you are using the global device context for drawing...

    You are doing pretty much everything wrong here, I think you should read a tutorial.
    I've already displayed something using textout() with no probs... I used debug to check whether or not GetDlgItemText() retrieves the string from EditBox, and the watch window shows that it doesn't... Has nothing to do with TextOut()....

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The code doesn't really show if your edit control was created with the id IDCE_DLG. Somehow I doubt it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    This code has no sense at all and is plenty of memory leaks.
    Read the Petzold to get the basis.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by maxorator View Post
    The code doesn't really show if your edit control was created with the id IDCE_DLG. Somehow I doubt it.
    full Code...

    Code:
    #include "stdafx.h"
    #include <windows.h>  //include all the basics
    #include <tchar.h>    //string and other mapping macros
    #include <string>
    
    // Define structure for colors
    typedef struct rgbColors {
    	int R,G,B;
    } RGBColor;
    
    COLORREF g_rgbBackground = RGB(0xD3,0xD3,0xD3); // Set to grey
    
    #define MAXCOLOR 10
    
    //define an unicode string type alias
    typedef std::basic_string<TCHAR> ustring;
    //=============================================================================
    //message processsing function declarations
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    
    // function declarations
    inline int ErrMsg(const ustring&);
    HWND OnCreate(const HWND,CREATESTRUCT*, RECT& rc);
    RECT initRecArea(int left, int top, int width, int height);
    void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC);
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index);
    void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc);
    HWND CreateEdit(const HWND,const HINSTANCE,DWORD,const RECT&,const int, const ustring&);  
    HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    				  const RECT& rc,const int id,const ustring& caption);
    
    //setup some edit control id's
    enum {
    	IDCE_SINGLELINE=200,
    	IDCE_MULTILINE,
    	IDCE_DLG,
    	IDBC_DEFPUSHBUTTON,
    	IDBC_PUSHBUTTON,
    	IDBC_AUTOCHECKBOX,
    	IDBC_AUTORADIOBUTTON,
    	IDBC_GROUPBOX,
    	IDBC_ICON,
    	IDBC_BITMAP
    };
    
    // Globals
    PAINTSTRUCT pntS;
    HWND editCntrl;
    
    //=============================================================================
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
    {
    	ustring classname=_T("RFS Range");
    	WNDCLASSEX wcx={0};  //used for storing information about the wnd 'class'
    
    	wcx.cbSize			= sizeof(WNDCLASSEX);           
    	wcx.lpfnWndProc		= WndProc;             //wnd Procedure pointer
    	wcx.hInstance		= hInst;               //app instance
    	wcx.hIcon			= (HICON)LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED);
    	wcx.hCursor			= (HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED);
    	wcx.hbrBackground	= reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);   
    	wcx.lpszClassName	= classname.c_str(); 
    
    	if (!RegisterClassEx(&wcx))
    	{
    		ErrMsg(_T("Failed to register wnd class"));
    		return -1;
    	}
    
    	int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
    	int desktopheight=GetSystemMetrics(SM_CYSCREEN);
    
    	HWND hwnd=CreateWindowEx(0,                     //extended styles
    		classname.c_str(),     //name: wnd 'class'
    		_T("RFS Range"),	   //wnd title
    		WS_OVERLAPPEDWINDOW,   //wnd style
    		desktopwidth/4,        //position:left
    		desktopheight/4,       //position: top
    		desktopwidth/2,        //width
    		desktopheight/2,       //height
    		0,                     //parent wnd handle
    		0,                     //menu handle/wnd id
    		hInst,                 //app instance
    		0);                    //user defined info
    	if (!hwnd)
    	{
    		ErrMsg(_T("Failed to create wnd"));
    		return -1;
    	}
    
    	ShowWindow(hwnd,nCmd); 
    	UpdateWindow(hwnd);
    	MSG msg;
    
    	while (GetMessage(&msg,0,0,0)>0)
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return static_cast<int>(msg.wParam);
    }
    
    //=============================================================================
    LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    	int pos=0;
    	HDC hdc;
    	char szString[100];
    	int maxChars = 20;
    	HWND hWndDLG;
    	RGBColor colors[MAXCOLOR] = {{0xD3,0xD3,0xD3},{0x00,0xFF,0x00},
    	{0xFF,0xFF,0xFF},{0x00,0x00,0x00}};
    	
    	CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT*>(lParam);
    
    	switch (uMsg)
    	{
    	case WM_CREATE:
    		editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    		CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
    			IDBC_DEFPUSHBUTTON,_T("DISPLAY"));
    		return 0;
    
    	case WM_PAINT:
    		hdc = BeginPaint(hwnd, &pntS);
    
    		// Draw unto the client area
    		PaintObject(hwnd,initRecArea(10,10,300,200),colors, hdc, pos);
    		printText(hwnd, "Hello there", initRecArea(15,15,0,0), hdc);
    		PaintObject(hwnd,initRecArea(305,10,605,250),colors, hdc, pos);
    		PaintObject(hwnd,initRecArea(10,205,300,315),colors, hdc, pos);
    		PaintObject(hwnd,initRecArea(10,320,300,420),colors, hdc, pos);
    		
    		EndPaint(hwnd, &pntS);
    
    	case WM_COMMAND:
    		if (LOWORD(wParam) == IDBC_DEFPUSHBUTTON) 
    		{
    			GetDlgItemText(hwnd, IDCE_DLG, szString, 80);
    			printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    		}
    		
    		return 0;
    
    	case WM_TIMER: 
    		/* Force the WM_PAINT */
    		//SetTimer(hwnd, ID_TIMER, 500, NULL);
    		//InvalidateRect (hwnd, NULL, TRUE);
    		//UpdateWindow(hwnd);
    		break;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);    //signal end of application
    		return 0;
    	default:
    		//let system deal with msg
    		return DefWindowProc(hwnd,uMsg,wParam,lParam);  
    	}
    }
    
    //=============================================================================
    inline int ErrMsg(const ustring& s)
    {
    	return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
    }
    
    //=============================================================================
    // EDIT control
    HWND CreateEdit(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    				const RECT& rc,const int id,const ustring& caption)
    {
    	dwStyle|=WS_CHILD|WS_VISIBLE;
    	return CreateWindowEx(WS_EX_CLIENTEDGE,	//extended styles
    		_T("edit"),							//control 'class' name
    		caption.c_str(),					//control caption
    		dwStyle,							//control style 
    		rc.left,							//position: left
    		rc.top,								//position: top
    		rc.right,							//width
    		rc.bottom,							//height
    		hParent,							//parent window handle
    		reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
    		hInst,								//application instance
    		0);									//user defined info
    }
    //=============================================================================
    // BUTTON Control
    HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    				  const RECT& rc,const int id,const ustring& caption)
    {
    	dwStyle|=WS_CHILD|WS_VISIBLE;
    	return CreateWindowEx(0,          //extended styles
    		_T("button"),                 //control 'class' name
    		caption.c_str(),              //control caption
    		dwStyle,                      //control style 
    		rc.left,                      //position: left
    		rc.top,                       //position: top
    		rc.right,                     //width
    		rc.bottom,                    //height
    		hParent,                      //parent window handle
    		reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
    		hInst,                        //application instance
    		0);                           //user defined info
    }
    
    // Purpose: Draws an object on the window
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index)
    {					
    	HPEN hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
    	HBRUSH hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));
    	DrawARectangle(hwnd, hpen, hdc, hbrush, rc);
    }
    
    // Purpose: Draws a rectangle which will have text boxes for input data
    void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc) 
    {
    	// Select the new pen and brush, and then draw.
    	HPEN hpenOld = (HPEN)SelectObject(hdc, hpen);
    	HBRUSH hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
    	Rectangle(hdc,rc.left,rc.top,rc.right,rc.bottom);
    
    	// Do not forget to clean up.
    	SelectObject(hdc, hpenOld);
    	DeleteObject(hpen);
    	SelectObject(hdc, hbrushOld);
    	DeleteObject(hbrush);
    }
    
    // Initialize rectangle area
    RECT initRecArea(int left, int top, int width, int height)
    {
    	RECT rec = {left, top, width, height};
    	return rec;
    }
    
    //handles the WM_CREATE message of the main, parent window; return -1 to fail
    HWND OnCreate(const HWND hwnd,CREATESTRUCT *cs, RECT& rc)
    {
    	return CreateEdit(hwnd,cs->hInstance,0,rc,IDCE_SINGLELINE,_T(""));
    }
    
    // Display text on screen
    void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
    {
    	HDC hdc = GetDC(NULL);
    	long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    	ReleaseDC(NULL, hdc);
    
    	HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
    	SelectObject(hDC, font);
    	SetBkColor(hDC, g_rgbBackground);
    	TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));
    	DeleteObject(font);
    }

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Managed to figure out something! Works super.........

    Code:
    	case WM_PAINT:
    		hdc = BeginPaint(hwnd, &pntS);
    
    		// Draw rectangle areas
    		PaintObject(hwnd,initRecArea(10,10,300,200),colors, hdc, pos);
    		PaintObject(hwnd,initRecArea(305,10,605,250),colors, hdc, pos);
    		PaintObject(hwnd,initRecArea(10,205,300,315),colors, hdc, pos);
    		PaintObject(hwnd,initRecArea(10,320,300,420),colors, hdc, pos);
    		
    		// Display Instruction Text!
    		printText(hwnd, szString, initRecArea(15,15,0,0), hdc);
    
    		// Read text from edit control
    		GetWindowText(editCntrl, szString2, 100);
    		printText(hwnd, szString2, initRecArea(315,15,0,0), hdc);
    		
    		EndPaint(hwnd, &pntS);
    
    	case WM_COMMAND:
    		switch(wParam){
    			case IDBC_DEFPUSHBUTTON:
    			{
    				// Update client area...
    				InvalidateRect (hwnd, NULL, TRUE);
    				UpdateWindow(hwnd);		
    			}
    		}
    		return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 06:58 AM
  2. find if text in edit box has been changed
    By willc0de4food in forum Windows Programming
    Replies: 13
    Last Post: 09-10-2005, 10:47 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  4. How do I make my edit box move text to the next line?
    By ElWhapo in forum Windows Programming
    Replies: 2
    Last Post: 01-04-2005, 10:21 PM
  5. Edit Control
    By sean345 in forum Windows Programming
    Replies: 0
    Last Post: 07-08-2002, 04:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21