⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 viewwin.h

📁 Visual_C++[1].NET_Bible1 Visual_C++宝典书中的全部源码
💻 H
字号:
#pragma once
#include "stdafx.h"

class CViewWin :
  public CWindowImpl<CViewWin, CWindow, 
   CWinTraits<WS_CHILD | WS_VISIBLE, WS_EX_CLIENTEDGE> >
{
public:
  CViewWin(void);
  ~CViewWin(void);

protected:
  POINT m_startPoint;
  POINT m_endPoint;

public:
  COLORREF m_color;

public:
	DECLARE_WND_CLASS(_T("MyView"))

	BEGIN_MSG_MAP(CMainFrame)
    MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
    MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUP)
    MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
	END_MSG_MAP()

  LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam,
                        LPARAM lParam, BOOL& bHandled)
  {
   // Keep track of the beginning of the line 
   // being drawn.
   m_startPoint.x = LOWORD(lParam);
   m_startPoint.y = HIWORD(lParam);
   
   return 0;
  }

  LRESULT OnLButtonUP(UINT uMsg, WPARAM wParam,
                      LPARAM lParam, BOOL& bHandled)
  {
   // The user has stopped drawing, so initialize
   // the start point variable.
   m_startPoint.x = -1;
   m_startPoint.y = -1;

   return 0;
  }

  LRESULT OnMouseMove(UINT uMsg, WPARAM wParam,
                      LPARAM lParam, BOOL& bHandled)
  {
   // If the user is currently drawing something...
   if (-1 != m_startPoint.x)
   {
    // Retrieve the current mouse coordinates.
    m_endPoint.x = LOWORD(lParam);
    m_endPoint.y = HIWORD(lParam);
   
    // Get the device context handle for drawing.
    HDC hdc = GetDC();

  
    // Create a solid pen with a width of 2 and 
    // the color specified by the user
    HPEN hPen = CreatePen(PS_SOLID, 2, m_color);

    // Select the new pen into the device context,
    // saving the old pen for later restoration.
    HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);

    // Use standard GDI calls to draw the line
    MoveToEx(hdc, m_startPoint.x, m_startPoint.y, NULL);
    LineTo(hdc, m_endPoint.x, m_endPoint.y);

    // Move the start point to end point so that 
    // we don't waste cycles rewriting the same
    // part of the line over and over again.
    m_startPoint.x = m_endPoint.x;
    m_startPoint.y = m_endPoint.y;

    // Restore the old pen.
    SelectObject(hdc, hOldPen);
   }
   
   return 0;
  }
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -