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 #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;
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: { 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 ); hFont = CreateFont( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "Arial" ); SendMessage( hEdit, WM_SETFONT,( WPARAM ) hFont, 0 ); 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, PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 8, 0, 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 ); 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 ); } break; case WM_COMMAND: if( wPar == 1 ) { przekazanietekstu( true ); } if( wPar == 2 ) SetWindowText( hEdit, "dupapaspdada" ); break; case WM_PAINT: kon = BeginPaint( hwnd, & ps ); 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: DeleteObject( hFont ); DestroyWindow( hEdit ); DestroyWindow( hButton ); 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 ) { name = "Uzytkownik : "; } else { name = "Komputer : "; } char * tekst = new char[ nCharacters + 1 ]; if( nCharacters == 0 ) { SetWindowText( hpole, "Uzytkownik : " ); SetWindowText( hEdit, "" ); delete[] tekst; } else { GetWindowTextA( hEdit, tekst, nCharacters + 1 ); string retchar = name; retchar = retchar + tekst; SetWindowText( hpole, retchar.c_str() ); if( nr != 0 ) { SetWindowText( hEdit, "" ); } delete[] tekst; } }
kod nr 2 #include <windows.h> #include <commctrl.h> #include <gl/gl.h> #pragma comment (lib, "GLU32.lib") #pragma comment (lib, "opengl32.lib")
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";
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; 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 ); 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 ); EnableOpenGL( pole, & hDC, & hRC ); ShowWindow( hWnd, iCmdShow ); UpdateWindow( hWnd ); while( !quit ) { if( PeekMessage( & msg, NULL, 0, 0, PM_REMOVE ) ) { if( msg.message == WM_QUIT ) { quit = TRUE; } else { TranslateMessage( & msg ); DispatchMessage( & msg ); } } else { 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; } } DisableOpenGL( pole, hDC, hRC ); DestroyWindow( pole ); return msg.wParam; }
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 ); } }
void EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC ) { PIXELFORMATDESCRIPTOR pfd; int format; * hDC = GetDC( hWnd ); 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 ); * hRC = wglCreateContext( * hDC ); wglMakeCurrent( * hDC, * hRC ); }
void DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC ) { wglMakeCurrent( NULL, NULL ); wglDeleteContext( hRC ); ReleaseDC( hWnd, hDC ); }
|