⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 movedlg.cpp

📁 代码Move是一个在屏幕上跑来跑去的小鱼。 代码 倒计时 是一个简单的倒计时控制器
💻 CPP
字号:
// moveDlg.cpp : implementation file
//

#include "stdafx.h"
#include "move.h"
#include "moveDlg.h"
#include "resource.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMoveDlg dialog

CMoveDlg::CMoveDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMoveDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMoveDlg)
		// 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);
}

void CMoveDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMoveDlg)
	DDX_Control(pDX, IDC_PIC, m_pic);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMoveDlg, CDialog)
	ON_WM_CONTEXTMENU()
	//{{AFX_MSG_MAP(CMoveDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_CATCH, OnCatch)
	ON_COMMAND(ID_SLEEP, OnSleep)
	ON_COMMAND(ID_WAKE, OnWake)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMoveDlg message handlers

BOOL CMoveDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	m_points[0].x=0;m_points[0].y=9;
	m_points[1].x=2;m_points[1].y=7;
	m_points[2].x=4;m_points[2].y=7;
	m_points[3].x=4;m_points[3].y=6;
	m_points[4].x=6;m_points[4].y=6;
	m_points[5].x=7;m_points[5].y=5;
	m_points[6].x=9;m_points[6].y=5;
	m_points[7].x=10;m_points[7].y=4;
	m_points[8].x=18;m_points[8].y=4;
	m_points[9].x=21;m_points[9].y=2;
	m_points[10].x=22;m_points[10].y=1;
	m_points[11].x=24;m_points[11].y=1;
	m_points[12].x=24;m_points[12].y=2;
	m_points[13].x=25;m_points[13].y=3;
	m_points[14].x=25;m_points[14].y=8;
	m_points[15].x=27;m_points[15].y=11;
	m_points[16].x=29;m_points[16].y=9;
	m_points[17].x=29;m_points[17].y=8;
	m_points[18].x=30;m_points[18].y=7;
	m_points[19].x=30;m_points[19].y=6;
	m_points[20].x=32;m_points[20].y=5;
	m_points[21].x=32;m_points[21].y=25;
	m_points[22].x=31;m_points[22].y=24;
	m_points[23].x=27;m_points[23].y=20;
	m_points[24].x=26;m_points[24].y=22;
	m_points[25].x=26;m_points[25].y=29;
	m_points[26].x=24;m_points[26].y=28;
	m_points[27].x=23;m_points[27].y=27;
	m_points[28].x=19;m_points[28].y=27;
	m_points[29].x=19;m_points[29].y=29;
	m_points[30].x=19;m_points[30].y=30;
	m_points[31].x=19;m_points[31].y=32;
	m_points[32].x=17;m_points[32].y=32;
	m_points[33].x=14;m_points[33].y=29;
	m_points[34].x=14;m_points[34].y=32;
	m_points[35].x=12;m_points[35].y=31;
	m_points[36].x=9;m_points[36].y=28;
	m_points[37].x=7;m_points[37].y=28;
	m_points[38].x=3;m_points[38].y=24;
	m_points[39].x=3;m_points[39].y=23;
	m_points[40].x=0;m_points[40].y=20;
	m_points[41].x=0;m_points[41].y=19;
	m_points[42].x=2;m_points[42].y=17;
	m_points[43].x=0;m_points[43].y=15;
	GetCursorPos(&m_points[59]);
	m_lb=0;m_command=0;
	m_width=GetSystemMetrics(SM_CXSCREEN);
	m_height=GetSystemMetrics(SM_CYSCREEN);
	SetTimer(1,50,NULL);
	hBmp1=::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP3));
	hBmp2=::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP4));
	SetWindowPos(&wndTopMost,m_points[59].x,m_points[59].y,40,40,SWP_NOSIZE);
	m_rgnWnd.CreatePolygonRgn(m_points,44,ALTERNATE);
	SetWindowRgn((HRGN) m_rgnWnd,TRUE);
	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 CMoveDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMoveDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMoveDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_lb=1;
	SetCapture();
	m_x=point.x;m_y=point.y;
	CDialog::OnLButtonDown(nFlags, point);
}

void CMoveDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(nFlags==MK_LBUTTON)
	{
		GetWindowRect(&m_window);
		m_window.top+=point.y-m_y; m_window.bottom+=point.y-m_y;
		m_window.left+=point.x-m_x; m_window.right+=point.x-m_x;
		MoveWindow(&m_window);
	}
	CDialog::OnMouseMove(nFlags, point);
}

void CMoveDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	KillTimer(1);
	// TODO: Add your message handler code here
	
}

void CMoveDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	int maxstep=10,maxcount=10,maxspeed=6,minspeed=3,w,h;
	static int step=5,dir=0,speed=0,x,y,count;
	count++;
	if(count>=maxcount)
	{
		count=0;
		m_width=GetSystemMetrics(SM_CXSCREEN);
		m_height=GetSystemMetrics(SM_CYSCREEN);
	}

	if(m_lb==0)
	{
		GetWindowRect(&m_window);
		if(m_window.top<=speed) { y=speed;}
		if(m_window.bottom>=m_height-speed) { dir=1;y=-speed;}
		if(m_window.left<=speed) { x=speed;}
		if(m_window.right>=m_width-speed) { x=-speed;}
		if(step>=maxstep)
		{
			step=0;dir=0;
			GetCursorPos(&m_points[59]);
			GetWindowRect(&m_window);
			step=rand()%maxstep;
			speed=rand()%(maxspeed-minspeed)+minspeed;
			if(m_command==1)
			{
				if(m_points[59].x>m_window.right) dir+=8;
				if(m_points[59].x<m_window.left) dir+=4;
				if(m_points[59].y>m_window.bottom) dir+=2;
				if(m_points[59].y<m_window.top) dir+=1;
			}
			if(m_command==0)
			{
				dir=rand()%11;
				if(dir==3) dir=4;  if(dir==7) dir=8;
			}
			if(m_command==2)
			{
				dir=rand()%11;
				if(dir==3) dir=0;  if(dir==7) dir=0;
				speed=rand()%minspeed;
				m_pic.SetBitmap(hBmp2);
			}
			else m_pic.SetBitmap(hBmp1);

		
			switch(dir)
			{
			case 0:x=0;y=0;break;//stay
			case 1:x=0;y=-speed;break;//up
			case 2:x=0;y=speed;break;//down
			case 4:x=-speed;y=0;break;//left
			case 8:x=speed;y=0;break;//right
			case 5:x=-speed;y=-speed; break;//up_left
			case 9:x=speed;y=-speed;break;//up_right
			case 6:x=-speed;y=speed;break;//down_left
			case 10:x=speed;y=speed;break;//down_right
			}
		}
		else 
		{
			step++;
			m_window.top+=y; m_window.bottom+=y;
			m_window.left+=x; m_window.right+=x;
			MoveWindow(&m_window,TRUE);
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CMoveDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_lb=0;
	ReleaseCapture();
	CDialog::OnLButtonUp(nFlags, point);
}

void CMoveDlg::OnContextMenu(CWnd*, CPoint point)
{
	// CG: This block was added by the Pop-up Menu component	{		if (point.x == -1 && point.y == -1){			//keystroke invocation			CRect rect;			GetClientRect(rect);			ClientToScreen(rect);			point = rect.TopLeft();			point.Offset(5, 5);		}		CMenu menu;		VERIFY(menu.LoadMenu(CG_IDR_POPUP_MOVE_DLG));		CMenu* pPopup = menu.GetSubMenu(0);		ASSERT(pPopup != NULL);		CWnd* pWndPopupOwner = this;		while (pWndPopupOwner->GetStyle() & WS_CHILD)			pWndPopupOwner = pWndPopupOwner->GetParent();		pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,			pWndPopupOwner);	}
}

void CMoveDlg::OnCatch() 
{
	// TODO: Add your command handler code here
	m_command=1;
}

void CMoveDlg::OnSleep() 
{
	// TODO: Add your command handler code here
	m_command=2;
}

void CMoveDlg::OnWake() 
{
	// TODO: Add your command handler code here
	m_command=0;
}

void CMoveDlg::OnAppAbout() 
{
	// TODO: Add your command handler code here
	MessageBox("程序设计:Panic。\n 版本:1.0");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -