Home > Back-end >  Minimalist CreateWindowEx fails
Minimalist CreateWindowEx fails

Time:04-12

this is just a minimalist console application that should show a windows created on the fly :

#include <windows.h>

void main()

     {
        WNDCLASSEX  _c4w = {0};

           _c4w.cbSize        = sizeof(WNDCLASSEX);
           //_c4w.hCursor       = ::LoadCursor(0, IDC_APPLICATION);
           //_c4w.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
           _c4w.hInstance     = 0;
           _c4w.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
           _c4w.lpszClassName = "c4w";

        HWND _h;
        
        if(!::RegisterClassEx(&_c4w))

            { _h = ::CreateWindowEx( 0, "c4w",
                          "Minimal Windows Application",
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                          0, 0, 640, 480,
                          HWND_DESKTOP,
                          0,
                          ::GetModuleHandle(0), NULL
                        );

               ::ShowWindow(_h, SW_SHOW);
            }
     ....
    }

unfortunatlity the RegisterClassEx function actually always fails...

I am using C Builder 5 and compiling a console Application optioned as MultiThreaded but no VCL...

CodePudding user response:

As @WhozCraig said Your window class doesn't have a wndproc. If you want to use winapi well, please study according to the official documentation. Your program needs a wndproc.

Here is how to create a blank window in MSDN.

Notice that the program does not explicitly call the WindowProc function, even though we said this is where most of the application logic is defined. Windows communicates with your program by passing it a series of messages.

In the code, wc.lpfnWndProc = WindowProc specifies the process callback function.

CodePudding user response:

I understood that a normal Win32 application using HWND and WNDPROC is not what I have done, but here is my solution :

HWND GetSTD()

 { static
   HWND     _result(0);
   static
   bool     _lock(false);
   static
   bool     _setup(false);

   while(_lock) ::Sleep(0);

   if(_setup == false)
     { _lock = true;

       OSVERSIONINFO _os;

       _os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
       ::GetVersionEx(&_os);

       if(_os.dwPlatformId != VER_PLATFORM_WIN32s)
         { char _title[1024];

           ::GetConsoleTitle(_title, sizeof(_title));

           _result = ::FindWindow(0, _title);

           if(_os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
             { _result = ::GetWindow(_result, GW_CHILD);

               if(_result != 0)
                 { char _class[128];

                   ::GetClassName(_result, _class, sizeof(_class));

                   while(::strcmp(_class, "ttyGrab") != 0)
                        { _result = ::GetNextWindow(_result, GW_HWNDNEXT);

                          if(_result != 0)
                            ::GetClassName(_result, _class, sizeof(_class));
                        }
                 }
             }
         }

       _setup = true;
       _lock = false;
     }

   return _result;
 }

Using the resulting HWND, we could draw over the console with the ::GetDC() and ::ReleaseDC() mechanism...

NB : static variables are used to ensure to return only 1 STD handle per call...

  • Related