Ejemplo n.º 1
0
///////////////////////////////////////////////////////////////////////////////
// main function of a windows application
///////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdArgs, int cmdShow)
{
    // register UpDown control from comctl32.dll brfore creating windows
    INITCOMMONCONTROLSEX commonCtrls;
    commonCtrls.dwSize = sizeof(commonCtrls);
    commonCtrls.dwICC = ICC_UPDOWN_CLASS;
    ::InitCommonControlsEx(&commonCtrls);

    // create model and view components for controllers
    ModelGL modelGL;
    Win::ViewGL viewGL;
    Win::ViewFormGL viewFormGL(&modelGL);

    // create main window
    Win::ControllerMain mainCtrl;
    Win::Window mainWin(hInst, L"OpenGL Projection Matrix", 0, &mainCtrl);
    mainWin.setMenuName(MAKEINTRESOURCE(IDR_MENU_MAIN));
    mainWin.setWindowStyleEx(WS_EX_WINDOWEDGE);
    if(mainWin.create())
        Win::log("Main window is created.");
    else
        Win::log("[ERROR] Failed to create main window.");

    // create OpenGL rendering window as a child
    Win::ControllerGL glCtrl(&modelGL, &viewGL);
    Win::Window glWin(hInst, L"WindowGL", mainWin.getHandle(), &glCtrl);
    glWin.setClassStyle(CS_OWNDC);
    glWin.setWindowStyle(WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
    glWin.setWidth(300);
    glWin.setHeight(600);
    if(glWin.create())
        Win::log("OpenGL child window is created.");
    else
        Win::log("[ERROR] Failed to create OpenGL window.");

    // create a child dialog box contains controls
    Win::ControllerFormGL formCtrl(&modelGL, &viewFormGL);
    Win::DialogWindow glDialog(hInst, IDD_FORMVIEW, mainWin.getHandle(), &formCtrl);
    glDialog.setWidth(300);
    glDialog.setHeight(600);
    if(glDialog.create())
        Win::log("OpenGL form dialog is created.");
    else
        Win::log("[ERROR] Failed to create OpenGL form dialog.");

    // place windows in the right position ================
    const int GL_WIDTH = 300;
    const int GL_HEIGHT = 600;
    const int DIALOG_WIDTH = 365;
    int winWidth;
    int winHeight;

    RECT rect = {0, 0, GL_WIDTH, GL_HEIGHT};                            // exact client size for OpenGL display, 300x600
    DWORD style = ::GetWindowLong(glWin.getHandle(), GWL_STYLE);        // get current window style
    DWORD styleEx = ::GetWindowLong(glWin.getHandle(), GWL_EXSTYLE);    // get current extended window style

    // adjust window size of glWin because camera display should have 300x600 client area
    // It modifies rect struct.
    ::AdjustWindowRectEx(&rect, style, FALSE, styleEx);
    winWidth = rect.right - rect.left;
    winHeight = rect.bottom - rect.top;

    // set glWin first with adjusted window size
    ::SetWindowPos(glWin.getHandle(), 0, 0, 0, winWidth, winHeight, SWP_NOZORDER);

    // set glDialog
    ::SetWindowPos(glDialog.getHandle(), 0, winWidth, 0, DIALOG_WIDTH, winHeight, SWP_NOZORDER);

    // compute window width and height
    winWidth = (rect.right - rect.left) + DIALOG_WIDTH;
    winHeight = rect.bottom - rect.top;

    // set mainWin
    rect.left = 0;
    rect.right = winWidth;
    rect.top = 0;
    rect.bottom = winHeight;
    style = ::GetWindowLong(mainWin.getHandle(), GWL_STYLE);
    styleEx = ::GetWindowLong(mainWin.getHandle(), GWL_EXSTYLE);
    ::AdjustWindowRectEx(&rect, style, TRUE, styleEx);
    ::SetWindowPos(mainWin.getHandle(), 0, 0, 0, (rect.right-rect.left), (rect.bottom-rect.top), SWP_NOZORDER);

    // show all windows
    glWin.show();
    glDialog.show();
    mainWin.show();


    // main message loop //////////////////////////////////////////////////////
    int exitCode;
    HACCEL hAccelTable = 0;
    //hAccelTable = ::LoadAccelerators(hInst, MAKEINTRESOURCE(ID_ACCEL));
    exitCode = mainMessageLoop(hAccelTable);

    Win::log("Application is terminated.");
    return exitCode;
}
Ejemplo n.º 2
0
///////////////////////////////////////////////////////////////////////////////
// main function of a windows application
///////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdArgs, int cmdShow)
{
    // set log mode: FILE, DIALOG or BOTH
    //Win::logMode(Win::LOG_MODE_BOTH);
    Win::logMode(Win::LOG_MODE_FILE); // LOG_MODE_FILE is default

    // register slider(trackbar) from comctl32.dll brfore creating windows
    INITCOMMONCONTROLSEX commonCtrls;
    commonCtrls.dwSize = sizeof(commonCtrls);
    commonCtrls.dwICC = ICC_BAR_CLASSES;        // trackbar is in this class
    ::InitCommonControlsEx(&commonCtrls);

    // get app name from resource file
    wchar_t name[256];
    ::LoadString(hInst, IDS_APP_NAME, name, 256);

    Win::ControllerMain mainCtrl;
    Win::Window mainWin(hInst, name, 0, &mainCtrl);

    // add menu to window class
    mainWin.setMenuName(MAKEINTRESOURCE(IDR_MAIN_MENU));
    mainWin.setWidth(300);
    mainWin.setHeight(460);
    mainWin.setWindowStyleEx(WS_EX_WINDOWEDGE);

    // create a window and show
    if(mainWin.create())
        Win::log("Main window is created.");
    else
        Win::log("[ERROR] Failed to create main window.");

    // create model and view components for controller
    ModelGL modelGL;
    Win::ViewGL viewGL;
    Win::ViewFormGL viewFormGL;

    // create OpenGL rendering window as a child
    Win::ControllerGL glCtrl(&modelGL, &viewGL);
    Win::Window glWin(hInst, L"WindowGL", mainWin.getHandle(), &glCtrl);
    glWin.setClassStyle(CS_OWNDC);
    glWin.setWindowStyle(WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
    glWin.setWidth(300);
    glWin.setHeight(300);
    if(glWin.create())
        Win::log("OpenGL window is created.");
    else
        Win::log("[ERROR] Failed to create OpenGL window.");

    // create a child dialog box contains controls
    Win::ControllerFormGL formCtrl(&modelGL, &viewFormGL);
    Win::DialogWindow glDialog(hInst, IDD_CONTROLS, mainWin.getHandle(), &formCtrl);
    glDialog.setWidth(300);
    glDialog.setHeight(160);
    if(glDialog.create())
        Win::log("OpenGL form dialog is created.");
    else
        Win::log("[ERROR] Failed to create OpenGL form dialog.");

    // create status bar window using CreateWindowEx()
    // mainWin must pass WM_SIZE message to the status bar
    // So, mainWin accesses the status bar with GetDlgItem(handle, IDC_STATUSBAR).
    HWND statusHandle = ::CreateWindowEx(0, STATUSCLASSNAME, 0, WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
                                         0, 0, 0, 0, mainWin.getHandle(), (HMENU)IDC_STATUSBAR, ::GetModuleHandle(0), 0);
    if(statusHandle)
        Win::log("Status bar window is created.");
    else
        Win::log("[ERROR] Failed to create status bar window.");
    ::SendMessage(statusHandle, SB_SETTEXT, 0, (LPARAM)L"Ready");

    // send window handles to mainCtrl, they are used for resizing window
    mainCtrl.setGLHandle(glWin.getHandle());
    mainCtrl.setFormHandle(glDialog.getHandle());

    // place the opengl form dialog in right place, bottome of the opengl rendering window
    ::SetWindowPos(glDialog.getHandle(), 0, 0, 300, 300, 160, SWP_NOZORDER);

    // compute height of all sub-windows
    int height = 0;
    RECT rect;
    ::GetWindowRect(glWin.getHandle(), &rect);      // get size of glWin
    height += rect.bottom - rect.top;
    ::GetWindowRect(glDialog.getHandle(), &rect);   // get size of glDialog
    height += rect.bottom - rect.top;
    ::GetWindowRect(statusHandle, &rect);           // get size of status bar
    height += rect.bottom - rect.top;

    // resize main window, so all sub windows are fit into the client area of main window
    DWORD style = (DWORD)::GetWindowLongPtr(mainWin.getHandle(), GWL_STYLE);       // get current window style
    DWORD styleEx = (DWORD)::GetWindowLongPtr(mainWin.getHandle(), GWL_EXSTYLE);   // get current extended window style
    rect.left = 0;
    rect.right = 300;
    rect.top = 0;
    rect.bottom = height;
    ::AdjustWindowRectEx(&rect, style, TRUE, styleEx);
    ::SetWindowPos(mainWin.getHandle(), 0, 100, 100, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);

    // show all windows
    glWin.show();
    glDialog.show();
    mainWin.show();
    //::SendMessage(mainWin.getHandle(), WM_NCPAINT, 1, 0);   // repaint window frame

    // main message loop //////////////////////////////////////////////////////
    int exitCode;
    HACCEL hAccelTable = 0;
    //hAccelTable = ::LoadAccelerators(hInst, MAKEINTRESOURCE(ID_ACCEL));
    exitCode = mainMessageLoop(hAccelTable);

    Win::log("Application terminated.");
    return exitCode;
}