效果演示
上位机MFC波动文本实例
例程从CStatic派生一个CSinusFrame类,实现正弦波形滚动文本,其中,滚动速度、滚动文本及其字体都可以设置,参见上图。
例程使用的两文件源代码可以在下面复制使用,也可以直接下载例程源代码使用:
实现过程:
1.新建一对话框工程,界面如上所示,添加控件并排版。
滑块,编辑框,按钮,图像控件。其中图像控件的ID要做修改IDC_STATIC1,并设置属性为矩形等。
2.将例程根目录文件复制并加载到自己工程中使用,这样工程会多出一集成类CSinusDlg。
接着就是这个类的使用。包含头文件,并与图像控件关联,当前滑块编辑框控件也关联变量。
#include "SinusFrame.h"
CFont m_font;
// Dialog Data
//{{AFX_DATA(CCTestDlg)
enum { IDD = IDD_CTEST_DIALOG };
CSinusFrame m_Static;
CSliderCtrl m_Slider;
CString m_Text;
//}}AFX_DATA
紧接着初始化变量
VERIFY(m_font.CreatePointFont(220, "Arial Black"));
m_Text = "_____________hi____________________" "this is www.gkbc8.com demo for static."
"you can use the code any time any where ________________________________"
m_Slider.SetRange(-10, 10);
m_Slider.SetPos(6);
m_Slider.SetLineSize(1);
m_Slider.SetPageSize(5);
m_Static.SetText(m_Text);
m_Static.SetFont(&m_font);
m_Static.SetSinusSpeed(6);
m_Static.Start();
UpdateData(false);
3.后面就是滑块,编辑框和按钮功能的实现。
添加WM_HSCROLL消息处理函数
- <blockquote>void CCTestDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
复制代码
双击编辑框添加文本更改处理函数点击函数- void CCTestDlg::OnChangeEdit1()
- {
- UpdateData();
- if(m_Text.GetLength() > 0)
- m_Static.SetText(m_Text);
- }
复制代码 双击按钮控件,添加字体设置的
- void CCTestDlg::OnButton1()
- {
- LOGFONT lf;
- m_font.GetLogFont(&lf);
-
- CFontDialog dlg(&lf);
- if(dlg.DoModal()==IDOK)
- {
- dlg.GetCurrentFont(&lf);
- m_font.DeleteObject();
- VERIFY(m_font.CreateFontIndirect(&lf));
- m_Static.SetFont(&m_font);
- }
- }
复制代码 4.最后就是编译运行例程,查看效果。下面是例程中的两文件源代码:
- #ifndef AFX_SINUSFRAME_H__41258CBF_8436_11D1_9C04_084D65000000__INCLUDED_
- #define AFX_SINUSFRAME_H__41258CBF_8436_11D1_9C04_084D65000000__INCLUDED_
- class CSinusFrame : public CStatic
- {
- // Konstruktion
- public:
- CSinusFrame();
- // Attribute
- public:
- // Operationen
- public:
- // erschreibungen
- // Vom Klassen-Assistenten generierte virtuelle Funktions黚erschreibungen
- //{{AFX_VIRTUAL(CSinusFrame)
- //}}AFX_VIRTUAL
- // Implementierung
- public:
- void SetSinusSpeed(int n);
- void SetFont(CFont* pFont);
- void Stop();
- void Start();
- void SetText(LPCTSTR lpszText);
- virtual ~CSinusFrame();
- // Generierte Nachrichtenzuordnungsfunktionen
- protected:
- //{{AFX_MSG(CSinusFrame)
- afx_msg void OnFontChange();
- afx_msg void OnSize(UINT nType, int cx, int cy);
- afx_msg void OnClose();
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- private:
- void Cleanup();
- void DoCharBlit();
- void Scroll();
- TCHAR GetNextChar();
- void DoSinScroll();
- int GetNextSinusOffset();
- double GetNextSinValue();
- void Initialize();
- int m_nSinusSpeed;
- int m_nSinusOffset;
- int m_nSinusPos;
- int m_nCharWidth;
- int m_nCharHeight;
- int m_nPixLeft;
- int m_nTextPos;
- double m_dSinusTable[720];
-
- UINT m_nTimer;
-
- static void CALLBACK AFX_EXPORT TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime);
-
- bool m_bRunning;
- bool m_bInitialized;
- CFont* m_pFont;
- CBitmap* m_pBMSinus;
- CBitmap* m_pBMScroll;
- CDC* m_pDCScroll;
- CDC* m_pDCSinus;
- CRect m_rcScroll;
- CRect m_rcClient;
- CString m_strText;
- };
- /////////////////////////////////////////////////////////////////////////////
- //{{AFX_INSERT_LOCATION}}
- // Microsoft Developer Studio f黦t zus鋞zliche Deklarationen unmittelbar vor der vorhergehenden Zeile ein.
- #endif // AFX_SINUSFRAME_H__41258CBF_8436_11D1_9C04_084D65000000__INCLUDED_
复制代码- // SinusFrame.cpp: Implementierungsdatei
- //
- #include "stdafx.h"
- #include "SinusFrame.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CSinusFrame
- CSinusFrame::CSinusFrame()
- {
- m_bInitialized = false;
- m_bRunning = false;
- m_nTimer = 0;
- m_nTextPos = 0;
- m_nSinusPos = 0;
- m_nPixLeft = 0;
- m_nSinusOffset = 0;
- m_nSinusSpeed = -3;
- m_pFont = NULL;
- m_pDCScroll = NULL;
- m_pDCSinus = NULL;
- m_pBMScroll = NULL;
- m_pBMSinus = NULL;
-
- const double rad = 2.0*3.141592654/360.0;
- int i; float f;
- for(i=0, f=0.0; i<720; i++, f+=0.5f)
- m_dSinusTable[i] = sin(rad*f);
- }
- CSinusFrame::~CSinusFrame()
- {
- Cleanup();
- }
- BEGIN_MESSAGE_MAP(CSinusFrame, CStatic)
- //{{AFX_MSG_MAP(CSinusFrame)
- ON_WM_FONTCHANGE()
- ON_WM_SIZE()
- ON_WM_CLOSE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // Behandlungsroutinen f黵 Nachrichten CSinusFrame
- void CSinusFrame::Initialize()
- {
- ASSERT(::IsWindow(m_hWnd));
- if(m_bInitialized==true)
- return;
- CFont* pOldFont;
- // Font-Ausma遝 ermitteln...
- CDC* pDC = GetDC();
- ASSERT(pDC);
- if(m_pFont)
- pOldFont = pDC->SelectObject(m_pFont);
- TEXTMETRIC tm;
- VERIFY(pDC->GetTextMetrics(&tm));
- m_nCharHeight = tm.tmHeight;
- m_nCharWidth = tm.tmMaxCharWidth;
- GetClientRect(m_rcClient);
- Cleanup();
- m_pDCScroll = new CDC;
- m_pDCSinus = new CDC;
- m_pBMScroll = new CBitmap;
- m_pBMSinus = new CBitmap;
- ASSERT(m_pDCScroll && m_pDCSinus && m_pBMScroll && m_pBMSinus);
- m_rcScroll.SetRect(0, 0, m_rcClient.Width()+m_nCharWidth, m_nCharHeight);
-
- VERIFY(m_pDCScroll->CreateCompatibleDC(pDC));
- VERIFY(m_pBMScroll->CreateCompatibleBitmap(pDC, m_rcScroll.Width(), m_rcScroll.Height()));
-
- m_pDCScroll->SelectObject(m_pBMScroll);
- m_pDCScroll->PatBlt(0, 0, m_rcScroll.Width(), m_rcScroll.Height(), BLACKNESS);
- m_pDCScroll->SetTextColor(RGB(255,255,192));
- m_pDCScroll->SetBkColor(RGB(0,0,0));
-
- if(m_pFont)
- m_pDCScroll->SelectObject(m_pFont);
-
- VERIFY(m_pDCSinus->CreateCompatibleDC(pDC));
- VERIFY(m_pBMSinus->CreateCompatibleBitmap(pDC, m_rcClient.Width(), m_rcClient.Height()));
-
- m_pDCSinus->SelectObject(m_pBMSinus);
- m_pDCSinus->PatBlt(0, 0, m_rcClient.Width(), m_rcClient.Height(), BLACKNESS);
- ReleaseDC(pDC);
-
- if(m_pFont)
- pDC->SelectObject(pOldFont);
- m_bInitialized = true;
- }
- void CSinusFrame::OnFontChange()
- {
- CStatic::OnFontChange();
- m_bInitialized = false;
- }
- void CSinusFrame::OnSize(UINT nType, int cx, int cy)
- {
- CStatic::OnSize(nType, cx, cy);
- m_bInitialized = false;
- }
- void CALLBACK AFX_EXPORT CSinusFrame::TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
- {
- ASSERT(hWnd);
- CSinusFrame* pSin = (CSinusFrame*)CWnd::FromHandle(hWnd);
- pSin->Scroll();
- }
- void CSinusFrame::SetText(LPCTSTR lpszText)
- {
- m_strText = lpszText;
- m_nTextPos = 0;
- m_bInitialized = false;
- }
- void CSinusFrame::Start()
- {
- ASSERT(::IsWindow(m_hWnd));
- if(m_bRunning)
- Stop();
- m_bRunning = true;
- Initialize();
- m_nTimer = SetTimer(1, 15, TimerProc);
- ASSERT(m_nTimer!=0);
- }
- void CSinusFrame::Stop()
- {
- ASSERT(::IsWindow(m_hWnd));
- if(m_bRunning)
- {
- ASSERT(m_nTimer!=0);
- KillTimer(m_nTimer);
- m_bRunning = false;
- }
- }
- TCHAR CSinusFrame::GetNextChar()
- {
- if(m_nTextPos >= m_strText.GetLength())
- m_nTextPos = 0;
- return m_strText[m_nTextPos++];
- }
- void CSinusFrame::Scroll()
- {
- if(m_bInitialized && m_bRunning)
- {
- DoCharBlit();
- DoSinScroll();
-
- CDC* pDC = GetDC();
- pDC->BitBlt(0, 0, m_rcClient.Width(), m_rcClient.Height(), m_pDCSinus, 0, 0, SRCCOPY);
- ReleaseDC(pDC);
- }
- else
- Initialize();
- }
- void CSinusFrame::DoCharBlit()
- {
- if(m_nPixLeft <= 0)
- {
- TCHAR c = GetNextChar();
- m_pDCScroll->PatBlt(m_rcClient.right, m_rcScroll.top, m_rcScroll.right, m_rcScroll.bottom, BLACKNESS);
- CSize sz = m_pDCScroll->GetTextExtent((LPCTSTR)&c, 1);
- m_pDCScroll->TextOut(m_rcClient.right, m_rcScroll.top, (LPCTSTR)&c, 1);
- m_nPixLeft = sz.cx;
- }
- }
- void CSinusFrame::DoSinScroll()
- {
- int nScrollWidth = -2;
-
- // first scroll Scroll-Bitmap
- m_pDCScroll->BitBlt(0, 0, m_rcScroll.Width()-2, m_rcScroll.Height(), m_pDCScroll, 2, 0, SRCCOPY);
- // clear sinus-bitmap
- m_pDCSinus->PatBlt(0, 0, m_rcClient.Width(), m_rcClient.Height(), BLACKNESS);
- // get sinustable-startposition
- m_nSinusPos = GetNextSinusOffset();
- for(int i=0; i<m_rcClient.Width(); i++)
- {
- double d = GetNextSinValue();
-
- int y = ((m_rcClient.Height() - 2*m_rcScroll.Height()) >> 1) + (m_rcScroll.Height() >> 1);
- y += (int)(d * y);
- m_pDCSinus->BitBlt(i, y, 1, y+m_rcScroll.Height(), m_pDCScroll, i, 0, SRCCOPY);
- }
- m_nPixLeft += nScrollWidth;
- }
- void CSinusFrame::OnClose()
- {
- Stop();
- CStatic::OnClose();
- }
- double CSinusFrame::GetNextSinValue()
- {
- if(++m_nSinusPos >= 720)
- m_nSinusPos = 0;
- return m_dSinusTable[m_nSinusPos];
- }
- int CSinusFrame::GetNextSinusOffset()
- {
- m_nSinusOffset += m_nSinusSpeed;
-
- if(m_nSinusSpeed > 0 && m_nSinusOffset >= 720)
- m_nSinusOffset = 0;
- else if(m_nSinusSpeed < 0 && m_nSinusOffset < 0)
- m_nSinusOffset = 719;
- return m_nSinusOffset;
- }
- void CSinusFrame::SetFont(CFont * pFont)
- {
- ASSERT(pFont);
- m_pFont = pFont;
- m_nTextPos = 0;
- m_bInitialized = false;
- }
- void CSinusFrame::SetSinusSpeed(int n)
- {
- m_nSinusSpeed = n;
- }
- void CSinusFrame::Cleanup()
- {
- delete m_pDCScroll;
- delete m_pDCSinus;
- delete m_pBMScroll;
- delete m_pBMSinus;
- m_pDCScroll = NULL;
- m_pDCSinus = NULL;
- m_pBMScroll = NULL;
- m_pBMSinus = NULL;
- }
复制代码
|