Button im Child Fenster



  • Hallo,

    ich habe da mal eine hoffentlich kleine Frage.
    Ich probiere mich seid kurzen in dem Coden eine kleiner C++ GUI.
    Ich habe mir einige Tutorials angeschaut und auch versucht alles zu verstehen. Aber auf eine Antwort bei meinem Problem kahm ich leider noch nicht.

    Meine GUI soll darauf basieren das ich ein Parent Window hab in dem 2 Child Window sind. Eines soll als Menu mit Buttons funktionieren welches im 2 Child bestimmte Fenster erstellt.

    Fenster erstellen funktioniert einwandfrei. Jedoch habert es daran den Buttons klar zu machen in welchem Fenster sie erscheinen sollen. Ich poste einfach mal den Code vielleicht könntet ihr mir ja mal mein Denkfehler auftun. Da ich nichtmal annähernd weiß ob der Fehler im Aufbau liegt oder sonstiges.

    Derzeit wird nur der Button erstellt der im auch Parent Window erscheinen soll. Und der andere der im Child sein soll wird garnicht angezeigt. Dafür aber der vom Parent Window in beiden. 😞

    #include <Windows.h>
    #include <iostream>
    
    	HINSTANCE	hInstMenu;
    	HINSTANCE	hInstButton;
    	HWND		hWndMenu;
    
    	HWND		hWndButton1;
    	HWND		hWndButton2;
    
    LRESULT CALLBACK WndMainProc ( HWND hWndMain, UINT msg, WPARAM wParam, LPARAM lParam);
    
    bool InitWndClassEx( WNDCLASSEX *WndClassEx, HINSTANCE hInstance, const char* szClassName );
    
    const char szClassName[] = "MyClassEx";
    	  char szTitle[] = "Titel";
    
    using namespace std;
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    					LPSTR szCmdLine, int iCmdShow){
    
    	WNDCLASSEX	WndClassEx;
    	WNDCLASSEX	WndClassEx2;
    	HWND		hWndMain;
    	MSG			msg;
    
    			//========== REGISTRIERUNG ==========//
    
    		InitWndClassEx( &WndClassEx, hInstance, szClassName );
    		InitWndClassEx( &WndClassEx2, hInstance, "WndClassEx2" );
    
    			//========== MAIN FENSTER ==========//
    
    	hWndMain	=	CreateWindowEx(	WS_EX_CLIENTEDGE,
    						szClassName,
    						szTitle,
    						WS_OVERLAPPEDWINDOW,
    						100,
    						100,
    						700,
    						550,
    						NULL,									
    						NULL,
    						hInstance,
    						NULL );
    
    	if( hWndMain == NULL ){
    		MessageBox( NULL, "Haubtfenster konnte nicht erstellt werden", "Fehler", MB_OK);
    	}
    
    				//========== MENU FENSTER ==========//
    
    	hWndMenu	=	CreateWindowEx(	WS_EX_CLIENTEDGE,
    						"WndClassEx2",
    						"Menu",
    						WS_OVERLAPPEDWINDOW | WS_CHILD | WS_VISIBLE | DCX_CLIPSIBLINGS,
    						50,
    						50,
    						250,
    						400,
    						hWndMain,
    						NULL,
    						hInstMenu,
    						NULL );
    
    	if( hWndMenu == NULL ){
    		MessageBox( NULL, "Menu konnte nicht erstellt werden", "Fehler", MB_OK);
    	}
    
    	ShowWindow ( hWndMain, iCmdShow );
    
    	SetClassLong( hWndMenu, GCL_HBRBACKGROUND, (LONG) GetStockObject ( GRAY_BRUSH ) );
    
    	while(GetMessage( &msg, hWndMain, NULL, NULL) > NULL)
    	{
    		TranslateMessage ( &msg );
    		DispatchMessage ( &msg );
    	}
    
    return 0;
    
    }
    
    	LRESULT CALLBACK WndMainProc ( HWND hWndMain, UINT msg, WPARAM wParam, LPARAM lParam){
    		switch( msg ){
    		case WM_CREATE:
    			hWndButton1 = CreateWindow( "button", "Button 1", WS_VISIBLE | WS_CHILD,10, 30, 100, 20, hWndMenu, (HMENU) 1, hInstButton, NULL);
    
    			hWndButton2 = CreateWindow( "button", "Button 2", WS_VISIBLE | WS_CHILD,10, 10, 100, 20, hWndMain, (HMENU) 2, hInstButton, NULL);
    
    			break;
    		case WM_QUIT:
    			MessageBox (NULL, "Programm wurde beendet", "Programm Ende", MB_OK);
    			DestroyWindow( hWndMain );
    			break;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    
    		default:
    			return DefWindowProc( hWndMain, msg, wParam, lParam); 
    			break;
    		}
    	}
    
    bool InitWndClassEx( WNDCLASSEX *WndClassEx, HINSTANCE hInstance, const char* szClassName){
    	WndClassEx->cbSize			= sizeof( WNDCLASSEX );
    	WndClassEx->style			= NULL;
    	WndClassEx->lpfnWndProc		= WndMainProc;
    	WndClassEx->cbClsExtra		= NULL;
    	WndClassEx->cbWndExtra		= NULL;
    	WndClassEx->hInstance		= hInstance;
    	WndClassEx->hIcon			= NULL;
    	WndClassEx->hCursor			= LoadCursor ( NULL, IDC_ARROW );
    	WndClassEx->hbrBackground	= ( HBRUSH ) GetStockObject(BLACK_BRUSH);
    	WndClassEx->lpszMenuName	= NULL;
    	WndClassEx->lpszClassName	= szClassName;
    	WndClassEx->hIconSm			= NULL;
    
    	if( !RegisterClassEx( WndClassEx ) ){
    		MessageBox( NULL, "Klasse konnte nicht registriert werden", "Fehler", MB_OK); 
    		return false;
    	}
    
    }
    

    Ich Danke schonmal fürs anschauen. 🙂

    lg snappo



  • FlameDev wie?
    Finger weg.



  • Hehe joa,

    was ist daran so falsch?



  • Falsch ist daran nichts, der Stil ist nur ziemlich schlecht.
    So programmiert man nicht, erst recht nicht in C++!
    Man nutzt keine globalen Variablen, char-Arrays,...



  • Hey Snappo,

    du benutzt sowohl in hWndMain, als auch in hWndMenu die gleiche WndProc.

    Ich denke wenn du deinen Windows jeweils eine eigene WndProc schreibst, wird es so funktionieren wie du möchtest.

    Beispiel

    #include <windows.h>
    #include <windowsx.h>
    
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    LRESULT CALLBACK WindowProc2(HWND hWnd2, UINT message, WPARAM wParam, LPARAM lParam);
    
    HWND  hWnd, hWnd2, hButton1, hButton2;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
        WNDCLASSEX wc;
    	WNDCLASSEX wc2;
    
        ZeroMemory(&wc, sizeof(WNDCLASSEX));
    	ZeroMemory(&wc2, sizeof(WNDCLASSEX));
    
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WindowProc;
        wc.hInstance = 0;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
        wc.lpszClassName = "WindowClass1";
    
    	wc2.cbSize = sizeof(WNDCLASSEX);
        wc2.style = CS_HREDRAW | CS_VREDRAW;
        wc2.lpfnWndProc = WindowProc2;
        wc2.hInstance = 0;
        wc2.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc2.hbrBackground = (HBRUSH)COLOR_WINDOW;
        wc2.lpszClassName = "WindowClass2";
    
    	RegisterClassEx(&wc);
    	RegisterClassEx(&wc2);
    
        hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                              "WindowClass1",   
                              "Main",  
                              WS_VISIBLE | WS_OVERLAPPEDWINDOW,   
                              0,   
                              0,   
                              800,  
                              400,   
                              NULL,   
                              NULL,   
                              0,    
                              NULL); 
    
    	hWnd2 = CreateWindowEx(WS_EX_CLIENTEDGE,
                              "WindowClass2", 
                              "Menu",   
                              WS_VISIBLE | WS_OVERLAPPEDWINDOW | WS_CHILD,
                              550,    
                              5,    
                              200,  
                              350,    
                              hWnd,   
                              NULL,   
                              0,  
                              NULL); 
    
        ShowWindow(hWnd, nCmdShow);
    
        MSG msg;
    
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    }
    
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
        switch(message)
        {
    		case WM_CREATE:
    
    			return 0;
    
            case WM_DESTROY:       
                    PostQuitMessage(0);
                    return 0;
        }
    
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
    
    LRESULT CALLBACK WindowProc2(HWND hWnd2, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
        switch(message)
        {
    		case WM_CREATE:
    
    			hButton1 = CreateWindowEx(NULL,"button","button1",WS_VISIBLE | WS_CHILD, 20, 20,60,30,hWnd2,(HMENU) 1,0,0);
    			hButton1 = CreateWindowEx(NULL,"button","button2",WS_VISIBLE | WS_CHILD, 20, 60,60,30,hWnd2,(HMENU) 2,0,0);
    			return 0;
    
    		case WM_COMMAND:
    
    			switch( wParam )
    			{
    				case 1:
    					CreateWindowEx(NULL,"static","button1 clicked",WS_VISIBLE | WS_CHILD, 20, 20,100,30,hWnd,0,0,0);
    					return 0;
    
    				case 2:
    					CreateWindowEx(NULL,"static","button2 clicked",WS_VISIBLE | WS_CHILD, 20, 60,100,30,hWnd,0,0,0);
    					return 0;
    			}
    			return 0;
    
            case WM_DESTROY:       
                 PostQuitMessage(0);
                 return 0;
        }
    
        return DefWindowProc (hWnd2, message, wParam, lParam);
    }
    

    Grüße
    Jan.


Anmelden zum Antworten