home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_01 / Hello3 / Hello3.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-17  |  2.9 KB  |  87 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : hello3.cpp                                                             //
  10. //  Description: Hello World Demo 3, C++ window, Chapter 1                           //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include <tchar.h>
  20.  
  21. #include "..\..\include\win.h"
  22.  
  23. const TCHAR szMessage[] = _T("Hello, World !");
  24. const TCHAR szFace[]    = _T("Times New Roman");
  25. const TCHAR szHint[]    = _T("Press ESC to quit.");
  26. const TCHAR szProgram[] = _T("HelloWorld3");
  27.  
  28. void CenterText(HDC hDC, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point)
  29. {
  30.     HFONT hFont = CreateFont(
  31.         - point * GetDeviceCaps(hDC, LOGPIXELSY) / 72,
  32.         0, 0, 0, FW_BOLD, TRUE, FALSE, FALSE, 
  33.         ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, 
  34.         PROOF_QUALITY, VARIABLE_PITCH, szFace);
  35.     assert(hFont);
  36.  
  37.     HGDIOBJ hOld = SelectObject(hDC, hFont);
  38.  
  39.     SetTextAlign(hDC, TA_CENTER | TA_BASELINE);
  40.  
  41.     SetBkMode(hDC, TRANSPARENT);
  42.     SetTextColor(hDC, RGB(0, 0, 0xFF));
  43.     TextOut(hDC, x, y, szMessage, _tcslen(szMessage));
  44.  
  45.     SelectObject(hDC, hOld);
  46.     DeleteObject(hFont);
  47. }
  48.  
  49.  
  50. class KHelloWindow : public KWindow
  51. {
  52.     void OnKeyDown(WPARAM wParam, LPARAM lParam)
  53.     {
  54.         if ( wParam==VK_ESCAPE )
  55.             PostMessage(m_hWnd, WM_CLOSE, 0, 0);
  56.     }
  57.  
  58.     void OnDraw(HDC hDC)
  59.     {
  60.         TextOut(hDC, 0, 0, szHint, lstrlen(szHint));
  61.         CenterText(hDC, GetDeviceCaps(hDC, HORZRES)/2,
  62.             GetDeviceCaps(hDC, VERTRES)/2, szFace, szMessage, 72);
  63.     }
  64.  
  65. public:
  66.     
  67. };
  68.  
  69.  
  70. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, 
  71.                    LPSTR lpCmd, int nShow)
  72. {
  73.     KHelloWindow win;
  74.     
  75.     win.CreateEx(0, szProgram, szProgram,
  76.                  WS_POPUP,
  77.                  0, 0,
  78.                  GetSystemMetrics( SM_CXSCREEN ),
  79.                  GetSystemMetrics( SM_CYSCREEN ),
  80.                  NULL, NULL, hInst);
  81.         
  82.     win.ShowWindow(nShow);
  83.  
  84.     win.UpdateWindow();
  85.   
  86.     return win.MessageLoop();
  87. }