如何修复调整大小功能

时间:2014-01-21 10:01:22

标签: winapi opengl c99 c89

这是我的所有代码。以zx开头的函数就是这样,当我完成后,我可以根据这些函数快速组建一个自定义库。我需要帮助的功能是 zxResizeGL ,它仅用于 WM_SIZE 事件atm。

#include <windows.h>
#include <gl/gl.h>

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);

/* Necessary value we need access to */
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define ZX_THIS_INSTANCE ((HINSTANCE)&__ImageBase)
/* We define 2 here so dev can retrieve the FPN b4 it is multiplied by 100 */
#define ZX__PERCENT( NUM, OF ) ( ( NUM ) / ( OF ) )
#define ZX_PERCENT( NUM, OF ) ( ZX__PERCENT( NUM, OF ) * 100 )

WNDCLASSEX zxGetOSWindowClassEX( void );

BOOL zxInitOSClasses( void );
HWND zxNewOSWindow( int w, int h, int x, int y, char const *text );    
HDC   hDC = NULL;
HGLRC hRC = NULL;

void zxResizeGL(int width, int height )
{
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0, width, 0, height, -1.0, 1.0);
   glFlush();
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hwnd;
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0,
          width = 256,
         height = width,
              w = ZX__PERCENT( width - 20, width ),
              h = w;
    if ( !zxInitOSClasses() )
      return 0;

    hwnd = zxNewOSWindow( 480, 480, 0, 0, "OpenGL Sample" );

    ShowWindow(hwnd, nCmdShow);

    /* enable OpenGL for the window */
    EnableOpenGL(hwnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            switch ( msg.message )
            {
            case WM_QUIT:
              bQuit = TRUE;
              break;
            case WM_SIZE:
              /* This is where I'm trying to peform the resize */
               width = LOWORD( msg.lParam );
              height = HIWORD( msg.lParam );
                   w = ZX__PERCENT( width  - 20, width  );
                   h = ZX__PERCENT( height - 20, height );
              zxResizeGL( width, height );
              break;
            default:
              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,   h );
                glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(    w,  -h );
                glColor3f(0.0f, 0.0f, 1.0f);   glVertex2f(   -w,  -h );

            glEnd();

            glPopMatrix();

            SwapBuffers(hDC);

            theta += 1.0f;
            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL(hwnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow(hwnd);

    return msg.wParam;
}

从主代码中断掉的函数的定义。

WNDCLASSEX zxGetOSWindowClassEX( void )
{
  WNDCLASSEX    wx = {0};
         wx.cbSize = sizeof( WNDCLASSEX );
          wx.style = CS_OWNDC;
    wx.lpfnWndProc = WindowProc;
      wx.hInstance = ZX_THIS_INSTANCE;
          wx.hIcon = LoadIcon(   NULL, IDI_APPLICATION );
        wx.hCursor = LoadCursor( NULL, IDC_ARROW );
  wx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  wx.lpszClassName = "zxOSWindow";
        wx.hIconSm = LoadIcon(   NULL, IDI_APPLICATION );
  return wx;
}

BOOL zxInitOSClasses( void )
{
  WNDCLASSEX wx = zxGetOSWindowClassEX();
  if ( !RegisterClassEx( &wx ) )
    return 0;
  return 1;
}

HWND zxNewOSWindow( int w, int h, int x, int y, char const *text )
{
  WNDCLASSEX wx = zxGetOSWindowClassEX();
  return CreateWindowEx(
    0, wx.lpszClassName, text,
    WS_OVERLAPPEDWINDOW,
    x, y, w, h, NULL, NULL,
    wx.hInstance, NULL );
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
        break;

        case WM_DESTROY:
            return 0;

        case WM_KEYDOWN:
        {
            switch (wParam)
            {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                break;
            }
        }
        break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
    PIXELFORMATDESCRIPTOR pfd;

    int iFormat;

    /* 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;

    iFormat = ChoosePixelFormat(*hDC, &pfd);

    SetPixelFormat(*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext(*hDC);

    wglMakeCurrent(*hDC, *hRC);
}

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

2 个答案:

答案 0 :(得分:0)

在尝试了断点后,我注意到我没有收到WM_SIZE事件,将调查执行调整大小的其他方法。现在我将把 zxResizeGL 放在绘图循环中

答案 1 :(得分:0)

Microsoft says WM_SIZE条消息已发送(未已发布),因此通过WindowProc()进入。

PeekMessage()只能检索发布的消息。

处理WM_SIZE中的WindowProc()