My class doesn't create an edit box.

This is a discussion on My class doesn't create an edit box. within the Windows Programming forums, part of the Platform Specific Boards category; Hi I have a class that creates an edit box, unfortunately it doesn't appear in my main window when I ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    96

    My class doesn't create an edit box.

    Hi I have a class that creates an edit box, unfortunately it doesn't appear in my main window when I create it.

    Code:
    #include <windows.h>
    #include "window.h"
    #include "editcontrol.h"
    
    EditControl::EditControl()
    {
    	
    }
    
    EditControl::~EditControl()
    {
    	
    }
    
    int EditControl::Create()
    {
    	Window Window;
    
    	m_hEdit = CreateWindowEx(
    	          WS_EX_CLIENTEDGE,
    	          "EDIT",
    	          NULL,
    	          WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          Window.g_hMainWindow,
    	          (HMENU)ID_TEXT,
    	          Window.g_hThisInstance,
    	          NULL
    	          );
    	          
    	SendMessage(Window.g_hMainWindow, WM_SETFONT, 
    	            (WPARAM)GetStockObject(DEFAULT_GUI_FONT),
    	            0);          
    }
    Here is my other class which creates a window.

    Code:
    #include <windows.h>
    #include "main.h"
    #include "window.h"
    
    Window::Window()
    {
    	
    }
    
    Window::~Window()
    {
    	
    }
    
    int Window::Register()
    {
    	//g_szClassName[ ] = "Text Editor";
    	
    	m_wclx.hInstance = g_hThisInstance;
    	m_wclx.lpszClassName = g_szClassName;
    	m_wclx.lpfnWndProc = WindowProcedure;
    	m_wclx.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    	m_wclx.cbSize = sizeof(WNDCLASSEX);
    	m_wclx.hIcon = static_cast<HICON>(LoadImage(0, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE));
    	m_wclx.hIconSm = static_cast<HICON>(LoadImage(0, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE));
    	m_wclx.hCursor = static_cast<HICON>(LoadImage(0, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE));
    	m_wclx.lpszMenuName = NULL;
    	m_wclx.cbClsExtra = 0;
    	m_wclx.cbWndExtra = 0;
    	m_wclx.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
    	
    	if(!RegisterClassEx(&m_wclx))
    	{
    		MessageBox(NULL, "Can't register window class!", "Error!",
    		           MB_ICONEXCLAMATION | MB_OK);
    		return -1;
    	}
    	return 0;
    }
    
    int Window::Create()
    {
    	g_hMainWindow = CreateWindowEx(
    	                WS_EX_APPWINDOW,
    	                g_szClassName,
                        "Text Editor",
    	                WS_OVERLAPPEDWINDOW,
    	                CW_USEDEFAULT,
    	                CW_USEDEFAULT,
    	                CW_USEDEFAULT,
    	                CW_USEDEFAULT,
    	                HWND_DESKTOP,
    	                NULL,
    	                g_hThisInstance,
    	                NULL
    	                );
    	
    	if(!g_hMainWindow)
    	{
    		MessageBox(NULL, "Can't create window!", "Error!",
    		           MB_ICONEXCLAMATION | MB_OK);
    		return -1;
    	}
    	return 0;
    }
    
    int Window::Menu()
    {
    	g_hMenu = LoadMenu(NULL, MAKEINTRESOURCE(IDM_MENU));
    	SetMenu(g_hMainWindow, g_hMenu);
    }
    
    int Window::Show(int iCmdShow)
    {
    	ShowWindow(g_hMainWindow, iCmdShow);
    }
    Here is the WinMain()

    Code:
    #include <windows.h>
    #include "editcontrol.h"
    #include "window.h"
    #include "main.h"
    
    EditControl EditControl;
    Window Window;
    
    int WINAPI
    WinMain(HINSTANCE hInstance,
            HINSTANCE hPrevInstance,
            LPSTR lpCmdLine,
            int nCmdShow)
    {
    	MSG messages;	
    	
    	Window.Register();
    	Window.Create();
    	Window.Menu();
    	Window.Show(nCmdShow);
    	
    	FindReplace.g_uFindReplaceMessage = RegisterWindowMessage(FINDMSGSTRING);
    	
    	while(GetMessage(&messages, NULL, 0, 0))
    	{
    		TranslateMessage(&messages);
    		DispatchMessage(&messages);
    	}
    	return messages.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT messages,
                                     WPARAM wParam, LPARAM lParam)
    {
    	switch(messages)
    	{
    		case WM_CREATE:
    		     {     		
    		     	EditControl.Create();
    		     }
    		     break;     
    	
    		
    		case WM_CLOSE:
    		     DestroyWindow(hwnd);
    		     break;
    		
    		case WM_DESTROY:
    		     PostQuitMessage(0);
    		     break;
    		
    		default:
    		     return DefWindowProc(hwnd, messages, wParam, lParam);     
    	}	
    	return 0;
    }
    If someone could help me I would appreciate it.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I take it that
    Code:
    int EditControl::Create()
    {
    	Window Window;
    }
    is a typo?

    Is m_hEdit in your EditControl::Create() function non-zero? If it is zero then what does GetLastError have to say about why CreateWindowEx has failed?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    No
    Code:
    Window Window;
    is not a typo.
    My debugger (GDB, I use Code::Blocks) says m_hEdit is 0x0.
    I'll try GetLastError() and see what it says....

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    This is what I get if I check it before the edit control creation.

    failed with error 0: (little temperature sign)G%

    This is what I get if I check it after the edit control creation.

    failed with error 1400: hT%

    winerror.h defines 1400L as

    Code:
    #define ERROR_INVALID_WINDOW_HANDLE 1400L
    I'm guessing this means I have a bad window handle.
    Last edited by sethjackson; 09-01-2005 at 03:13 PM.

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    That error code (1400) corresponds to 'invalid window handle' and is probably referring to the parent window handle ie. Window.g_hMainWindow.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Okay I set the parent handle to NULL, and the instance handle to NULL. Still nothing I get ERROR_TLW_WITH_WSCHILD 1406L.
    So I have to have a parent window I guess? How do I go about fixing this?

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try using GetDesktopWindow as the parent and GetModuleHandle(0) as the hInstance.

    edit: But it looks like whatever 'Window' is (and this is where having a variable name set as the same as the class/struct name gets really confusing) isn't doing what it's supposed to ie. initialise its member variables with suitable values.

    If you still keep getting problems you should zip up and attach your project.
    Last edited by Ken Fitlike; 09-01-2005 at 03:28 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Just glanced through the topic, and I'm not sure if this is your problem, but I don't think you can create a child window (one with WS_CHILD) with the desktop window as the parent. You should specify an existing top-level window as the parent for the child control (the edit control).

    The description for ERROR_TLW_WITH_WSCHILD is

    "Cannot create a top-level child window."

    edit:

    Is g_hMainWindow a global variable or an instance variable?
    Last edited by Dante Shamest; 09-01-2005 at 03:32 PM.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    g_hMainWindow is a public member of the class Window;
    Umm how do I attach a zip to my post? I tried before and it rejected it because of the .zip extension.

    Edit:

    I set the main window's parent handle to NULL and I still don't get an edit control and I still get error 1400.
    Last edited by sethjackson; 09-01-2005 at 03:58 PM.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Change the file extension to 'txt'.

    edit:
    Quote Originally Posted by Dante
    but I don't think you can create a child window (one with WS_CHILD) with the desktop window as the parent
    You can but it's not pretty - well this isn't anyway:
    Code:
    #include <windows.h>
    #include <tchar.h>
    
    int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
    {
    HWND h=CreateWindowEx(0,_T("button"),_T("child of desktop"),WS_VISIBLE|WS_CHILD,
                          100,100,200,200,
                          GetDesktopWindow(),0,GetModuleHandle(0),0);
    
    MSG msg;
    while (GetMessage(&msg,0,0,0)>0)
      {
      if (msg.message==WM_LBUTTONDOWN)
        {
        DestroyWindow(h);
        break;
        }
      DispatchMessage(&msg);
      TranslateMessage(&msg);
      }
    return 0;
    }
    I suggested it to hopefully discover if the function would create a window if given valid parameters.
    Last edited by Ken Fitlike; 09-01-2005 at 05:10 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    I would....

    Overload the edits constructor to take the parents HWND (or in MFC CWnd). Add a member to hold it

    Then use GetModuleHandle(0) for the HINSTANCE

    Code:
    EditControl::EditControl(HWND hWndParent/*=NULL*/)
    {
    	m_hWndParent=hWndParent;
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    I tried what you said novacain, but I still don't get an edit box.
    I don't see how overloading the constructor helps either because I still have to use Window.g_hMainWindow as my parent window handle.
    I zipped my project all up and attached it to my post.
    Attached Files Attached Files

  13. #13
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by Ken Fitlike
    You can but it's not pretty - well this isn't anyway:
    Thanks. Guess I've learnt something new today!

    I always thought GetDesktopHandle() returned NULL, and so I always used NULL for hWndParent when creating a child window, which of course never worked.

    Edit:
    @sethjackson

    I believe your problem is you have 2 different CWindow objects.

    One is a global object defined at the top of main.cpp

    Code:
    #include <windows.h>
    #include "editcontrol.h"
    #include "window.h"
    #include "main.h"
    
    CEditControl EditControl;
    CWindow Window;
    Another is a local object defined in CEditControl's Create() method.

    Code:
    int CEditControl::Create()
    {
    	CWindow Window;
        
    	m_hEdit = CreateWindowEx(
    	          WS_EX_CLIENTEDGE,
    	          "EDIT",
    	          NULL,
    	          WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | 
                      ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | 
                      ES_WANTRETURN,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          Window.g_hMainWindow,
    	          (HMENU)ID_TEXT,
    	          Window.g_hThisInstance,
    	          NULL
    	          );

    So you're trying to create the edit control using a local CWindow object that didn't call CWindow::Create.

    Another possible issue: Your CWindow and CEditControl contructors don't initialize their data members to meaningful values.
    Last edited by Dante Shamest; 09-02-2005 at 01:36 PM.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    So how do I fix it? Make CEditControl a subclass of CWindow?
    Also what should my constructors be initializing?

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    I think Dante is right - you have 2 copies of Window, a global one and a local one.

    I've only had a cursory glance at your code, but something occured to me: in Winmain, you call Window::Create which wraps the standard CreateWindow API. During this call, the window recieves a WM_CREATE message, then returns the HWND. Unfortunately, with EditControl::Create, you rely on the g_hMainWindow member of the window, but I doubt this would be set as the CreateWIndow in Window::Create wont have returned. To script out your code, it would look more like

    1. Call CreateWindow in Window::Create
    2. Call CreateWindow in EditControl::Create using g_hMainWindow wheich is still unset
    3. EditControl::Create returns
    4. CreateWindow in Window::Create returns
    5. g_hMainWindow is now set to the right HWND.

    If this is indeed the case, then a good fix would be to ammend EditControl::Create like so
    Code:
    int EditControl::Create(HWND Parent = 0)
    {
    	
    	m_hEdit = CreateWindowEx(
    	          WS_EX_CLIENTEDGE,
    	          "EDIT",
    	          NULL,
    	          WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          CW_USEDEFAULT,
    	          Parent,
    	          (HMENU)ID_TEXT,
    	          GetModuleHandle(0),
    	          NULL
    	          );
    Then in the WndProc

    Code:
    case WM_CREATE:
    		     {     		
    		     	EditControl.Create(hwnd);
    		     }
    		     break;
    Like I said, I havent tried your code, but you may find what I suggest will help

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. edit box
    By beene in forum Windows Programming
    Replies: 3
    Last Post: 11-11-2006, 03:40 AM
  2. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 07:46 AM
  3. Multiline Edit Box Parser
    By The Brain in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2005, 06:15 PM
  4. setting fixed floats in edit box
    By WaterNut in forum Windows Programming
    Replies: 4
    Last Post: 08-13-2004, 09:13 AM
  5. Limiting Characters in Edit Box :: MFC
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 06-02-2002, 10:21 AM

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