Children window - GetWindowText

This is a discussion on Children window - GetWindowText within the Windows Programming forums, part of the Platform Specific Boards category; Hello I am trying to recieve text from my EDIT window but the code return a strange error. Could you ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Children window - GetWindowText

    Hello I am trying to recieve text from my EDIT window but the code return a strange error.
    Could you look threw the code please?

    Code:
    #include <windows.h>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       PSTR szCmdLine, int iCmdShow)
    {
        TCHAR szAppName[] = TEXT("Default window");           // Header
        HWND hWnd;
    	HWND hWnd_read;
    	HWND hWnd_edit;
        MSG msg;
        WNDCLASSEX wc;
        
        wc.cbSize = sizeof(wc);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));            // Ico
        wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));         // Ico_sm
        wc.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR));
        wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);           // Background
        wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
        wc.lpszClassName = szAppName;
        
        RegisterClassEx(&wc);
        
        hWnd = CreateWindowEx(0,szAppName,
                            szAppName,
                            WS_OVERLAPPEDWINDOW,
                            200,                    // Margin from left
                            200,                    // Margin from top
                            500,                    // Widht
                            400,                    // Height
                            NULL,
                            NULL,
                            hInstance,NULL);
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
        
    //Child wins
    	hWnd_read = CreateWindowEx(0,
    		TEXT("BUTTON"),
    		TEXT("Read"),
    		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    		20, 40, 75, 25,
    		hWnd, 
    		NULL,
    		hInstance,
    		NULL);
    	if ( hWnd_read == NULL )
    		return FALSE;
    
    	hWnd_edit = CreateWindowEx(WS_EX_CLIENTEDGE,
    		TEXT("EDIT"),
    		TEXT("Edit"),
    		WS_CHILD | WS_VISIBLE | ES_LEFT,
    		20, 10, 200, 25,
    		hWnd,
    		NULL,
    		hInstance,
    		NULL);
    	if ( hWnd_edit == NULL )
    		return FALSE;
    
        while(GetMessage(&msg, NULL, 0, 0))
        {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    void Write(HWND hWnd)
    {
    	PAINTSTRUCT	ps;
    	HDC			hDC;
    	RECT		rect;
    	hDC = BeginPaint(hWnd, &ps);
    	GetClientRect(hWnd, &rect);
    	SetBkMode(hDC, TRANSPARENT);
    	DrawText(hDC, TEXT("Hi there"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    	ReleaseDC(hWnd, hDC);
    	EndPaint(hWnd, &ps);
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	TCHAR chText[100];
            switch(message)
            {
              case WM_DESTROY:
              PostQuitMessage(0);
              return 0;
    
    		  case WM_PAINT:
    			  Write(hWnd);
    				break;
    
    		  case WM_COMMAND:
    			  switch(LOWORD(wParam))
    			  {
    			  case ID_EXIT:
    				  SendMessage(hWnd, WM_DESTROY, wParam, lParam);
    			  case ID_FUNCTION1:
    				  GetWindowText(hWnd_edit, chText, 100);
    				  MessageBox(hWnd, chText, TEXT("Your text"), MB_ICONINFORMATION);
    				  break;
    			  }
            }
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    Error content: "error C2065: 'hWnd_edit' : undeclared identifier"

    MS Visual C++

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You declared hWnd_edit in winmain(), it's local to winmain() and not accessable to WndProc(). You will need to declare it globally or use something like getdlgitem() to have access to it in your wndproc

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Yes, in fact, you should declare ALL of those as globals.
    Code:
    #include <windows.h>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    TCHAR chText[100];
    TCHAR szAppName[ ] = TEXT("Default window"); // Header
    HWND hWnd;
    HWND hWnd_read;
    HWND hWnd_edit;
    MSG msg;
    WNDCLASSEX wc;
    PAINTSTRUCT ps;
    HDC hDC;
    RECT rect;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       PSTR szCmdLine, int iCmdShow)
    {
        wc.cbSize = sizeof(wc);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));            // Ico
        wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));         // Ico_sm
        wc.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR));
        wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);           // Background
        wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
        wc.lpszClassName = szAppName;
        
        RegisterClassEx(&wc);
        
        hWnd = CreateWindowEx(0,szAppName,
                            szAppName,
                            WS_OVERLAPPEDWINDOW,
                            200,                    // Margin from left
                            200,                    // Margin from top
                            500,                    // Widht
                            400,                    // Height
                            NULL,
                            NULL,
                            hInstance,NULL);
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
        
    //Child wins
    	hWnd_read = CreateWindowEx(0,
    		TEXT("BUTTON"),
    		TEXT("Read"),
    		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    		20, 40, 75, 25,
    		hWnd, 
    		NULL,
    		hInstance,
    		NULL);
    	if ( hWnd_read == NULL )
    		return FALSE;
    
    	hWnd_edit = CreateWindowEx(WS_EX_CLIENTEDGE,
    		TEXT("EDIT"),
    		TEXT("Edit"),
    		WS_CHILD | WS_VISIBLE | ES_LEFT,
    		20, 10, 200, 25,
    		hWnd,
    		NULL,
    		hInstance,
    		NULL);
    	if ( hWnd_edit == NULL )
    		return FALSE;
    
        while(GetMessage(&msg, NULL, 0, 0))
        {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    void Write(HWND hWnd)
    {
    	hDC = BeginPaint(hWnd, &ps);
    	GetClientRect(hWnd, &rect);
    	SetBkMode(hDC, TRANSPARENT);
    	DrawText(hDC, TEXT("Hi there"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    	ReleaseDC(hWnd, hDC);
    	EndPaint(hWnd, &ps);
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
            switch(message)
            {
              case WM_DESTROY:
              PostQuitMessage(0);
              return 0;
    
    		  case WM_PAINT:
    			  Write(hWnd);
    				break;
    
    		  case WM_COMMAND:
    			  switch(LOWORD(wParam))
    			  {
    			  case ID_EXIT:
    				  SendMessage(hWnd, WM_DESTROY, wParam, lParam);
    			  case ID_FUNCTION1:
    				  GetWindowText(hWnd_edit, chText, 100);
    				  MessageBox(hWnd, chText, TEXT("Your text"), MB_ICONINFORMATION);
    				  break;
    			  }
            }
            return DefWindowProc(hWnd, message, wParam, lParam);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM

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