home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n07 / gdiobj.exe / GDIOBJ1.CPP next >
C/C++ Source or Header  |  1995-07-01  |  5KB  |  174 lines

  1. //***************************************************************************
  2. //
  3. //  GdiObj1 demonstrates basic pen and brush styles.
  4. //
  5. //***************************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "gdiobj1.h"
  9.  
  10. CMyApp myApp;
  11.  
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CMyApp member functions
  14.  
  15. BOOL CMyApp::InitInstance ()
  16. {
  17.     m_pMainWnd = new CMainWindow ();
  18.     m_pMainWnd->ShowWindow (m_nCmdShow);
  19.     m_pMainWnd->UpdateWindow ();
  20.     return TRUE;
  21. }
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CMainWindow message map and member functions
  25.  
  26. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  27.     ON_WM_CREATE ()
  28.     ON_WM_PAINT ()
  29. END_MESSAGE_MAP ()
  30.  
  31. CMainWindow::CMainWindow ()
  32. {
  33.     Create (NULL, "GdiObj1");
  34. }
  35.  
  36. int CMainWindow::OnCreate (LPCREATESTRUCT lpCreateStruct)
  37. {
  38.     TEXTMETRIC tm;
  39.  
  40.     CDC* pDC = GetDC ();
  41.     pDC->GetTextMetrics (&tm);
  42.     m_cxChar = tm.tmAveCharWidth;
  43.     m_cyChar = tm.tmHeight;
  44.     ReleaseDC (pDC);
  45.     return 0;
  46. }
  47.  
  48. void CMainWindow::OnPaint ()
  49. {
  50.     CPaintDC dc (this);
  51.                 
  52.     ShowPenStyles (&dc, m_cxChar << 1, m_cyChar);   
  53.     ShowPenWidths (&dc, m_cxChar << 1, m_cyChar * 15);
  54.     ShowBrushStyles (&dc, m_cxChar << 1, m_cyChar * 27);
  55. }
  56.  
  57. void CMainWindow::ShowPenStyles (CDC* pDC, int x, int y)
  58. {
  59.     CPen* pPen;
  60.     CPen* pOldPen;
  61.  
  62.     struct STYLES styles[] = {
  63.         PS_SOLID,       "PS_SOLID",
  64.         PS_DASH,        "PS_DASH",
  65.         PS_DOT,         "PS_DOT",
  66.         PS_DASHDOT,     "PS_DASHDOT",
  67.         PS_DASHDOTDOT,  "PS_DASHDOTDOT",
  68.         PS_NULL,        "PS_NULL",
  69.         PS_INSIDEFRAME, "PS_INSIDEFRAME"
  70.     };
  71.  
  72.     CString string = "Pen Styles";
  73.     pDC->TextOut (x, y, string);
  74.  
  75.     int dy = (m_cyChar * 3) >> 1;
  76.     pDC->SetTextColor (RGB (255, 0, 0));
  77.  
  78.     for (int i=0; i<7; i++) {
  79.         y += dy;
  80.         string = styles[i].szStyleName;
  81.         pDC->TextOut (x + (m_cxChar << 1), y, string);
  82.  
  83.         pPen = new CPen (styles[i].nStyle, 1, RGB (255, 0, 0));
  84.         pOldPen = pDC->SelectObject (pPen);
  85.  
  86.         pDC->MoveTo (x + (m_cxChar * 22), y + (m_cyChar >> 1));
  87.         pDC->LineTo (x + (m_cxChar * 46), y + (m_cyChar >> 1));
  88.  
  89.         delete pDC->SelectObject (pOldPen);
  90.     }
  91. }
  92.  
  93. void CMainWindow::ShowPenWidths (CDC* pDC, int x, int y)
  94. {
  95.     CPen* pPen;
  96.     CPen* pOldPen;
  97.  
  98.     int nPenWidths[] = { 2, 8, 16, 24 };
  99.  
  100.     CString string = "Pen Widths";
  101.     pDC->SetTextColor (RGB (0, 0, 0));
  102.     pDC->TextOut (x, y, string);
  103.  
  104.     int dy = m_cyChar << 1;
  105.     pDC->SetTextColor (RGB (0, 0, 255));
  106.  
  107.     for (int i=0; i<4; i++) {
  108.         y += dy;
  109.         string.Format ("%d Pixels", nPenWidths[i]);
  110.         pDC->TextOut (x + (m_cxChar << 1), y, string);
  111.  
  112.         pPen = new CPen (PS_SOLID, nPenWidths[i], RGB (0, 0, 255));
  113.         pOldPen = pDC->SelectObject (pPen);
  114.  
  115.         pDC->MoveTo (x + (m_cxChar * 22), y + (m_cyChar >> 1));
  116.         pDC->LineTo (x + (m_cxChar * 46), y + (m_cyChar >> 1));
  117.  
  118.         delete pDC->SelectObject (pOldPen);
  119.     }
  120. }
  121.  
  122. void CMainWindow::ShowBrushStyles (CDC* pDC, int x, int y)
  123. {
  124.     CBrush* pBrush;
  125.     CBrush* pOldBrush;
  126.  
  127.     struct STYLES styles[] = {
  128.         HS_BDIAGONAL,   "HS_BDIAGONAL",
  129.         HS_CROSS,       "HS_CROSS",
  130.         HS_DIAGCROSS,   "HS_DIAGCROSS",
  131.         HS_FDIAGONAL,   "HS_FDIAGONAL",
  132.         HS_HORIZONTAL,  "HS_HORIZONTAL",
  133.         HS_VERTICAL,    "HS_VERTICAL"
  134.     };
  135.  
  136.     CString string = "Brush Styles";
  137.     pDC->SetTextColor (RGB (0, 0, 0));
  138.     pDC->TextOut (x, y, string);
  139.  
  140.     int dy = m_cyChar * 3;
  141.  
  142.     for (int i=0; i<6; i++) {
  143.         y += dy;
  144.         string = styles[i].szStyleName;
  145.         pDC->TextOut (x + (m_cxChar << 1), y, string);
  146.  
  147.         CRect rect (x + (m_cxChar * 22), y - m_cyChar,
  148.             x + (m_cxChar * 46), y + m_cyChar);
  149.  
  150.         pBrush = new CBrush (styles[i].nStyle, RGB (0, 255, 0));
  151.         CPoint point (rect.left, rect.top);
  152.         pDC->LPtoDP (&point);
  153.         point.x &= 0x07;
  154.         point.y &= 0x07;
  155.         pDC->SetBrushOrg (point);
  156.  
  157.         pOldBrush = pDC->SelectObject (pBrush);
  158.         pDC->Rectangle (rect);
  159.         delete pDC->SelectObject (pOldBrush);
  160.     }
  161.  
  162.     y += dy;
  163.     string = "Solid";
  164.     pDC->TextOut (x + (m_cxChar << 1), y, string);
  165.  
  166.     pBrush = new CBrush (RGB (0, 255, 0));
  167.     pOldBrush = pDC->SelectObject (pBrush);
  168.  
  169.     pDC->Rectangle (x + (m_cxChar * 22), y - m_cyChar,
  170.         x + (m_cxChar * 46), y + m_cyChar);
  171.  
  172.     delete pDC->SelectObject (pOldBrush);
  173. }
  174.