Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Richedit control in Win32 C++

Richedit control in Win32 C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T tareqsiraj

    Hi ... can any1 tell me what is wrong with the following code? i'm sure its some silly mistake that i made ... I am trying to create a Richedit control in a window. I have included richedit.h and my code compiles successfully. but _hWnd remains NULL after this call :( ... i am using Microsoft VC++ 6 with the Platform SDK 2003 installed. please help me out. thankx in advance.

    _hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
    RICHEDIT_CLASS,
    TEXT("AA"),
    WS_CHILD | WS_VISIBLE | ES_MULTILINE,
    10, 10,
    100, 100,
    _parent,
    0,
    (HINSTANCE)::GetModuleHandle(0),
    0);

    -Tareq

    A Offline
    A Offline
    ashxly
    wrote on last edited by
    #3

    Did you init richedit control?? if you use MFC, please try to call AfxInitRichedit() in App::InitInstance(). maybe that's the point.

    T 1 Reply Last reply
    0
    • M Michael Dunn

      What's the value of GetLastError() right after the call? --Mike-- Latest blog entry: *drool* (Alyson) [May 10] Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

      T Offline
      T Offline
      tareqsiraj
      wrote on last edited by
      #4

      After using the following code from MSDN to retrieve the last error right after CreateWindowEx, i get "Invalid Window Handle". :((

          LPVOID lpMsgBuf;
      FormatMessage( 
      	FORMAT\_MESSAGE\_ALLOCATE\_BUFFER | 
      	FORMAT\_MESSAGE\_FROM\_SYSTEM | 
      	FORMAT\_MESSAGE\_IGNORE\_INSERTS,
      	NULL,
      	GetLastError(),
      	MAKELANGID(LANG\_NEUTRAL, SUBLANG\_DEFAULT), // Default language
      	(LPTSTR) &lpMsgBuf,
      	0,
      	NULL );
      MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB\_OK | MB\_ICONINFORMATION );
      LocalFree( lpMsgBuf );
      

      -Tareq

      R 1 Reply Last reply
      0
      • A ashxly

        Did you init richedit control?? if you use MFC, please try to call AfxInitRichedit() in App::InitInstance(). maybe that's the point.

        T Offline
        T Offline
        tareqsiraj
        wrote on last edited by
        #5

        No MFC. Just Win32. -Tareq

        A 1 Reply Last reply
        0
        • T tareqsiraj

          No MFC. Just Win32. -Tareq

          A Offline
          A Offline
          ashxly
          wrote on last edited by
          #6

          try to use "InitCommonControlsEx();" in your InitInstance() function.

          T 1 Reply Last reply
          0
          • A ashxly

            try to use "InitCommonControlsEx();" in your InitInstance() function.

            T Offline
            T Offline
            tareqsiraj
            wrote on last edited by
            #7

            nope :( its the same. -Tareq

            A 1 Reply Last reply
            0
            • T tareqsiraj

              nope :( its the same. -Tareq

              A Offline
              A Offline
              ashxly
              wrote on last edited by
              #8

              try ::LoadLibrary("Riched20.dll"); For win95 the rich edit control lib is "Riched32.dll", for win98 win2k&winxp, the lib is "Riched20.dll"

              T 1 Reply Last reply
              0
              • A ashxly

                try ::LoadLibrary("Riched20.dll"); For win95 the rich edit control lib is "Riched32.dll", for win98 win2k&winxp, the lib is "Riched20.dll"

                T Offline
                T Offline
                tareqsiraj
                wrote on last edited by
                #9

                tried that also :( ... i'm sure its some silly mistake. it cant be that complicated.

                HINSTANCE hLib;
                hLib = LoadLibrary("Riched20.dll");
                if (!hLib)
                {
                hLib = LoadLibrary("Riched32.dll");
                if (!hLib)
                {
                MessageBox(0, "Richedit control failed to initialize!", "Editor", MB_ICONERROR);
                exit(0);
                }
                else
                strcpy(_className, "RichEdit");
                }
                else
                {
                strcpy(_className, "RICHEDIT_CLASS");
                }

                -Tareq

                1 Reply Last reply
                0
                • T tareqsiraj

                  After using the following code from MSDN to retrieve the last error right after CreateWindowEx, i get "Invalid Window Handle". :((

                      LPVOID lpMsgBuf;
                  FormatMessage( 
                  	FORMAT\_MESSAGE\_ALLOCATE\_BUFFER | 
                  	FORMAT\_MESSAGE\_FROM\_SYSTEM | 
                  	FORMAT\_MESSAGE\_IGNORE\_INSERTS,
                  	NULL,
                  	GetLastError(),
                  	MAKELANGID(LANG\_NEUTRAL, SUBLANG\_DEFAULT), // Default language
                  	(LPTSTR) &lpMsgBuf,
                  	0,
                  	NULL );
                  MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB\_OK | MB\_ICONINFORMATION );
                  LocalFree( lpMsgBuf );
                  

                  -Tareq

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #10

                  This seems to suggest that the parent window has not been created yet. Where are you calling the function from? Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                  Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                  T 1 Reply Last reply
                  0
                  • R Ryan Binns

                    This seems to suggest that the parent window has not been created yet. Where are you calling the function from? Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                    Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                    T Offline
                    T Offline
                    tareqsiraj
                    wrote on last edited by
                    #11

                    Here is the code ...

                    #define WIN32_LEAN_AND_MEAN
                    #include < windows.h >
                    #include < stdlib.h >
                    #include < richedit.h >
                    #include < commctrl.h >

                    HWND _rich;
                    HMODULE _hRich;
                    char className[14];

                    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
                    {
                    switch(msg)
                    {
                    case WM_CREATE:
                    _rich = CreateWindowEx(WS_EX_CLIENTEDGE,
                    className,
                    "Sample Text",
                    WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_BORDER | WS_VSCROLL,
                    10, 10,
                    200, 200,
                    hWnd,
                    0,
                    GetModuleHandle(0),
                    0);

                    	LPVOID lpMsgBuf;	
                    	FormatMessage( 		
                    		FORMAT\_MESSAGE\_ALLOCATE\_BUFFER | 		
                    		FORMAT\_MESSAGE\_FROM\_SYSTEM | 		
                    		FORMAT\_MESSAGE\_IGNORE\_INSERTS,		
                    		NULL,		
                    		GetLastError(),		
                    		MAKELANGID(LANG\_NEUTRAL, SUBLANG\_DEFAULT), // Default language		
                    		(LPTSTR) &lpMsgBuf,		
                    		0,		
                    		NULL );	
                    	MessageBox( 
                    		NULL, 
                    		(LPCTSTR)lpMsgBuf, 
                    		"Error", 
                    		MB\_OK | MB\_ICONINFORMATION 
                    		);	
                    	LocalFree( lpMsgBuf );
                    	break;
                    
                    case WM\_CLOSE:
                    	DestroyWindow(hWnd);
                    	break;
                    
                    case WM\_DESTROY:
                    	PostQuitMessage(0);
                    	break;
                    }
                    
                    return DefWindowProc(hWnd, msg, wParam, lParam);
                    

                    }

                    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
                    {
                    WNDCLASSEX wc;
                    HWND _hWnd;

                    ZeroMemory(className, 14);
                    InitCommonControls();
                    
                    \_hRich = LoadLibrary("RICHED20.DLL");
                    if (!\_hRich)
                    {
                    	\_hRich = LoadLibrary("RICHED32.DLL");
                    	if (!\_hRich)
                    	{
                    		MessageBox(0, "Richedit control not loaded!", "Error", MB\_ICONERROR);
                    		exit(0);
                    	}
                    	else
                    	{
                    		strcpy(className, "RichEdit");
                    	}
                    }
                    else
                    {
                    	strcpy(className, "RICHEDIT\_CLASS");
                    }
                    
                    wc.cbClsExtra = 0;
                    wc.cbSize = sizeof(WNDCLASSEX);
                    wc.cbWndExtra = 0;
                    wc.hbrBackground = (HBRUSH)(COLOR\_WINDOW);
                    wc.hCursor = 0;
                    wc.hIcon = 0;
                    wc.hIconSm = 0;
                    wc.hInstance = hInstance;
                    wc.lpfnWndProc = (WNDPROC)WndProc;
                    wc.lpszClassName = "MainWindowClass";
                    wc.lpszMenuName = 0;
                    wc.style = 0;
                    
                    if (!RegisterClassEx(&wc))
                    {
                    	MessageBox(0, "Error", "", MB\_ICONERROR);
                    	exit(0);
                    }
                    
                    \_hWnd = CreateWindowEx(WS\_EX\_WINDOWEDGE,
                    				"MainWindowClass",
                    				"",
                    				WS\_OVERLAPPEDWINDOW,
                    				0, 0,
                    				300, 300,
                    				0, 0,
                    				hInstance, 0);
                    
                    ShowWindow(\_hWnd, SW\_SHOW);
                    UpdateWindow(\_hWnd);
                    
                    MSG msg;
                    
                    while (GetMessage(&msg, 0, 0, 0) > 0)
                    {
                    	TranslateMessage(&msg);
                    	DispatchMessage(&msg);
                    }
                    return msg.wParam;
                    

                    }

                    -Tareq

                    R 1 Reply Last reply
                    0
                    • T tareqsiraj

                      Here is the code ...

                      #define WIN32_LEAN_AND_MEAN
                      #include < windows.h >
                      #include < stdlib.h >
                      #include < richedit.h >
                      #include < commctrl.h >

                      HWND _rich;
                      HMODULE _hRich;
                      char className[14];

                      LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
                      {
                      switch(msg)
                      {
                      case WM_CREATE:
                      _rich = CreateWindowEx(WS_EX_CLIENTEDGE,
                      className,
                      "Sample Text",
                      WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_BORDER | WS_VSCROLL,
                      10, 10,
                      200, 200,
                      hWnd,
                      0,
                      GetModuleHandle(0),
                      0);

                      	LPVOID lpMsgBuf;	
                      	FormatMessage( 		
                      		FORMAT\_MESSAGE\_ALLOCATE\_BUFFER | 		
                      		FORMAT\_MESSAGE\_FROM\_SYSTEM | 		
                      		FORMAT\_MESSAGE\_IGNORE\_INSERTS,		
                      		NULL,		
                      		GetLastError(),		
                      		MAKELANGID(LANG\_NEUTRAL, SUBLANG\_DEFAULT), // Default language		
                      		(LPTSTR) &lpMsgBuf,		
                      		0,		
                      		NULL );	
                      	MessageBox( 
                      		NULL, 
                      		(LPCTSTR)lpMsgBuf, 
                      		"Error", 
                      		MB\_OK | MB\_ICONINFORMATION 
                      		);	
                      	LocalFree( lpMsgBuf );
                      	break;
                      
                      case WM\_CLOSE:
                      	DestroyWindow(hWnd);
                      	break;
                      
                      case WM\_DESTROY:
                      	PostQuitMessage(0);
                      	break;
                      }
                      
                      return DefWindowProc(hWnd, msg, wParam, lParam);
                      

                      }

                      int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
                      {
                      WNDCLASSEX wc;
                      HWND _hWnd;

                      ZeroMemory(className, 14);
                      InitCommonControls();
                      
                      \_hRich = LoadLibrary("RICHED20.DLL");
                      if (!\_hRich)
                      {
                      	\_hRich = LoadLibrary("RICHED32.DLL");
                      	if (!\_hRich)
                      	{
                      		MessageBox(0, "Richedit control not loaded!", "Error", MB\_ICONERROR);
                      		exit(0);
                      	}
                      	else
                      	{
                      		strcpy(className, "RichEdit");
                      	}
                      }
                      else
                      {
                      	strcpy(className, "RICHEDIT\_CLASS");
                      }
                      
                      wc.cbClsExtra = 0;
                      wc.cbSize = sizeof(WNDCLASSEX);
                      wc.cbWndExtra = 0;
                      wc.hbrBackground = (HBRUSH)(COLOR\_WINDOW);
                      wc.hCursor = 0;
                      wc.hIcon = 0;
                      wc.hIconSm = 0;
                      wc.hInstance = hInstance;
                      wc.lpfnWndProc = (WNDPROC)WndProc;
                      wc.lpszClassName = "MainWindowClass";
                      wc.lpszMenuName = 0;
                      wc.style = 0;
                      
                      if (!RegisterClassEx(&wc))
                      {
                      	MessageBox(0, "Error", "", MB\_ICONERROR);
                      	exit(0);
                      }
                      
                      \_hWnd = CreateWindowEx(WS\_EX\_WINDOWEDGE,
                      				"MainWindowClass",
                      				"",
                      				WS\_OVERLAPPEDWINDOW,
                      				0, 0,
                      				300, 300,
                      				0, 0,
                      				hInstance, 0);
                      
                      ShowWindow(\_hWnd, SW\_SHOW);
                      UpdateWindow(\_hWnd);
                      
                      MSG msg;
                      
                      while (GetMessage(&msg, 0, 0, 0) > 0)
                      {
                      	TranslateMessage(&msg);
                      	DispatchMessage(&msg);
                      }
                      return msg.wParam;
                      

                      }

                      -Tareq

                      R Offline
                      R Offline
                      Ryan Binns
                      wrote on last edited by
                      #12

                      This code won't work. You'll get an error message saying that you're using an unknown class. You need to change

                      strcpy(className, "RICHEDIT_CLASS");

                      to

                      strcpy(className, RICHEDIT_CLASS);

                      I tried this and it worked fine. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                      Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                      T 1 Reply Last reply
                      0
                      • R Ryan Binns

                        This code won't work. You'll get an error message saying that you're using an unknown class. You need to change

                        strcpy(className, "RICHEDIT_CLASS");

                        to

                        strcpy(className, RICHEDIT_CLASS);

                        I tried this and it worked fine. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                        Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                        T Offline
                        T Offline
                        tareqsiraj
                        wrote on last edited by
                        #13

                        :-D thankx a lot man ... i knew it was a silly mistake!!! -Tareq

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups