Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[C++ + WINAPI + OPENGL] Okno w win32 a w nim okno opengl

Ostatnio zmodyfikowano 2016-02-27 22:31
Autor Wiadomość
Navvroth
Temat założony przez niniejszego użytkownika
[C++ + WINAPI + OPENGL] Okno w win32 a w nim okno opengl
» 2016-02-27 17:01:53
Witam mam problem z połączeniem winapi + opengl otóż mój zamysł jest taki mam okno a w nim kontrolki itd("nie mam z tym problemu"), ale jak chcę zrobić okno w oknie i to wewnętrzne podpiąć pod opengl to nic nie wychodzi a jak wychodzi to jest to oddzielne okno.
PS drugi kod to jakiegoś kolesia, starałem się sprawdzić czy w jego kodzie zadziała tak jak chciałbym ale nie.
kod nr 1
Tam gdzie X chciałbym okno z opengl ;s\

http://i.imgur.com/i4856ZX.jpg
A tu program kolesia zmieniony przeze mnie.
http://i.imgur.com/mufZH66.jpg
C/C++
#include <windows.h>
#include <commctrl.h>
#include <string>
#include "Header.h"
#include <GL\GL.h>
#include <GL\GLU.h>

#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "GLU32.lib")
HWND hEdit, hButton, hWnd, hpole, hglutokno;
HFONT hFont; //uchwyt do czcionki





CHAR szClassName[] = "OknoRodzica";
HINSTANCE * hInst;
HDC kon;
PAINTSTRUCT ps;
using namespace std;
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
char * tekstrozmowy = new char[ 2500 ];
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lStart, INT nShow )
{
    hInst = & hInstance;
    WNDCLASSEX wc;
    wc.hInstance = * hInst;
    wc.lpszClassName = szClassName;
    wc.lpfnWndProc = WndProc;
    wc.style = 0
    ;
    wc.cbSize = sizeof( WNDCLASSEX );
    wc.hIcon = LoadIcon( 0
    , IDI_APPLICATION );
    wc.hIconSm = LoadIcon( 0
    , IDI_APPLICATION );
    wc.hCursor = LoadCursor( 0
    , IDC_ARROW );
    wc.lpszMenuName = 0
    ;
    wc.cbClsExtra = 0
    ;
    wc.cbWndExtra = 0
    ;
    wc.hbrBackground =( HBRUSH ) COLOR_BACKGROUND;
    if( !RegisterClassEx( & wc ) ) return 0;
   
    hWnd = CreateWindowEx( 0
    , szClassName, "KCK"
    , WS_OVERLAPPEDWINDOW, 20
    , 20
    , 1024
    , 800
    , 0
    , 0
    , * hInst, 0
    );
    ShowWindow( hWnd, nShow );
    UpdateWindow( hWnd );
    MSG msgs;
    while( GetMessage( & msgs, 0
    , 0
    , 0
    ) )
    {
        TranslateMessage( & msgs );
        DispatchMessage( & msgs );
    }
    return msgs.wParam;
}


LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wPar, LPARAM lPar )
{
    switch( msg )
    {
    case WM_CREATE:
        //tworzymy kontrolke edit
        {
            hEdit = CreateWindowEx( WS_EX_CLIENTEDGE, WC_EDIT, "Napisz wiadomosc"
            ,
            WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_VSCROLL, 30
            , 550
            , 900
            , 150
            , hwnd,
            0
            , * hInst, 0
            );
            //ustawiamy czcionke
           
            hFont = CreateFont( 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , 0
            , "Arial"
            );
            SendMessage( hEdit, WM_SETFONT,( WPARAM ) hFont, 0 );
           
            //opengl
           
            hglutokno = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, 0
            , "nazwa",
            WS_CHILD | WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 30
            , 30
            , 500
            , 500
            , hwnd,( HMENU ) 3
            , * hInst,
            0
            );
            PIXELFORMATDESCRIPTOR pfd =
            {
                sizeof( PIXELFORMATDESCRIPTOR ),
                1,
                PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
                PFD_TYPE_RGBA, //The kind of framebuffer. RGBA or palette.
                32, //Colordepth of the framebuffer.
                0, 0, 0, 0, 0, 0,
                0,
                0,
                0,
                0, 0, 0, 0,
                24, //Number of bits for the depthbuffer
                8, //Number of bits for the stencilbuffer
                0, //Number of Aux buffers in the framebuffer.
                PFD_MAIN_PLANE,
                0,
                0, 0, 0
            };
           
            HDC ourWindowHandleToDeviceContext = GetDC( hglutokno );
           
            int letWindowsChooseThisPixelFormat;
            letWindowsChooseThisPixelFormat = ChoosePixelFormat( ourWindowHandleToDeviceContext, & pfd );
            SetPixelFormat( ourWindowHandleToDeviceContext, letWindowsChooseThisPixelFormat, & pfd );
           
            HGLRC ourOpenGLRenderingContext = wglCreateContext( ourWindowHandleToDeviceContext );
            wglMakeCurrent( ourWindowHandleToDeviceContext, ourOpenGLRenderingContext );
            wglDeleteContext( ourOpenGLRenderingContext );
           
           
           
           
           
            //tworzymy przyciski!!
           
            hButton = CreateWindowEx( 0
            , WC_BUTTON, "wyslij"
            ,
            WS_CHILD | WS_VISIBLE, 450
            , 710
            , 570
            , 40
            , hwnd,( HMENU ) 1
            , * hInst, 0
            );
            hpole = CreateWindowEx( WS_EX_CLIENTEDGE, WC_EDIT, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
            WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 550, 30, 400, 450, hwnd,( HMENU ) 2, * hInst, NULL );
           
           
            /* hpole = CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, "tekst"
            ,
            WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_VSCROLL, 550
            , 30
            , 400
            , 450
            , hwnd,
            0
            , *hInst, 0
            );*/
           
        }
        break;
       
    case WM_COMMAND:
        //obsluga przycisku
       
        if( wPar == 1 )
        {
            przekazanietekstu( true );
        }
        if( wPar == 2
        ) SetWindowText( hEdit, "dupapaspdada"
             );
        //funkcje SetWindowText czyscimy tekst w kontrolce
       
        break;
    case WM_PAINT:
        kon = BeginPaint( hwnd, & ps );
       
        //Tutaj ta funkcja:
        //TextOut(kon, 50, 50, "Jakis tekst", 11);
       
        EndPaint( hwnd, & ps );
        break;
    case WM_KEYDOWN:
        {
            switch(( int ) wPar )
            {
            case VK_RETURN:
               
                przekazanietekstu( true );
                MessageBox( hwnd, "Wciśnięto Entera", "Yeah", MB_ICONINFORMATION );
                break;
            case VK_ESCAPE:
                DestroyWindow( hwnd );
                break;
                default:
                break;
            }
            break;
        }
    case WM_DESTROY: //przed usuni?ciem okna
       
        DeleteObject( hFont ); //zwalniamy czcionk?
       
        DestroyWindow( hEdit ); //zwalniamy kontrolke edit
       
        DestroyWindow( hButton ); //zwalniamy przycisk
        DestroyWindow( hpole );
        DestroyWindow( hglutokno );
        PostQuitMessage( 0
        );
        break;
        default:
        return DefWindowProc( hwnd, msg, wPar, lPar );
       
    }
    return 0;
   
}
void przekazanietekstu( bool nr )
{
    int nCharacters = GetWindowTextLength( hEdit );
    string name;
    if( nr == true ) // uzytkownik
    {
        name = "Uzytkownik : ";
       
    }
    else // komputer
    {
        name = "Komputer : ";
       
    }
    char * tekst = new char[ nCharacters + 1 ];
   
   
   
    if( nCharacters == 0 )
    {
       
        //MessageBox(NULL, " zyjemy nadal" , NULL, NULL);
        SetWindowText( hpole, "Uzytkownik : " );
        SetWindowText( hEdit, "" );
        delete[] tekst;
       
    }
    else
    {
       
        GetWindowTextA( hEdit, tekst, nCharacters + 1 );
        string retchar = name;
        retchar = retchar + tekst;
       
        // MessageBox(NULL,retchar.c_str(), NULL, NULL);
        SetWindowText( hpole, retchar.c_str() );
        if( nr != 0 )
        {
            SetWindowText( hEdit, "" );
        }
       
        delete[] tekst;
    }
   
   
   
   
   
   
}

kod nr 2
C/C++
#include <windows.h>
#include <commctrl.h>
#include <gl/gl.h>
#pragma comment (lib, "GLU32.lib")
#pragma comment (lib, "opengl32.lib")
// Function Declarations

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
void EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC );
void DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC );
CHAR szClassName[] = "OknoRodzica";
// WinMain
HWND pole;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int iCmdShow )
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL quit = FALSE;
    float theta = 0.0f;
   
    // register window class
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground =( HBRUSH ) GetStockObject( BLACK_BRUSH );
    wc.lpszMenuName = NULL;
    wc.lpszClassName = L"GLSample";
    RegisterClass( & wc );
   
    // create main window
    hWnd = CreateWindow(
    L"GLSample", L"OpenGL Sample",
    WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
    0, 0, 1024, 800,
    NULL, NULL, hInstance, NULL );
   
   
    pole = CreateWindow(
    L"GLSample", 0,
    WS_VISIBLE | WS_BORDER,
    0, 0, 256, 256,
    hWnd, NULL, hInstance, NULL );
   
    HWND hpole = CreateWindowEx( WS_EX_CLIENTEDGE, WC_EDIT, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
    WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 550, 30, 400, 450, hWnd,( HMENU ) 2, hInstance, NULL );
    // enable OpenGL for the window
    EnableOpenGL( pole, & hDC, & hRC );
   
    // program main loop
    ShowWindow( hWnd, iCmdShow );
    UpdateWindow( hWnd );
    while( !quit )
    {
       
        // check for messages
        if( PeekMessage( & msg, NULL, 0, 0, PM_REMOVE ) )
        {
           
            // handle or dispatch messages
            if( msg.message == WM_QUIT )
            {
                quit = TRUE;
            }
            else
            {
                TranslateMessage( & msg );
                DispatchMessage( & msg );
            }
           
        }
        else
        {
           
            // OpenGL animation code goes here
           
            glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
            glClear( GL_COLOR_BUFFER_BIT );
           
            glPushMatrix();
            glRotatef( theta, 0.0f, 0.0f, 1.0f );
            glBegin( GL_TRIANGLES );
            glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
            glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, - 0.5f );
            glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( - 0.87f, - 0.5f );
            glEnd();
            glPopMatrix();
           
            SwapBuffers( hDC );
           
            theta += 1.0f;
           
        }
       
    }
   
    // shutdown OpenGL
    DisableOpenGL( pole, hDC, hRC );
   
    // destroy the window explicitly
    DestroyWindow( pole );
   
    return msg.wParam;
   
}

// Window Procedure

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
   
    switch( message )
    {
       
    case WM_CREATE:
        return 0;
       
    case WM_CLOSE:
        PostQuitMessage( 0 );
        return 0;
       
    case WM_DESTROY:
        return 0;
       
    case WM_KEYDOWN:
        switch( wParam )
        {
           
        case VK_ESCAPE:
            PostQuitMessage( 0 );
            return 0;
           
        }
        return 0;
       
        default:
        return DefWindowProc( hWnd, message, wParam, lParam );
       
    }
   
}

// Enable OpenGL

void EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
{
    PIXELFORMATDESCRIPTOR pfd;
    int format;
   
    // get the device context (DC)
    * hDC = GetDC( hWnd );
   
    // set the pixel format for the DC
    ZeroMemory( & pfd, sizeof( pfd ) );
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    format = ChoosePixelFormat( * hDC, & pfd );
    SetPixelFormat( * hDC, format, & pfd );
   
    // create and enable the render context (RC)
    * hRC = wglCreateContext( * hDC );
    wglMakeCurrent( * hDC, * hRC );
   
}

// Disable OpenGL

void DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
{
    wglMakeCurrent( NULL, NULL );
    wglDeleteContext( hRC );
    ReleaseDC( hWnd, hDC );
}


P-145356
Gabes
» 2016-02-27 21:08:40
WS_CHILD

C/C++
pole = CreateWindow(
"GLSample", 0,
WS_VISIBLE | WS_BORDER | WS_CHILD,
0, 0, 256, 256,
hWnd, NULL, hInstance, NULL );
P-145368
Navvroth
Temat założony przez niniejszego użytkownika
» 2016-02-27 22:31:41
Dziękuję bardzo <3. Sam o tym nie pomyślałem :) zamykam
P-145375
« 1 »
  Strona 1 z 1