http://www.csdn.net/expert/topic/521/521622.xml
12 个解决方案
#1
up!
#2
up!
#3
up!
#4
windows显示图形是画到一个dc(设备)上,dc有多种显示器就是一种.我们平时画图是直接画到屏幕上,我们经常画不止一次,比如画100条直线就有一百个向屏幕上画的过程,于是就产生了闪烁.
双缓冲就是先把这100直线画到"内存dc"中,在一次性的拷贝到"屏幕dc"上就消除了闪烁.
显示bmp图像的方法就是一种最简单的双缓冲技术应用。
CPaintDC dc(this);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
CDC memDC;
memDC.Create //这个函数名太长记不住了
memDC.SelectObject(&bmp); //把bmp图象画到内存dc上
dc.BitBlt(...,&memDC,...SRCCOPY);//把memDC拷贝到屏幕dc上
CDC
双缓冲就是先把这100直线画到"内存dc"中,在一次性的拷贝到"屏幕dc"上就消除了闪烁.
显示bmp图像的方法就是一种最简单的双缓冲技术应用。
CPaintDC dc(this);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
CDC memDC;
memDC.Create //这个函数名太长记不住了
memDC.SelectObject(&bmp); //把bmp图象画到内存dc上
dc.BitBlt(...,&memDC,...SRCCOPY);//把memDC拷贝到屏幕dc上
CDC
#5
thx you, but how about multimedia communication??????
#6
我给你一个很好的例子!!
给我你的E-MAIL
给我你的E-MAIL
#7
gzup
#8
Robert2001:
给我一个很好的例子!!
cvip11@yahoo.com
给我一个很好的例子!!
cvip11@yahoo.com
#9
我现在就在搞这个缓冲技术,过上一两天,大概就会有结果了,到时候可以有一个现成的例子了.要吗?
#10
我正在做这个;用的是上面的思路,可惜出来的是花屏,怎么会事呢?谁会阿!!急阿!sly582@sohu.com
#11
我也要,sly582可以给我吗?(arcill@21cn.com),多谢!
#12
给各例子看看! 直接编译可以运行!
#include "stdafx.h"
#include "Butterfly.h"
#include "ButterflyDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButterflyDlg dialog
CButterflyDlg::CButterflyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CButterflyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CButterflyDlg)
// 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);
turn=2;
speed=2;
rect.top=30;rect.left =10;
rect.right=350;rect.bottom =300;
bflyRect.left =bflyRect.top =0;
bflyRect.right =bflyRect.bottom =77;
}
void CButterflyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CButterflyDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CButterflyDlg, CDialog)
//{{AFX_MSG_MAP(CButterflyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_ABOUT, OnAbout)
ON_BN_CLICKED(ID_MY_PLAY, OnMyPlay)
ON_BN_CLICKED(ID_MY_FAST, OnMyFast)
ON_BN_CLICKED(ID_MY_SLOW, OnMySlow)
ON_BN_CLICKED(ID_END, OnEnd)
ON_WM_TIMER()
ON_BN_CLICKED(ID_MY_BACK, OnMyBack)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButterflyDlg message handlers
BOOL CButterflyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CButterflyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CButterflyDlg::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
{
CDialog::OnPaint();
}
CClientDC myDC(this);
CDC memDC;
CBitmap bitmap;
memDC.CreateCompatibleDC (&myDC);
switch(turn)
{ case 1: bitmap.LoadBitmap (IDB_FLY1);
break;
case 2: bitmap.LoadBitmap (IDB_FLY2);
break;
}
memDC.SelectObject (bitmap);
myDC.Rectangle(rect);
myDC.BitBlt (pos.x,pos.y,77,77,&memDC,0,0,SRCCOPY);
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CButterflyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CButterflyDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg dlg;
dlg.DoModal ();
}
void CButterflyDlg::OnMyPlay()
{
// TODO: Add your control notification handler code here
SetTimer(1,50,NULL);
}
void CButterflyDlg::OnMyFast()
{
// TODO: Add your control notification handler code here
InvalidateRect(rect);
}
void CButterflyDlg::OnMySlow()
{
// TODO: Add your control notification handler code here
speed-=3;
// InvalidateRect(bflyRect.OffsetRect (pos));
InvalidateRect(rect);
}
void CButterflyDlg::OnEnd()
{
// TODO: Add your control notification handler code here
KillTimer(1);
}
void CButterflyDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
pos.x =pos.x +speed;
pos.y =pos.y;
if(pos.x +77>rect.right||
pos.x<rect.left||
pos.y<rect.top||
pos.y+70>rect.bottom)
{if(speed>0)
pos.x=rect.left;
else
pos.x=rect.right-80;
pos.y=rect.top+rect.Height ()/2-35;
}
switch(turn)
{ case 1: turn=2; break;
case 2: turn=1; break;
}
InvalidateRect(rect);
CDialog::OnTimer(nIDEvent);
}
void CButterflyDlg::OnMyBack()
{
// TODO: Add your control notification handler code here
speed=-30;
//InvalidateRect(bflyRect.OffsetRect (pos));
InvalidateRect(rect);
}
#include "stdafx.h"
#include "Butterfly.h"
#include "ButterflyDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButterflyDlg dialog
CButterflyDlg::CButterflyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CButterflyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CButterflyDlg)
// 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);
turn=2;
speed=2;
rect.top=30;rect.left =10;
rect.right=350;rect.bottom =300;
bflyRect.left =bflyRect.top =0;
bflyRect.right =bflyRect.bottom =77;
}
void CButterflyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CButterflyDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CButterflyDlg, CDialog)
//{{AFX_MSG_MAP(CButterflyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_ABOUT, OnAbout)
ON_BN_CLICKED(ID_MY_PLAY, OnMyPlay)
ON_BN_CLICKED(ID_MY_FAST, OnMyFast)
ON_BN_CLICKED(ID_MY_SLOW, OnMySlow)
ON_BN_CLICKED(ID_END, OnEnd)
ON_WM_TIMER()
ON_BN_CLICKED(ID_MY_BACK, OnMyBack)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButterflyDlg message handlers
BOOL CButterflyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CButterflyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CButterflyDlg::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
{
CDialog::OnPaint();
}
CClientDC myDC(this);
CDC memDC;
CBitmap bitmap;
memDC.CreateCompatibleDC (&myDC);
switch(turn)
{ case 1: bitmap.LoadBitmap (IDB_FLY1);
break;
case 2: bitmap.LoadBitmap (IDB_FLY2);
break;
}
memDC.SelectObject (bitmap);
myDC.Rectangle(rect);
myDC.BitBlt (pos.x,pos.y,77,77,&memDC,0,0,SRCCOPY);
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CButterflyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CButterflyDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg dlg;
dlg.DoModal ();
}
void CButterflyDlg::OnMyPlay()
{
// TODO: Add your control notification handler code here
SetTimer(1,50,NULL);
}
void CButterflyDlg::OnMyFast()
{
// TODO: Add your control notification handler code here
InvalidateRect(rect);
}
void CButterflyDlg::OnMySlow()
{
// TODO: Add your control notification handler code here
speed-=3;
// InvalidateRect(bflyRect.OffsetRect (pos));
InvalidateRect(rect);
}
void CButterflyDlg::OnEnd()
{
// TODO: Add your control notification handler code here
KillTimer(1);
}
void CButterflyDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
pos.x =pos.x +speed;
pos.y =pos.y;
if(pos.x +77>rect.right||
pos.x<rect.left||
pos.y<rect.top||
pos.y+70>rect.bottom)
{if(speed>0)
pos.x=rect.left;
else
pos.x=rect.right-80;
pos.y=rect.top+rect.Height ()/2-35;
}
switch(turn)
{ case 1: turn=2; break;
case 2: turn=1; break;
}
InvalidateRect(rect);
CDialog::OnTimer(nIDEvent);
}
void CButterflyDlg::OnMyBack()
{
// TODO: Add your control notification handler code here
speed=-30;
//InvalidateRect(bflyRect.OffsetRect (pos));
InvalidateRect(rect);
}
#1
up!
#2
up!
#3
up!
#4
windows显示图形是画到一个dc(设备)上,dc有多种显示器就是一种.我们平时画图是直接画到屏幕上,我们经常画不止一次,比如画100条直线就有一百个向屏幕上画的过程,于是就产生了闪烁.
双缓冲就是先把这100直线画到"内存dc"中,在一次性的拷贝到"屏幕dc"上就消除了闪烁.
显示bmp图像的方法就是一种最简单的双缓冲技术应用。
CPaintDC dc(this);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
CDC memDC;
memDC.Create //这个函数名太长记不住了
memDC.SelectObject(&bmp); //把bmp图象画到内存dc上
dc.BitBlt(...,&memDC,...SRCCOPY);//把memDC拷贝到屏幕dc上
CDC
双缓冲就是先把这100直线画到"内存dc"中,在一次性的拷贝到"屏幕dc"上就消除了闪烁.
显示bmp图像的方法就是一种最简单的双缓冲技术应用。
CPaintDC dc(this);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
CDC memDC;
memDC.Create //这个函数名太长记不住了
memDC.SelectObject(&bmp); //把bmp图象画到内存dc上
dc.BitBlt(...,&memDC,...SRCCOPY);//把memDC拷贝到屏幕dc上
CDC
#5
thx you, but how about multimedia communication??????
#6
我给你一个很好的例子!!
给我你的E-MAIL
给我你的E-MAIL
#7
gzup
#8
Robert2001:
给我一个很好的例子!!
cvip11@yahoo.com
给我一个很好的例子!!
cvip11@yahoo.com
#9
我现在就在搞这个缓冲技术,过上一两天,大概就会有结果了,到时候可以有一个现成的例子了.要吗?
#10
我正在做这个;用的是上面的思路,可惜出来的是花屏,怎么会事呢?谁会阿!!急阿!sly582@sohu.com
#11
我也要,sly582可以给我吗?(arcill@21cn.com),多谢!
#12
给各例子看看! 直接编译可以运行!
#include "stdafx.h"
#include "Butterfly.h"
#include "ButterflyDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButterflyDlg dialog
CButterflyDlg::CButterflyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CButterflyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CButterflyDlg)
// 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);
turn=2;
speed=2;
rect.top=30;rect.left =10;
rect.right=350;rect.bottom =300;
bflyRect.left =bflyRect.top =0;
bflyRect.right =bflyRect.bottom =77;
}
void CButterflyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CButterflyDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CButterflyDlg, CDialog)
//{{AFX_MSG_MAP(CButterflyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_ABOUT, OnAbout)
ON_BN_CLICKED(ID_MY_PLAY, OnMyPlay)
ON_BN_CLICKED(ID_MY_FAST, OnMyFast)
ON_BN_CLICKED(ID_MY_SLOW, OnMySlow)
ON_BN_CLICKED(ID_END, OnEnd)
ON_WM_TIMER()
ON_BN_CLICKED(ID_MY_BACK, OnMyBack)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButterflyDlg message handlers
BOOL CButterflyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CButterflyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CButterflyDlg::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
{
CDialog::OnPaint();
}
CClientDC myDC(this);
CDC memDC;
CBitmap bitmap;
memDC.CreateCompatibleDC (&myDC);
switch(turn)
{ case 1: bitmap.LoadBitmap (IDB_FLY1);
break;
case 2: bitmap.LoadBitmap (IDB_FLY2);
break;
}
memDC.SelectObject (bitmap);
myDC.Rectangle(rect);
myDC.BitBlt (pos.x,pos.y,77,77,&memDC,0,0,SRCCOPY);
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CButterflyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CButterflyDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg dlg;
dlg.DoModal ();
}
void CButterflyDlg::OnMyPlay()
{
// TODO: Add your control notification handler code here
SetTimer(1,50,NULL);
}
void CButterflyDlg::OnMyFast()
{
// TODO: Add your control notification handler code here
InvalidateRect(rect);
}
void CButterflyDlg::OnMySlow()
{
// TODO: Add your control notification handler code here
speed-=3;
// InvalidateRect(bflyRect.OffsetRect (pos));
InvalidateRect(rect);
}
void CButterflyDlg::OnEnd()
{
// TODO: Add your control notification handler code here
KillTimer(1);
}
void CButterflyDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
pos.x =pos.x +speed;
pos.y =pos.y;
if(pos.x +77>rect.right||
pos.x<rect.left||
pos.y<rect.top||
pos.y+70>rect.bottom)
{if(speed>0)
pos.x=rect.left;
else
pos.x=rect.right-80;
pos.y=rect.top+rect.Height ()/2-35;
}
switch(turn)
{ case 1: turn=2; break;
case 2: turn=1; break;
}
InvalidateRect(rect);
CDialog::OnTimer(nIDEvent);
}
void CButterflyDlg::OnMyBack()
{
// TODO: Add your control notification handler code here
speed=-30;
//InvalidateRect(bflyRect.OffsetRect (pos));
InvalidateRect(rect);
}
#include "stdafx.h"
#include "Butterfly.h"
#include "ButterflyDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButterflyDlg dialog
CButterflyDlg::CButterflyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CButterflyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CButterflyDlg)
// 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);
turn=2;
speed=2;
rect.top=30;rect.left =10;
rect.right=350;rect.bottom =300;
bflyRect.left =bflyRect.top =0;
bflyRect.right =bflyRect.bottom =77;
}
void CButterflyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CButterflyDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CButterflyDlg, CDialog)
//{{AFX_MSG_MAP(CButterflyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_ABOUT, OnAbout)
ON_BN_CLICKED(ID_MY_PLAY, OnMyPlay)
ON_BN_CLICKED(ID_MY_FAST, OnMyFast)
ON_BN_CLICKED(ID_MY_SLOW, OnMySlow)
ON_BN_CLICKED(ID_END, OnEnd)
ON_WM_TIMER()
ON_BN_CLICKED(ID_MY_BACK, OnMyBack)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButterflyDlg message handlers
BOOL CButterflyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CButterflyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CButterflyDlg::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
{
CDialog::OnPaint();
}
CClientDC myDC(this);
CDC memDC;
CBitmap bitmap;
memDC.CreateCompatibleDC (&myDC);
switch(turn)
{ case 1: bitmap.LoadBitmap (IDB_FLY1);
break;
case 2: bitmap.LoadBitmap (IDB_FLY2);
break;
}
memDC.SelectObject (bitmap);
myDC.Rectangle(rect);
myDC.BitBlt (pos.x,pos.y,77,77,&memDC,0,0,SRCCOPY);
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CButterflyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CButterflyDlg::OnAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg dlg;
dlg.DoModal ();
}
void CButterflyDlg::OnMyPlay()
{
// TODO: Add your control notification handler code here
SetTimer(1,50,NULL);
}
void CButterflyDlg::OnMyFast()
{
// TODO: Add your control notification handler code here
InvalidateRect(rect);
}
void CButterflyDlg::OnMySlow()
{
// TODO: Add your control notification handler code here
speed-=3;
// InvalidateRect(bflyRect.OffsetRect (pos));
InvalidateRect(rect);
}
void CButterflyDlg::OnEnd()
{
// TODO: Add your control notification handler code here
KillTimer(1);
}
void CButterflyDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
pos.x =pos.x +speed;
pos.y =pos.y;
if(pos.x +77>rect.right||
pos.x<rect.left||
pos.y<rect.top||
pos.y+70>rect.bottom)
{if(speed>0)
pos.x=rect.left;
else
pos.x=rect.right-80;
pos.y=rect.top+rect.Height ()/2-35;
}
switch(turn)
{ case 1: turn=2; break;
case 2: turn=1; break;
}
InvalidateRect(rect);
CDialog::OnTimer(nIDEvent);
}
void CButterflyDlg::OnMyBack()
{
// TODO: Add your control notification handler code here
speed=-30;
//InvalidateRect(bflyRect.OffsetRect (pos));
InvalidateRect(rect);
}