博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MFC按键控制
阅读量:4030 次
发布时间:2019-05-24

本文共 6641 字,大约阅读时间需要 22 分钟。

实现MFC下按键控制;参考:http://blog.csdn.net/mjk1133/article/details/6704694  http://blog.csdn.net/iamoyjj/article/details/6612584

工程中CPP代码

// MoveDlg.cpp : implementation file//#include "stdafx.h"#include "Move.h"#include "MoveDlg.h"#include "afxdialogex.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogEx{public:	CAboutDlg();// Dialog Data	enum { IDD = IDD_ABOUTBOX };	protected:	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support// Implementationprotected:	DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){}void CAboutDlg::DoDataExchange(CDataExchange* pDX){	CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()// CMoveDlg dialogCMoveDlg::CMoveDlg(CWnd* pParent /*=NULL*/)	: CDialogEx(CMoveDlg::IDD, pParent)	, SIcon(NULL){	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CMoveDlg::DoDataExchange(CDataExchange* pDX){	CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CMoveDlg, CDialogEx)	ON_WM_SYSCOMMAND()	ON_WM_PAINT()	ON_WM_QUERYDRAGICON()	ON_BN_CLICKED(IDC_BUP, &CMoveDlg::OnBnClickedBup)	ON_BN_CLICKED(IDC_BDOWN, &CMoveDlg::OnBnClickedBdown)	ON_BN_CLICKED(IDC_BLEFT, &CMoveDlg::OnBnClickedBleft)	ON_BN_CLICKED(IDC_BRIGHT, &CMoveDlg::OnBnClickedBright)END_MESSAGE_MAP()// CMoveDlg message handlersBOOL CMoveDlg::OnInitDialog(){	CDialogEx::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)	{		BOOL bNameValid;		CString strAboutMenu;		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);		ASSERT(bNameValid);		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	SIcon=(CStatic*)GetDlgItem(IDC_STATIC1);	SIcon->ModifyStyle(0,SS_ICON);	SIcon->SetIcon(AfxGetApp()->LoadIconW(IDI_ICON1));	return TRUE;  // return TRUE  unless you set the focus to a control}void CMoveDlg::OnSysCommand(UINT nID, LPARAM lParam){	if ((nID & 0xFFF0) == IDM_ABOUTBOX)	{		CAboutDlg dlgAbout;		dlgAbout.DoModal();	}	else	{		CDialogEx::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 CMoveDlg::OnPaint(){	if (IsIconic())	{		CPaintDC dc(this); // device context for painting		SendMessage(WM_ICONERASEBKGND, reinterpret_cast
(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 { CDialogEx::OnPaint(); }}// The system calls this function to obtain the cursor to display while the user drags// the minimized window.HCURSOR CMoveDlg::OnQueryDragIcon(){ return static_cast
(m_hIcon);}void CMoveDlg::OnBnClickedBup(){ // TODO: Add your control notification handler code here CRect rect;//获取 静态图片 位置 GetDlgItem(IDC_STATIC1)->GetWindowRect(&rect);//获得空间的绝对坐标 ScreenToClient(&rect);//获得相对于主窗体的坐标 rect.OffsetRect(CSize(0,-5));//这里要是要移动的相对位置 CRect rect2;//获取运动空间坐标位置 GetDlgItem(IDC_STATIC2)->GetWindowRect(&rect2); ScreenToClient(&rect2); if (rect.top<=rect2.top) { AfxMessageBox(_T("请注意,已经超出运动范围!!!")); return; } GetDlgItem(IDC_STATIC1)->MoveWindow(rect);//移动到目标位置 }void CMoveDlg::OnBnClickedBdown(){ // TODO: Add your control notification handler code here CRect rect;//获取 静态图片 位置 GetDlgItem(IDC_STATIC1)->GetWindowRect(&rect);//获得空间的绝对坐标 ScreenToClient(&rect);//获得相对于主窗体的坐标 rect.OffsetRect(CSize(0,5));//这里要是要移动的相对位置 CRect rect2;//获取运动空间坐标位置 GetDlgItem(IDC_STATIC2)->GetWindowRect(&rect2); ScreenToClient(&rect2); if (rect.bottom>=rect2.bottom) { AfxMessageBox(_T("请注意,已经超出运动范围!!!")); return; } GetDlgItem(IDC_STATIC1)->MoveWindow(rect);//移动到目标位置 }void CMoveDlg::OnBnClickedBleft(){ // TODO: Add your control notification handler code here CRect rect;//获取 静态图片 位置 GetDlgItem(IDC_STATIC1)->GetWindowRect(&rect);//获得空间的绝对坐标 ScreenToClient(&rect);//获得相对于主窗体的坐标 rect.OffsetRect(CSize(-5,0));//这里要是要移动的相对位置 CRect rect2;//获取运动空间坐标位置 GetDlgItem(IDC_STATIC2)->GetWindowRect(&rect2); ScreenToClient(&rect2); if (rect.left<=rect2.left) { AfxMessageBox(_T("请注意,已经超出运动范围!!!")); return; } GetDlgItem(IDC_STATIC1)->MoveWindow(rect);//移动到目标位置 }void CMoveDlg::OnBnClickedBright(){ // TODO: Add your control notification handler code here CRect rect;//获取 静态图片 位置 GetDlgItem(IDC_STATIC1)->GetWindowRect(&rect);//获得空间的绝对坐标 ScreenToClient(&rect);//获得相对于主窗体的坐标 rect.OffsetRect(CSize(5,0));//这里要是要移动的相对位置 CRect rect2;//获取运动空间坐标位置 GetDlgItem(IDC_STATIC2)->GetWindowRect(&rect2); ScreenToClient(&rect2); if (rect.right>=rect2.right) { AfxMessageBox(_T("请注意,已经超出运动范围!!!")); return; } GetDlgItem(IDC_STATIC1)->MoveWindow(rect);//移动到目标位置 }//通过重载虚函数PreTranslateMessage()对所关心的消息进行截取与响应:BOOL CMoveDlg::PreTranslateMessage(MSG* pMsg){ // TODO: Add your specialized code here and/or call the base class //判断是否为键盘消息 if (WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST) { //判断是否按下键盘Enter键 switch (pMsg->wParam) { case VK_UP: { if (GetAsyncKeyState(VK_LEFT)) { OnBnClickedBleft();//按组合键 左上 } else if(GetAsyncKeyState(VK_RIGHT)) { OnBnClickedBright();//按组合键 右下 } OnBnClickedBup(); } break; case VK_DOWN: { if (GetAsyncKeyState(VK_LEFT)) { OnBnClickedBleft();//按组合键 左上 } else if(GetAsyncKeyState(VK_RIGHT)) { OnBnClickedBright();//按组合键 右下 } OnBnClickedBdown(); } break; case VK_LEFT: { if (GetAsyncKeyState(VK_UP)) { OnBnClickedBup();//按组合键 左上 } else if(GetAsyncKeyState(VK_DOWN)) { OnBnClickedBdown();//按组合键 右下 } OnBnClickedBleft(); } break; case VK_RIGHT: { if (GetAsyncKeyState(VK_UP)) { OnBnClickedBup();//按组合键 左上 } else if(GetAsyncKeyState(VK_DOWN)) { OnBnClickedBdown();//按组合键 右下 } OnBnClickedBright(); } break; default: AfxMessageBox(_T("请按方向键进行控制!")); break; } } return CDialogEx::PreTranslateMessage(pMsg);}
结果图:

你可能感兴趣的文章
ubuntu下SVN服务器安装配置
查看>>
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>
[Mac]Mac 操作系统 常见技巧
查看>>
苹果Swift编程语言入门教程【中文版】
查看>>
捕鱼忍者(ninja fishing)之游戏指南+游戏攻略+游戏体验
查看>>
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
部分笔试算法题整理
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>