Search code examples
c++visual-studio-2013window

creating a window in c++


I have some experience with c++, and other languages but I've only made games before using c++ and I need to make a OBS plugin.. I was wondering if anyone could help..

I'm trying to create a window with -

    int nHeight = 500;
int nWidth = 500;

#define metaData(lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)\
    CreateWindowExA(0L, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)

if (GetAsyncKeyState(VK_F5))
    {
        //MsgeBox::myMessage::createMessage(NULL, (LPCWSTR)L"Hello", (LPCWSTR)L"I See You.", MB_ICONWARNING | MB_CANCELTRYCONTINUE);
        #define CreateWindow metaData;
    }

It doesn't create a window, and it doesn't give an error.. when I call the messagebox it only appears once I try to close the window.. why is that?

How could I create a seperate window?

the tutorial im following is - https://msdn.microsoft.com/en-us/library/bb384843.aspx


Solution

  • You have to create a message procedure then respond to key messages. For example

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
    {
        switch (msg)
        {
        case WM_KEYDOWN:
            if (wp == VK_F5)
            CreateWindow(L"ChildClass", L"Child window", 
                        WS_VISIBLE | WS_POPUPWINDOW | WS_CAPTION, 
                        0, 0, 300, 200, hwnd, 0, 0, 0);
            break;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
        return DefWindowProc(hwnd, msg, wp, lp);
    }
    

    Note that you have to register a second class name for "ChildClass" and then create a different message procedure for this child class.

    Then you add a separate function called ChildProc which is similar to WndProc. For example:

    #define UNICODE
    #include <Windows.h>
    
    HINSTANCE g_hinstance = 0;
    
    LRESULT CALLBACK ChildProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
    {
        switch (msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
        return DefWindowProc(hwnd, msg, wp, lp);
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
    {
        switch (msg)
        {
        case WM_KEYDOWN:
        {
            if (wp == VK_F5)
            {
                MessageBox(0, L"VK_F5 detected", 0, 0);
                if (!CreateWindow(L"ChildClass", L"ChildTitle",
                    WS_VISIBLE | WS_POPUPWINDOW | WS_CAPTION, 100, 100, 300, 200,
                    hwnd, 0, g_hinstance, 0))
                {
                    DWORD err = GetLastError();
                    wchar_t buf[100];
                    wsprintf(buf, L"%d\n", err);
                    MessageBox(0, buf, 0, 0);
                }
            }
            break;
        }
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
        return DefWindowProc(hwnd, msg, wp, lp);
    }
    
    int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
    {
        WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
        wcex.style = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc = WndProc;
        wcex.hInstance = hInstance;
        wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
        wcex.lpszClassName = L"MainClass";
        RegisterClassEx(&wcex);
    
        wcex.lpszClassName = L"ChildClass";
        wcex.lpfnWndProc = ChildProc;
        RegisterClassEx(&wcex);
    
        CreateWindow(L"MainClass", L"MainTitle", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
            0, 0, 600, 400, 0, 0, hInstance, 0);
    
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return (int)msg.wParam;
    }