home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / saver / saverdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  7.1 KB  |  290 lines

  1. // Saverdlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Saver.h"
  6. #include "drawwnd.h"
  7. #include "Saverdlg.h"
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char BASED_CODE THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16.  
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20.     CAboutDlg();
  21.  
  22. // Dialog Data
  23.     //{{AFX_DATA(CAboutDlg)
  24.     enum { IDD = IDD_ABOUTBOX };
  25.     //}}AFX_DATA
  26.  
  27. // Implementation
  28. protected:
  29.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  30.     //{{AFX_MSG(CAboutDlg)
  31.     virtual BOOL OnInitDialog();
  32.     //}}AFX_MSG
  33.     DECLARE_MESSAGE_MAP()
  34. };
  35.  
  36. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  37. {
  38.     //{{AFX_DATA_INIT(CAboutDlg)
  39.     //}}AFX_DATA_INIT
  40. }
  41.  
  42. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44.     CDialog::DoDataExchange(pDX);
  45.     //{{AFX_DATA_MAP(CAboutDlg)
  46.     //}}AFX_DATA_MAP
  47. }
  48.  
  49. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  50.     //{{AFX_MSG_MAP(CAboutDlg)
  51.         // No message handlers
  52.     //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CAboutDlg message handlers
  57.  
  58. BOOL CAboutDlg::OnInitDialog()
  59. {
  60.     CDialog::OnInitDialog();
  61.     CenterWindow();
  62.  
  63.     // TODO: Add extra about dlg initialization here
  64.  
  65.     return TRUE;  // return TRUE  unless you set the focus to a control
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CSaverDlg dialog
  70.  
  71. CSaverDlg::CSaverDlg(CWnd* pParent /*=NULL*/)
  72.     : CDialog(CSaverDlg::IDD, pParent), m_wndPreview(FALSE)
  73. {
  74.     //{{AFX_DATA_INIT(CSaverDlg)
  75.     m_nWidth = 0;
  76.     m_nCapStyle = -1;
  77.     m_nResolution = 0;
  78.     m_nSpeed = 0;
  79.     m_nJoinStyle = -1;
  80.     //}}AFX_DATA_INIT
  81.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  82.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  83. }
  84.  
  85. void CSaverDlg::DoDataExchange(CDataExchange* pDX)
  86. {
  87.     CDialog::DoDataExchange(pDX);
  88.     //{{AFX_DATA_MAP(CSaverDlg)
  89.     DDX_Control(pDX, IDC_JOIN_STYLE, m_comboJoin);
  90.     DDX_Control(pDX, IDC_CAP_STYLE, m_comboStyle);
  91.     DDX_Control(pDX, IDC_SCROLLBAR_SPEED, m_scrollSpeed);
  92.     DDX_Control(pDX, IDC_SCROLLBAR_RES, m_scrollRes);
  93.     DDX_Text(pDX, IDC_EDIT_WIDTH, m_nWidth);
  94.     DDV_MinMaxInt(pDX, m_nWidth, 1, 100);
  95.     DDX_CBIndex(pDX, IDC_CAP_STYLE, m_nCapStyle);
  96.     DDX_Scroll(pDX, IDC_SCROLLBAR_RES, m_nResolution);
  97.     DDX_Scroll(pDX, IDC_SCROLLBAR_SPEED, m_nSpeed);
  98.     DDX_CBIndex(pDX, IDC_JOIN_STYLE, m_nJoinStyle);
  99.     //}}AFX_DATA_MAP
  100. }
  101.  
  102. BEGIN_MESSAGE_MAP(CSaverDlg, CDialog)
  103.     //{{AFX_MSG_MAP(CSaverDlg)
  104.     ON_WM_SYSCOMMAND()
  105.     ON_WM_PAINT()
  106.     ON_WM_QUERYDRAGICON()
  107.     ON_WM_HSCROLL()
  108.     ON_BN_CLICKED(IDC_BUTTON_COLOR, OnButtonColor)
  109.     ON_EN_KILLFOCUS(IDC_EDIT_WIDTH, OnKillfocusEditWidth)
  110.     ON_CBN_SELENDOK(IDC_JOIN_STYLE, OnSelendokStyle)
  111.     ON_CBN_SELENDOK(IDC_CAP_STYLE, OnSelendokStyle)
  112.     //}}AFX_MSG_MAP
  113. END_MESSAGE_MAP()
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CSaverDlg message handlers
  117.  
  118. BOOL CSaverDlg::OnInitDialog()
  119. {
  120.     CDialog::OnInitDialog();
  121.  
  122.     CRect rect;
  123.     GetDlgItem(IDC_PREVIEW)->GetWindowRect(&rect);
  124.     ScreenToClient(&rect);
  125.     m_wndPreview.Create(NULL, WS_VISIBLE|WS_CHILD, rect, this, NULL);
  126.     m_wndPreview.SetColor(m_color);
  127.     m_wndPreview.SetResolution(m_nResolution);
  128.     m_wndPreview.SetSpeed(m_nSpeed);
  129.     OnSelendokStyle();
  130.     m_wndPreview.SetPenWidth(m_nWidth);
  131.  
  132.     m_scrollRes.SetScrollRange(1, 30);
  133.     m_scrollSpeed.SetScrollRange(1, 100);
  134.     m_scrollRes.SetScrollPos(m_nResolution);
  135.     m_scrollSpeed.SetScrollPos(m_nSpeed);
  136.  
  137.  
  138.     CenterWindow();
  139.  
  140.     // Add "About..." menu item to system menu.
  141.  
  142.     // IDM_ABOUTBOX must be in the system command range.
  143.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  144.     ASSERT(IDM_ABOUTBOX < 0xF000);
  145.  
  146.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  147.     CString strAboutMenu;
  148.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  149.     if (!strAboutMenu.IsEmpty())
  150.     {
  151.         pSysMenu->AppendMenu(MF_SEPARATOR);
  152.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  153.     }
  154.  
  155.     // TODO: Add extra initialization here
  156.  
  157.     return TRUE;  // return TRUE  unless you set the focus to a control
  158. }
  159.  
  160. void CSaverDlg::OnSysCommand(UINT nID, LPARAM lParam)
  161. {
  162.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  163.     {
  164.         CAboutDlg dlgAbout;
  165.         dlgAbout.DoModal();
  166.     }
  167.     else
  168.     {
  169.         CDialog::OnSysCommand(nID, lParam);
  170.     }
  171. }
  172.  
  173. // If you add a minimize button to your dialog, you will need the code below
  174. //  to draw the icon.  For MFC applications using the document/view model,
  175. //  this is automatically done for you by the framework.
  176.  
  177. void CSaverDlg::OnPaint()
  178. {
  179.     if (IsIconic())
  180.     {
  181.         CPaintDC dc(this); // device context for painting
  182.  
  183.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  184.  
  185.         // Center icon in client rectangle
  186.         int cxIcon = GetSystemMetrics(SM_CXICON);
  187.         int cyIcon = GetSystemMetrics(SM_CYICON);
  188.         CRect rect;
  189.         GetClientRect(&rect);
  190.         int x = (rect.Width() - cxIcon + 1) / 2;
  191.         int y = (rect.Height() - cyIcon + 1) / 2;
  192.  
  193.         // Draw the icon
  194.         dc.DrawIcon(x, y, m_hIcon);
  195.     }
  196.     else
  197.     {
  198.         CDialog::OnPaint();
  199.     }
  200. }
  201.  
  202. // The system calls this to obtain the cursor to display while the user drags
  203. //  the minimized window.
  204. HCURSOR CSaverDlg::OnQueryDragIcon()
  205. {
  206.     return (HCURSOR) m_hIcon;
  207. }
  208.  
  209. void CSaverDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  210. {
  211.     ASSERT(pScrollBar != NULL);
  212.     int nCurPos = pScrollBar->GetScrollPos();
  213.     switch (nSBCode)
  214.     {
  215.     case SB_LEFT: //    Scroll to far left.
  216.         nCurPos = 1;
  217.         break;
  218.     case SB_LINELEFT: //    Scroll left.
  219.         nCurPos--;
  220.         break;
  221.     case SB_LINERIGHT: //    Scroll right.
  222.         nCurPos++;
  223.         break;
  224.     case SB_PAGELEFT: //    Scroll one page left.
  225.         nCurPos -= 10;
  226.         break;
  227.     case SB_PAGERIGHT: //    Scroll one page right.
  228.         nCurPos += 10;
  229.         break;
  230.     case SB_RIGHT: //    Scroll to far right.
  231.         nCurPos = 100;
  232.         break;
  233.     case SB_THUMBPOSITION: //    Scroll to absolute position. The current position is specified by the nPos parameter.
  234.     case SB_THUMBTRACK: //    Drag scroll box to specified position. The current position is specified by the nPos parameter.
  235.         nCurPos = nPos;
  236.     }
  237.     if (nCurPos < 1)
  238.         nCurPos = 1;
  239.     if (nCurPos > 100)
  240.         nCurPos = 100;
  241.     pScrollBar->SetScrollPos(nCurPos);
  242.  
  243.     m_wndPreview.SetSpeed(m_scrollSpeed.GetScrollPos());
  244.     m_wndPreview.SetResolution(m_scrollRes.GetScrollPos());
  245.  
  246.     CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
  247. }
  248.  
  249. void CSaverDlg::OnButtonColor()
  250. {
  251.     CColorDialog dlg(m_color);
  252.     if (dlg.DoModal() == IDOK)
  253.     {
  254.         m_color = dlg.GetColor();
  255.         m_wndPreview.SetColor(m_color);
  256.     }
  257. }
  258.  
  259. void CSaverDlg::OnKillfocusEditWidth()
  260. {
  261.     int nWidth = GetDlgItemInt(IDC_EDIT_WIDTH);
  262.     if (nWidth < 1)
  263.         nWidth = 1;
  264.     m_wndPreview.SetPenWidth(nWidth);
  265. }
  266.  
  267. void CSaverDlg::OnSelendokStyle()
  268. {
  269.     int nStyle = 0;
  270.     int nSel = m_comboStyle.GetCurSel();
  271.     if (nSel < 0)
  272.         nSel = 0;
  273.     if (nSel == 0)
  274.         nStyle = PS_ENDCAP_ROUND;
  275.     else if (nSel == 1)
  276.         nStyle = PS_ENDCAP_SQUARE;
  277.     else
  278.         nStyle = PS_ENDCAP_FLAT;
  279.     nSel = m_comboJoin.GetCurSel();
  280.     if (nSel < 0)
  281.         nSel = 0;
  282.     if (nSel == 0)
  283.         nStyle |= PS_JOIN_ROUND;
  284.     else if (nSel == 1)
  285.         nStyle |= PS_JOIN_BEVEL;
  286.     else
  287.         nStyle |= PS_JOIN_MITER;
  288.     m_wndPreview.SetLineStyle(nStyle);
  289. }
  290.