MFC按键控制

来源:互联网 发布:qq加人软件 编辑:程序博客网 时间:2024/06/01 08:39

实现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 Dataenum { 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 dialogSetIcon(m_hIcon, TRUE);// Set big iconSetIcon(m_hIcon, FALSE);// Set small icon// TODO: Add extra initialization hereSIcon=(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 paintingSendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// Center icon in client rectangleint 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 icondc.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<HCURSOR>(m_hIcon);}void CMoveDlg::OnBnClickedBup(){// TODO: Add your control notification handler code hereCRect 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 hereCRect 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 hereCRect 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 hereCRect 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);}

结果图:





0 0