/////////////////////////////////////////////////////////////////// // // // console.cpp - small console demo program that // // creates a graphical window // // // // Jim Fawcett, CSE691 - SW Modeling & Analysis, 20 Sep 2000 // // Ref: Program created by changing WinMain in minwin.cpp to // // main. A few other minor modifications were also made. // /////////////////////////////////////////////////////////////////// #include #include #include using namespace std; HWND hWnd; // handle to window created in InitInstance HANDLE hInst; // handle to current module //----< process windows messages >----------------------------- LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hDC; // device context RECT rect; // will hold dimensions of client area PAINTSTRUCT ps; // used by Windows to hold painting information const char *msg1 = "CSE691 - SW Modeling & Analysis"; const char *msg2 = "Hello World"; switch(message) { case WM_PAINT : hDC = BeginPaint(hWnd,&ps); GetClientRect(hWnd,&rect); DrawText( hDC,msg2,strlen(msg2),&rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER ); TextOut(hDC,30,30,msg1,strlen(msg1)); MoveToEx(hDC,25,50,NULL); LineTo(hDC,260,50); EndPaint(hWnd, &ps); break; case WM_LBUTTONDOWN: MessageBox( hWnd,"left mouse button clicked","Response to WM_LBUTTON",MB_OK ); break; case WM_DESTROY: PostQuitMessage(0); break; default : return(DefWindowProc(hWnd,message, wParam, lParam)); } return (NULL); } // //----< define and register window "class" >------------------- BOOL InitApplication(HINSTANCE hInstance) { // define window data in WNDCLASS struct and register WNDCLASS wc; wc.style = NULL; // default style wc.lpfnWndProc = WinProc; // name of function handling messages wc.cbClsExtra = 0; // no per-class user-defined data wc.cbWndExtra = 0; // no per-window user-defined data wc.hInstance = hInstance; // application attached to this class wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; // menu name wc.lpszClassName = "BasicWindow"; // name of this "class" return (RegisterClass(&wc)); } //----< create and display window >---------------------------- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; hWnd = CreateWindow( "BasicWindow", // "class" "CSE691 - Created by Console App", // window title WS_OVERLAPPEDWINDOW, // window style 400, // horizontal position 400, // vertical position 300, // 200 pixels wide 200, // 150 pixels high NULL, // no parent window NULL, // use window class menu hInstance, // owner of window NULL); if(!hWnd) return FALSE; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return (TRUE); } // //----< collect and dispatch windows messages >---------------- int main(int argc, char *argv[]) { HINSTANCE hInstance = GetModuleHandle("console.exe"); LPTSTR pCmdLine = GetCommandLine(); SetConsoleTitle("Main Window"); cout << "\n Console Application that creates a PopUp Graphical Window " << "\n ===========================================================\n\n"; MSG msg; if(!InitApplication(hInstance)) // return (FALSE); // can't initialize window if(!InitInstance(hInstance, SW_SHOW)) return (FALSE); while(GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } cout << "\n popup window has terminated\n\n"; return (msg.wParam); }