///////////////////////////////////////////////////////////////// // // // frame.cpp - small windows demo that creates a console // // window from a Win32 Application // // // // Jim Fawcett, CSE691 - SW Modeling & Analysis, 20 Sep 2000 // // Ref: Simply added a console to minwin.cpp. Made a few // // other cosmetic modifications. // ///////////////////////////////////////////////////////////////// #include #include HWND hWnd; // handle to window created in InitInstance HANDLE hInst; // handle to current module HANDLE stdin; HANDLE stdout; //----< 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 DWORD word; const char *msg1 = "CSE681 - SW Modeling & Analysis"; const char *msg2 = "Hello World"; const char *msg = ""; static int mcount = 0; static int pcount = 0; static char text[100]; switch(message) { case WM_PAINT: hDC = BeginPaint(hWnd,&ps); msg = "\n paint message"; GetClientRect(hWnd,&rect); TextOut(hDC,30,30,msg1,strlen(msg1)); MoveToEx(hDC,25,50,NULL); LineTo(hDC,260,50); EndPaint(hWnd, &ps); wsprintf(text,"\n Paint Count: %d",++pcount); msg = text; break; case WM_LBUTTONDOWN: msg = "\n left button message"; break; case WM_RBUTTONDOWN: msg = "\n right button message"; break; case WM_MOUSEMOVE: wsprintf(text,"\n Mouse Count: %d",++mcount); msg = text; break; case WM_SIZE: msg = "\n window size message"; GetClientRect(hWnd,&rect); break; case WM_DESTROY: msg = "\n destroy message"; PostQuitMessage(0); break; default : return(DefWindowProc(hWnd,message, wParam, lParam)); } WriteFile(stdout,msg,strlen(msg),&word,0); 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" to use when creating "CSE681 - Main Window", // window title WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // default horizontal position CW_USEDEFAULT, // default vertical position 400, // 200 pixels wide 300, // 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 WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; if(!InitApplication(hInstance)) return (FALSE); // can't initialize window if(!InitInstance(hInstance, nCmdShow)) return (FALSE); if(!AllocConsole()) return (FALSE); stdin = GetStdHandle(STD_INPUT_HANDLE); stdout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTitle("Console window created in Win32 Application"); while(GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); }