导航
  • 主页
  • VC/MVC基础类
  • .NET 技术
  • VC/MFC界面
  • VC/MFC 进程
  • VC/MFC 数据库
  • VC/MFC&Web
  • Doc/View

记时器怎么不管用

qjwj_hl 2002-07-28 09:49:25
// 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_)
这个程序是用记时器控制对话框上的灯的闪烁,类似霓虹灯的效果,我的问题是
记时器没有起到应有的效果,请高手帮着看看
...全文
14 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
howtotell 2002-07-28
你现在你的OnTimer里边添加一个
MessageBox("");
检查是不是定时器没有定时运行,还是你其他绘图的代码有问题.还有去掉
Settimer后边的
ASSERT(nTimer !=0);
试试看.
回复
badtank 2002-07-28
你把 int nTimer = SetTimer(1,200,NULL); 中的第一个参数改一下试试,

时钟id太小了有可能跟系统是中冲突,所以最好改得大一些!
回复
badtank 2002-07-28
你把 int nTimer = SetTimer(1,200,NULL); 中的第一个参数改一下试试,

时钟id太小了有可能跟系统是中冲突,所以最好改得大一些!
回复
doublesword 2002-07-28
要快!
回复
doublesword 2002-07-28
ontimer()函数应该没有问题?你的调试方法对不对?
要不你把程序给我看看:double.sword@163.com
回复
qjwj_hl 2002-07-28
晕了就别看
回复
watersome 2002-07-28
你不会有选择性的复制一段程序上来,无关的程序看的都晕!
回复
qjwj_hl 2002-07-28
呵呵断点跟踪我都试了,可以跳到OnTimer(UINT nIDEvent)当中
下面是头文件,请好心的各位在帮我一把
// 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

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_)
回复
Hogenbrown 2002-07-28
你没有Debug一下吗?看看你的记时器运行没有啊?跟踪一下不就知道了吗?你不会没有学会用断点跟踪吧.
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告