示例#1
0
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow) {
    HWND activeWindow;
    MSG msg;
	HACCEL hAccelTable = 0;
	wchar_t appName[256];
	HWND statusHandle;

	INITCOMMONCONTROLSEX commonCtrlEx;
	commonCtrlEx.dwSize = sizeof(commonCtrlEx);
	commonCtrlEx.dwICC = ICC_BAR_CLASSES;	//trackbar in this class
	::InitCommonControlsEx(&commonCtrlEx);

	//get App name from resource file
	::LoadString(hInstance, IDS_APP_NAME, appName, 256);

	Win::ControllerMain ctrlMain;
	Win::Window windowMain(hInstance, appName, 0, &ctrlMain);

	windowMain.setMenuName(MAKEINTRESOURCE(IDR_MAIN_MENU));
	windowMain.setSize(800, 650);
	windowMain.setWindowStyleEx(WS_EX_WINDOWEDGE);
	windowMain.create();

	ModelGL modelGL;

	//Register GL window class
	Win::ViewGL viewGL(hInstance, windowMain.getHandle(), L"MyGLWindow", 800, 500, CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
		WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 3, 1);
	Win::GLController controllerGL(&modelGL, &viewGL);
	Win::ConsoleView consoleView;

	//Create GL window and init OpenGL
	controllerGL.init();

	Win::ConsolePaneController consolePaneCtrl(&modelGL, &consoleView);

	Win::DialogWindow dlgConsole(hInstance, IDD_CONSOLE, windowMain.getHandle(), &consolePaneCtrl);
	dlgConsole.create();

	statusHandle = ::CreateWindowEx(0, STATUSCLASSNAME, 0, WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
		0, 0, 0, 0, windowMain.getHandle(), (HMENU)IDC_STATUSBAR, ::GetModuleHandle(0), 0);

	if(statusHandle) {
        ::SendMessage(statusHandle, SB_SETTEXT, 0, (LPARAM)L"Ready");
	}

	ctrlMain.setGLHandle(viewGL.getHandle());
	ctrlMain.setConsoleHandle(dlgConsole.getHandle());

	viewGL.show();
	dlgConsole.show();
	windowMain.show();

    while(::GetMessage(&msg, NULL, 0, 0) > 0) {
		activeWindow = ::GetActiveWindow();

		if(!::TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
    }

    return (int)msg.wParam;
}
示例#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;
}