如何自画 CSliderCtrl 派生类?

robothn 2002-03-08 04:56:58
该控件放在 toolbar 上
OnPaint()里画,track 时有问题,画的很凌乱
...全文
150 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
robothn 2002-03-08
  • 打赏
  • 举报
回复
gameboy999 就是牛,我没做过界面,
只用 OnPaint() OnNcPaint() OnEraseBkground() 就是不行
佩服佩服
robothn 2002-03-08
  • 打赏
  • 举报
回复
robothn@sina.com
一起看看
robothn 2002-03-08
  • 打赏
  • 举报
回复
我先试试
gameboy999 2002-03-08
  • 打赏
  • 举报
回复
// MySliderCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "testdlg.h"
#include "MySliderCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMySliderCtrl

CMySliderCtrl::CMySliderCtrl()
{
}

CMySliderCtrl::~CMySliderCtrl()
{
R = 0;
G = 0;
B = 0;
m_MouseDown = FALSE;
}


BEGIN_MESSAGE_MAP(CMySliderCtrl, CSliderCtrl)
//{{AFX_MSG_MAP(CMySliderCtrl)
ON_WM_DRAWITEM()
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomdraw)
ON_WM_PAINT()
ON_WM_PARENTNOTIFY_REFLECT()
ON_NOTIFY_REFLECT(NM_RELEASEDCAPTURE, OnReleasedcapture)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
ON_WM_TIMER()
ON_WM_LBUTTONUP()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMySliderCtrl message handlers

void CMySliderCtrl::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
CSliderCtrl::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

void CMySliderCtrl::OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
//MessageBox("hi");
*pResult = 0;
}


void CMySliderCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
/*CBitmap bBitmap;
bBitmap.LoadBitmap(IDB_BITMAP1);
CDC tempDC;
tempDC.CreateCompatibleDC(&dc);
tempDC.SelectObject(&bBitmap);
*/
CRect rect;
this->GetClientRect(&rect);
//dc.BitBlt(rect.left ,rect.top ,rect.Width(),rect.Height(),&tempDC,0,0,SRCCOPY);
dc.FillSolidRect(&rect,RGB(R,255 - G,B));
/*
CRect rectThumb;
this->GetThumbRect(rectThumb);
char hhh[9];
sprintf(hhh,"%d",this->GetPos());
//dc.FillSolidRect(&rectThumb,RGB(this->GetPos() * 2,255 - this->GetPos() * 2,this->GetPos() * 2));
dc.SetTextColor(RGB(255,255,255));
dc.SetBkMode(TRANSPARENT);

if(this->GetStyle() & TBS_VERT)
dc.TextOut(3,rectThumb.top,CString("▲"));
else
dc.TextOut(rectThumb.left,3,CString("▲"));
//bBitmap.DeleteObject();
*/
MakeChange();

//CSliderCtrl::OnPaint();//for painting messages
}



void CMySliderCtrl::ParentNotify(UINT message, LPARAM lParam)
{
// TODO: Add your message handler code here

}

void CMySliderCtrl::OnReleasedcapture(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here

*pResult = 0;
}

void CMySliderCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Invalidate(FALSE);
m_MouseDown = TRUE;
CSliderCtrl::OnLButtonDown(nFlags, point);
}

void CMySliderCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_MouseDown) Invalidate(FALSE);
CSliderCtrl::OnMouseMove(nFlags, point);
}

BOOL CMySliderCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
TRACKMOUSEEVENT event;
event.cbSize = sizeof(event);
event.dwFlags = TME_LEAVE;
event.hwndTrack = this->GetSafeHwnd();
event.dwHoverTime = HOVER_DEFAULT;

_TrackMouseEvent(&event);
m_bIn = TRUE;
//先触发一次再说,因为Timer是隔一段时间才触发的
OnTimer(1);
SetTimer(1,20,0);
return CSliderCtrl::OnSetCursor(pWnd, nHitTest, message);
}

BOOL CMySliderCtrl::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_MOUSELEAVE)
{
m_bIn = FALSE;
//先触发一次再说,因为Timer是隔一段时间才触发的
OnTimer(1);
SetTimer(1,20,0);
//MakeChange();
}

return CSliderCtrl::PreTranslateMessage(pMsg);
}

void CMySliderCtrl::MakeChange()
{
CRect rect;
this->GetClientRect(&rect);
CClientDC dc(this);
//dc.Draw3dRect(&rect,RGB(255 - R,255 - G,255 - B),RGB(255 - R,255 - G,255 - B));
dc.Draw3dRect(&rect,RGB(255,G,B),RGB(255,G,B));

CRect rectThumb;
this->GetThumbRect(rectThumb);
char hhh[9];
sprintf(hhh,"%d",this->GetPos());
//dc.FillSolidRect(&rectThumb,RGB(this->GetPos() * 2,255 - this->GetPos() * 2,this->GetPos() * 2));
//dc.SetTextColor(RGB(255 - R,255 - G,255 - B));
dc.SetTextColor(RGB(R,G,B));
dc.SetBkMode(TRANSPARENT);

if(this->GetStyle() & TBS_VERT)
dc.TextOut(3,rectThumb.top,CString("▲"));
else
dc.TextOut(rectThumb.left,3,CString("▲"));

//dc.FillSolidRect(&rect,RGB(R,G,B));
}

void CMySliderCtrl::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(m_bIn)
{
if((R >= 220) && (G >= 220) && (B >= 220))
{
R = G = B = 255;
KillTimer(1);
}
else
Invalidate();

if(R < 220) R += 30;
if(G < 220) G += 30;
if(B < 220) B += 30;
}
else
{
if((R <= 11) && (G <= 11) && (B <= 11))
{
R = G = B = 0;
KillTimer(1);
}
else
Invalidate();

if ( R > 11 ) R -= 5;
if ( G > 11 ) G -= 5;
if ( B > 11 ) B -= 5;
}

CSliderCtrl::OnTimer(nIDEvent);
}


void CMySliderCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_MouseDown = FALSE;
CSliderCtrl::OnLButtonUp(nFlags, point);
}

BOOL CMySliderCtrl::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default

return 0;//CSliderCtrl::OnEraseBkgnd(pDC);
}
gameboy999 2002-03-08
  • 打赏
  • 举报
回复
看我写的,挺有意思的
#if !defined(AFX_MYSLIDERCTRL_H__E644AAD1_5156_4773_A426_324DDC0D4CBB__INCLUDED_)
#define AFX_MYSLIDERCTRL_H__E644AAD1_5156_4773_A426_324DDC0D4CBB__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MySliderCtrl.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMySliderCtrl window

class CMySliderCtrl : public CSliderCtrl
{
// Construction
public:
CMySliderCtrl();

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMySliderCtrl)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
//}}AFX_VIRTUAL

// Implementation
public:
BOOL m_MouseDown;
BYTE R;
BYTE G;
BYTE B;
void MakeChange();
int m_bIn;
virtual ~CMySliderCtrl();

// Generated message map functions
protected:
//{{AFX_MSG(CMySliderCtrl)
afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg void OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnPaint();
afx_msg void ParentNotify(UINT message, LPARAM lParam);
afx_msg void OnReleasedcapture(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYSLIDERCTRL_H__E644AAD1_5156_4773_A426_324DDC0D4CBB__INCLUDED_)
vcfor 2002-03-08
  • 打赏
  • 举报
回复
email? 这有例程
robothn 2002-03-08
  • 打赏
  • 举报
回复
?

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