记时器怎么不管用
// MARQUEEDlg.h : header file
//
#if !defined(AFX_MARQUEEDLG_H__B2D813D2_D4FB_4262_AAF9_5C67D04CC5D0__INCLUDED_)
#define AFX_MARQUEEDLG_H__B2D813D2_D4FB_4262_AAF9_5C67D04CC5D0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CMARQUEEDlg dialog
// MARQUEEDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MARQUEE.h"
#include "MARQUEEDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMARQUEEDlg dialog
CMARQUEEDlg::CMARQUEEDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMARQUEEDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMARQUEEDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CMARQUEEDlg::~CMARQUEEDlg()
{
//Free memory.
GlobalUnlock(hPal);
GlobalFree(hPal);
}
void CMARQUEEDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMARQUEEDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMARQUEEDlg, CDialog)
//{{AFX_MSG_MAP(CMARQUEEDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMARQUEEDlg message handlers
BOOL CMARQUEEDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CenterWindow();
int colorIndex;
//Allocate memory for the palette entry array.
hPal = GlobalAlloc(GMEM_MOVEABLE,sizeof(LOGPALETTE)+4*sizeof(PALETTEENTRY));
if(hPal==NULL)
{
AfxMessageBox("Memory allocation error");
return TRUE;
}
//Initialize the LPLOGPALETTE pointer to point at the
//beginning of the locked allocated memory.
lpLogPal = (LPLOGPALETTE) GlobalLock(hPal);
//Fill members of the LOGPALETTE structure to indicate the
//Windows version and the number of palette entries.
lpLogPal->palVersion = 0x300;
lpLogPal->palNumEntries = 4;
//Fill all palette entries with colors.
for(colorIndex = 0; colorIndex < 4; colorIndex++)
{
lpLogPal->palPalEntry[colorIndex].peRed = (127 + colorIndex * 128 / 3);
lpLogPal->palPalEntry[colorIndex].peGreen = (127 + colorIndex * 128 /3);
lpLogPal->palPalEntry[colorIndex].peBlue = 0;
lpLogPal->palPalEntry[colorIndex].peFlags = PC_RESERVED;
}
//Create a logical palette from the array values.
if(!palette.CreatePalette(lpLogPal))
AfxMessageBox("Palette creation failed!");
//Note: we don't unlock and free the memory because
//we'll need this array later.
//Create an array of four CBrush objects.
for(long index = 0; index < 4; index++)
brush[index].CreateSolidBrush(0x1000000 | index);
//Set up the timer. Change the second parameter
//for faster or slower animation.
int nTimer = SetTimer(1,200,NULL);
ASSERT(nTimer !=0);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMARQUEEDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//Device context for painting;
CPaintDC dc(this);
//Here's where create the marquee lights.
int dotWidth;
long colorIndex;
//Select and realize the palette.
dc.SelectPalette(&palette,FALSE);
dc.RealizePalette();
//Calculate size of each "light"
CRect rect;
GetClientRect(&rect);
dotWidth = rect.Width() /24;
CRect rect1(dotWidth * 1.5,dotWidth * .5, dotWidth * 2.5, dotWidth * 1.5);
//Draw a black background.
dc.SelectStockObject(BLACK_BRUSH);
dc.Rectangle(rect);
colorIndex = 0;
//Draw the top row of lights.
do
{
dc.SelectObject(&brush[colorIndex]);
dc.Ellipse(rect1);
colorIndex == 3 ? colorIndex = 0 : colorIndex++;
rect1.OffsetRect(dotWidth * 1.5,0);
}while(rect1.BottomRight().x < (rect.Width() - dotWidth));
//Left column.
rect1.SetRect(dotWidth * .5,dotWidth * 1.5, dotWidth * 1.5, dotWidth * 2.5);
do
{
dc.SelectObject(&brush[colorIndex]);
dc.Ellipse(rect1);
colorIndex == 3 ? colorIndex = 0 : colorIndex++;
rect1.OffsetRect(0, dotWidth * 1.5);
}while(rect1.BottomRight().y < (rect.Height() - dotWidth));
//Right column.
rect1.SetRect((rect.Width() - dotWidth * 1.5), dotWidth * 1.5, (rect.Width() -
dotWidth * .5), dotWidth * 2.5);
do
{
dc.SelectObject(&brush[colorIndex]);
dc.Ellipse(rect1);
colorIndex == 3 ? colorIndex = 0 : colorIndex++;
rect1.OffsetRect(0, dotWidth * 1.5);
}while(rect1.BottomRight().y < (rect.Height() - dotWidth));
//Bottom column.
rect1.SetRect(dotWidth * 1.5, (rect.Height() - dotWidth * 1.5), dotWidth * 2.5,
(rect.Height() - dotWidth * .5));
do
{
dc.SelectObject(&brush[colorIndex]);
dc.Ellipse(rect1);
colorIndex == 3 ? colorIndex = 0 : colorIndex++;
rect1.OffsetRect( dotWidth * 1.5, 0);
}while(rect1.BottomRight().x < (rect.Width() - dotWidth));
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMARQUEEDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMARQUEEDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
int index;
for(index = 4; index > 0; index --)
lpLogPal->palPalEntry[index] = lpLogPal->palPalEntry[index-1];
lpLogPal->palPalEntry[0] = lpLogPal->palPalEntry[4];
palette.AnimatePalette(0,4,lpLogPal->palPalEntry);
CDialog::OnTimer(nIDEvent);
}
class CMARQUEEDlg : public CDialog
{
// Construction
public:
CMARQUEEDlg(CWnd* pParent = NULL); // standard constructor
~CMARQUEEDlg();
// Dialog Data
//{{AFX_DATA(CMARQUEEDlg)
enum { IDD = IDD_MARQUEE_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMARQUEEDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CMARQUEEDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CPalette palette;
//Array of brushes.
CBrush brush[4];
//For palettes.
LPLOGPALETTE lpLogPal;
HANDLE hPal;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MARQUEEDLG_H__B2D813D2_D4FB_4262_AAF9_5C67D04CC5D0__INCLUDED_)
这个程序是用记时器控制对话框上的灯的闪烁,类似霓虹灯的效果,我的问题是
记时器没有起到应有的效果,请高手帮着看看