#include #include #include #include "textPlay.h" using namespace std; const char className[] = "textPlay"; const char APP_TITLE[] = "Playing with text"; const unsigned short int APP_WIDTH = 500; const unsigned short int APP_HEIGHT = 400; static int fontHeight; LRESULT CALLBACK mainEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM params); LRESULT CALLBACK mainEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM params) { if (event == WM_CREATE) { TEXTMETRIC tm; HDC hdc = GetDC(hwnd); GetTextMetrics(hdc, &tm);//Gets information about the font being used fontHeight = tm.tmHeight; ReleaseDC(hwnd, hdc); } else if (event == WM_PAINT) { PAINTSTRUCT ps; RECT clientArea; HDC hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &clientArea); //Gets the size of the client area char buffer[30]; //Makes a message with the dimensions of the client area embedded sprintf(buffer, "width = %03u, height = %03u", clientArea.right, clientArea.bottom); int fromTop = 20; string strHello = "Hello World!"; string strBye = "Goodbye World!"; //TextOut, the 4th param expects char*, and the 5th param is the number of characters to display //c_str() outputs the string as a char*, and length() returns the number of characters in the string TextOut(hdc, 20, fromTop, strHello.c_str(), strHello.length()); fromTop += fontHeight; //Gets the height of the tallest character in the font, so the next line will start beneath it TextOut(hdc, 20, fromTop, strBye.c_str(), strBye.length()); //Outputs the client area dimension information to the centre of the client area DrawText(hdc, buffer, -1, &clientArea, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hwnd, &ps); } else if (event == WM_CLOSE) { DestroyWindow(hwnd); } else if (event == WM_DESTROY) { PostQuitMessage(0); } else { return DefWindowProc(hwnd, event, object, params); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = mainEventListener; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(imgMainIcon)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); wc.lpszMenuName = NULL; wc.lpszClassName = className; wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(imgMainIcon), IMAGE_ICON, 16, 16, 0); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_OK | MB_ICONEXCLAMATION); return 0; } hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, className, APP_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, APP_WIDTH, APP_HEIGHT, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_OK | MB_ICONEXCLAMATION); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }