自己写的线控件,可以上方显示标题。

编程积累 专栏收录该内容
6 篇文章 0 订阅

花了两个小时替别人完成了一个线控件,可以上方显示标题。邮件寄着不方便,于是便post到这儿。
colorlin.h//made by zheng017
#pragma once
#include "stdafx.h"

#define COLORLINE_CLASSNAME _T("BSColorLine")
class ColorLine :public CWnd
{
public:
 ColorLine(DWORD dwBkColor=RGB(255,0,0),int iLineHeight=5,const char *pWndText="BSColorLine");
 ~ColorLine();
 DECLARE_DYNCREATE(ColorLine)

protected:
 BOOL RegisterWindowClass();
 HCURSOR m_hHandCursor;
 bool m_bDrag ;
 bool m_bFirstDrag;
 CRect   m_rectPre;
 CRect   m_rectNow;
 CRect m_lastTextRect;
 CPen m_DrawLinePen;
 DWORD   m_dwBkColor;
 int  m_iLineHeight;
 char m_wndText[256];

 DECLARE_MESSAGE_MAP()
public:
 void    GetWindowPos(CRect &rect);
 long PutWindowPos(CRect &rect);
 long    PutWindowText(const char *pWindowText);
 afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
 afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
 afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
 afx_msg BOOL OnEraseBkgnd(CDC* pDC);
 afx_msg void OnPaint();
};
//colorline.cpp made by zheng017
#include "stdafx.h"
#include "colorline.h"

IMPLEMENT_DYNCREATE(ColorLine, CWnd)
ColorLine::ColorLine (DWORD dwBkColor,int iLineHeight,const char *pWndText)
:m_dwBkColor(dwBkColor)
,m_iLineHeight(iLineHeight)
,m_rectNow(0,0,0,0)
,m_rectPre(0,0,0,0)
{
 RegisterWindowClass();
 m_hHandCursor = LoadCursor(NULL,MAKEINTRESOURCE(32649)); 
 m_DrawLinePen.CreatePen (0,1,m_dwBkColor);
 if (pWndText != NULL)
  lstrcpy(m_wndText,pWndText);
}

ColorLine::~ColorLine ()
{
 DeleteObject(m_DrawLinePen);
 DestroyCursor(m_hHandCursor);
}

BOOL ColorLine::RegisterWindowClass()
{
 HINSTANCE hInst = AfxGetInstanceHandle();
 WNDCLASS wndcls;
 if (!(::GetClassInfo(hInst, COLORLINE_CLASSNAME, &wndcls)))
 {
  wndcls.style            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  wndcls.lpfnWndProc      = ::DefWindowProc;
  wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
  wndcls.hInstance        = hInst;
  wndcls.hIcon            = NULL;
  wndcls.hCursor          = NULL;
  wndcls.hbrBackground    = (HBRUSH) (COLOR_WINDOW+1);
  wndcls.lpszMenuName     = NULL;
  wndcls.lpszClassName    = COLORLINE_CLASSNAME;

  if (!AfxRegisterClass(&wndcls))
  {
   AfxThrowResourceException();
   return FALSE;
  }
 }
 return TRUE;
}

BEGIN_MESSAGE_MAP(ColorLine, CWnd)
 ON_WM_LBUTTONUP()
 ON_WM_LBUTTONDOWN()
 ON_WM_RBUTTONUP()
 ON_WM_MOUSEMOVE()
 ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
