// OFWIN.CPP Native windows program to display text in a window #include long WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); void OnPaint( HWND ); void OnLButtonDown( void ); void OnRButtonDown( void ); void OnChar( void ); // Listing OFWIN_1 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS WindowClass; // Structure to hold our window's attributes static char szAppName[] = "OFWin"; // Define window class name HWND hWnd; // Window handle MSG msg; // Windows message structure // Redraws the entire window if a movement or size // adjustment changes the height/width of the client area. WindowClass.style = CS_HREDRAW | CS_VREDRAW; // *** Define our procedure for message handling *** WindowClass.lpfnWndProc = WindowProc; WindowClass.cbClsExtra = 0; // No extra bytes after the window class WindowClass.cbWndExtra = 0; // structure or the window instance WindowClass.hInstance = hInstance; // Application instance handle // Set default application icon WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION ); // Set window cursor to be the standard arrow WindowClass.hCursor = LoadCursor(0, IDC_ARROW); // Set gray brush for background color WindowClass.hbrBackground = static_cast(GetStockObject(GRAY_BRUSH)); WindowClass.lpszMenuName = 0; // No menu, so no menu resource name WindowClass.lpszClassName = szAppName; // Set class name // The RegisterClass function registers a window class // for subsequent use in calls to the CreateWindow or // CreateWindowEx function RegisterClass(&WindowClass); // Now we can create the window hWnd = CreateWindow( szAppName, // the window class name "A Basic Window the Hard Way", // The window title WS_OVERLAPPEDWINDOW, // Window style as overlapped 0, // CW_USEDEFAULT, // Default screen position of upper left 0, //CW_USEDEFAULT, // corner of our window as x,y... 700, //CW_USEDEFAULT, // Default window size 400, //CW_USEDEFAULT, // .... 0, // No parent window 0, // No menu hInstance, // Program Instance handle 0 // No window creation data ); ShowWindow( hWnd, nCmdShow ); // Display the window // Cause window client area to be drawn // by posting a WM_PAINT message to the message queue UpdateWindow( hWnd ); // The GetMessage function retrieves a message // from the calling thread's message queue and places // it in the specified structure. // Exits loop on WM_CLOSE event! while( GetMessage(&msg, 0, 0, 0) == TRUE) // Get any messages { // Translates virtual-key messages into character // messages. The character messages are posted to the // calling thread's message queue, to be read the next // time the thread calls the GetMessage or // PeekMessage function. TranslateMessage(&msg); // The DispatchMessage function dispatches a message // to a window procedure. It is typically used to // dispatch a message retrieved by the GetMessage function DispatchMessage(&msg); } return msg.wParam; // End so return to Windows } // This is your function that handles messages that windows // routes to this application. Only write code for the messages // that you want to handle. long WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { // Display context handle switch( message ) // Process selected messages { case WM_PAINT: // Message is to redraw the window OnPaint( hWnd ); return 0; case WM_DESTROY: // Window is being destroyed // The PostQuitMessage function posts a // WM_QUIT message to the thread's message // queue and returns immediately; the function // simply indicates to the system that the // thread is requesting to quit at some time in the future. PostQuitMessage(0); return 0; case WM_LBUTTONDOWN: OnLButtonDown(); return 0; case WM_RBUTTONDOWN: OnRButtonDown(); return 0; case WM_CHAR: OnChar(); return 0; default: // Any other message - we don't want to know, // so call default message processing return DefWindowProc(hWnd, message, wParam, lParam); } } void OnPaint( HWND hWnd ) { RECT aRect; PAINTSTRUCT PaintSt; // Structure defining area to be drawn HDC hDC; hDC = BeginPaint( hWnd, &PaintSt ); // Prepare to draw the window // Get upper left and lower right of client area GetClientRect( hWnd, &aRect ); SetBkMode(hDC, TRANSPARENT); // Set text background mode // Now draw the text in the window client area DrawText( hDC, // Device context handle "But, soft! What light through yonder window breaks?", -1, // Indicate null terminated string &aRect, // Rectangle in which text is to be drawn DT_SINGLELINE| // Text format - single line DT_CENTER| // - centered in the line DT_VCENTER ); // - line centered in aRect EndPaint(hWnd, &PaintSt); // Terminate window redraw operation } void OnLButtonDown( void ) { MessageBox( NULL, "You just pressed the left mouse button", "OnLButtonDown()", MB_OK ); } void OnRButtonDown( void ) { for( int i=0; i<5; i++) { MessageBeep( (DWORD)-1 ); Sleep( 100 ); } } void OnChar( void ) { MessageBox( NULL, "You just pressed a Key!", "OnChar()", MB_OK ); }