void  ColorLine::GetWindowPos(CRect &rect)
{
 GetWindowRect(&rect);
}
long ColorLine::PutWindowPos(CRect &rect)
{
 return ::SetWindowPos (GetSafeHwnd(),HWND_TOP,rect.left  ,rect.top ,0,0,
  SWP_NOSIZE|SWP_SHOWWINDOW|SWP_NOZORDER);
}
void ColorLine::OnLButtonUp(UINT nFlags, CPoint point)
{
 CWnd *pParent = GetParent();
 if (pParent)
 {
  pParent ->InvalidateRect (m_lastTextRect,TRUE);
 }
 CWnd::OnLButtonUp (nFlags,point);
 ReleaseCapture();
 ClipCursor(NULL);
 SetCursor(LoadCursor(NULL,IDC_ARROW));
 m_bDrag = false; 
 ::SetWindowPos (GetSafeHwnd(),HWND_TOP,m_rectPre.left  ,m_rectPre.top,0,0,
  SWP_NOSIZE|SWP_SHOWWINDOW|SWP_NOZORDER|SWP_FRAMECHANGED);

}
long  ColorLine::PutWindowText(const char *pWindowText)
{
 lstrcpy(m_wndText,pWindowText);
 return 0;
}
void ColorLine::OnLButtonDown(UINT nFlags, CPoint point)
{
 CWnd::OnLButtonDown (nFlags,point);
 SetFocus();
 SetCapture();
 SetCursor(m_hHandCursor);
 CWnd *pParent = GetParent();
 pParent->GetWindowRect (&m_rectNow); 
 m_rectNow.InflateRect (-5,-5,-5,-5);
 ClipCursor(&m_rectNow);
 m_bDrag = true;
 GetWindowRect(&m_rectNow);  
 memset(&m_rectPre,0x0,sizeof(RECT));
 m_bFirstDrag = true;
}
void ColorLine::OnRButtonUp(UINT nFlags, CPoint point)
{
 CWnd::OnRButtonUp (nFlags,point);
}
void ColorLine::OnMouseMove(UINT nFlags, CPoint point)
{
 CWnd::OnMouseMove (nFlags,point);
 if (true == m_bDrag)
 {
  CWnd *pParent = GetParent();
  CDC *cdcParent = pParent->GetDC ();
  if (true == m_bFirstDrag)
  {
   ShowWindow(SW_HIDE);
  }

  if (cdcParent)
  {
   int iOldMode = 0;
   int iDrawLineCnt =0; ;
   CPen *pOldPen = (CPen*)cdcParent ->SelectObject (m_DrawLinePen);
   if (m_rectPre.Width () >0)
   {
    iOldMode = cdcParent ->SetROP2 (R2_XORPEN);
    for (iDrawLineCnt=0;iDrawLineCnt<m_iLineHeight;++iDrawLineCnt)
    {
     cdcParent ->MoveTo (m_rectPre.left ,m_rectPre.top+iDrawLineCnt );
     cdcParent ->LineTo (m_rectPre.right ,m_rectPre.top+iDrawLineCnt );
    }
    cdcParent ->SetROP2(iOldMode);
   }
   int iRectWidth = m_rectNow.Width ();
   int iRectHeight = m_rectNow.Height ();
   ClientToScreen(&point);
   pParent->ScreenToClient (&point);
   m_rectNow.left = point.x;
   m_rectNow.top  = point.y ;
   m_rectNow.right = m_rectNow.left+iRectWidth;
   m_rectNow.bottom = m_rectNow.top +iRectHeight;
   if (iRectWidth >0)
   {
    iOldMode =  cdcParent ->SetROP2 (R2_XORPEN);
    for (iDrawLineCnt=0;iDrawLineCnt<m_iLineHeight;++iDrawLineCnt)
    {
     cdcParent ->MoveTo (m_rectNow.left ,m_rectNow.top+iDrawLineCnt );
     cdcParent ->LineTo (m_rectNow.right ,m_rectNow.top+iDrawLineCnt );
    }
    cdcParent ->SetROP2(iOldMode);
   }
 
   memcpy(&m_rectPre,&m_rectNow,sizeof(RECT));
  }
 }
}
BOOL ColorLine::OnEraseBkgnd(CDC* pDC)
{
 CBrush backBrush(m_dwBkColor);
 CBrush* pOldBrush = pDC->SelectObject(&backBrush);
 pDC->GetClipBox(&m_lastTextRect);
 
 pDC->PatBlt(m_lastTextRect.left, m_lastTextRect.top, m_lastTextRect.Width(), m_lastTextRect.Height(),PATCOPY);
 pDC->SelectObject(pOldBrush);
 if (lstrlen(m_wndText) >0 && false == m_bDrag)
 {
  CWnd *pParent = GetParent();
  CDC *cdcParent = pParent->GetDC ();
  if (cdcParent)
  {
   ClientToScreen(&m_lastTextRect);
   pParent->ScreenToClient (&m_lastTextRect);
   m_lastTextRect.top -= 15;
   m_lastTextRect.bottom  -= 5;
   int iOldMode = cdcParent ->SetROP2 (R2_XORPEN);
   cdcParent->SetBkMode (TRANSPARENT);
   cdcParent->DrawText (m_wndText,m_lastTextRect,DT_CENTER);
   cdcParent ->SetROP2(iOldMode);
  }
 }
 return TRUE;
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值