📄 mainwin.cpp
字号:
#include "MainWin.h"
#include "Utility.h"
BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_KEYUP()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
: m_pISimpleDrawable(NULL),
m_pIRobot(NULL),
m_pINoise(NULL)
{
Create(NULL, "NoisyRobot Client Program");
}
CMainWindow::~CMainWindow()
{
// Release COM interfaces
if (m_pISimpleDrawable) {
m_pISimpleDrawable->Release();
m_pISimpleDrawable = NULL;
}
if (m_pIRobot) {
m_pIRobot->Release();
m_pIRobot = NULL;
}
if (m_pINoise) {
m_pINoise->Release();
m_pINoise = NULL;
}
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
if (m_pISimpleDrawable)
m_pISimpleDrawable->Draw(dc);
}
int CMainWindow::OnCreate(LPCREATESTRUCT lpcs)
{
HRESULT hResult;
if (CFrameWnd::OnCreate(lpcs) == -1)
return -1;
// Create a new NoisyRobot object and return an IRobot
// interface pointer
VerboseMsg("Creating new NoisyRobot object instance.\n");
hResult = CoCreateInstance(CLSID_NoisyRobot,
NULL, CLSCTX_INPROC_SERVER,
IID_IRobot,
(PPVOID) &m_pIRobot);
if (FAILED(hResult)) {
ReportError("Could not create a new NoisyRobot object.", hResult);
}
else {
try
{
// Initialize the robot
VerboseMsg("Initializing Robot object.\n");
hResult = m_pIRobot->Initialize(150, 150);
if (FAILED(hResult)) {
ReportError("Could not initialize the new Robot object.", hResult);
throw FALSE;
}
// Obtain a drawable interface pointer (we'll hold onto this
// interface pointer for the duration of the view lifetime)
VerboseMsg("Obtaining ISimpleDrawable interface.\n");
hResult = m_pIRobot->QueryInterface(IID_ISimpleDrawable,
(PPVOID) &m_pISimpleDrawable);
if (FAILED(hResult)) {
ReportError("Could not obtain ISimpleDrawable interface.", hResult);
throw FALSE;
}
// Obtain a noise interface pointer (we'll hold onto this
// interface pointer for the duration of the view lifetime)
VerboseMsg("Obtaining INoise interface.\n");
hResult = m_pIRobot->QueryInterface(IID_INoise,
(PPVOID) &m_pINoise);
if (FAILED(hResult)) {
ReportError("Could not obtain INoise interface.", hResult);
throw FALSE;
}
VerboseMsg("Object initialized fine.\n");
}
catch(HRESULT)
{
VerboseMsg("Robot construction failed.\n");
// Clean up
m_pIRobot->Release();
m_pIRobot = NULL;
if (m_pISimpleDrawable) {
m_pISimpleDrawable->Release();
m_pISimpleDrawable = NULL;
}
}
}
return 0;
}
void CMainWindow::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
USHORT usHeading;
switch(nChar) {
case VK_LEFT:
if (m_pIRobot) {
m_pIRobot->GetCurrentHeading(&usHeading);
m_pIRobot->TurnTo(usHeading + 15);
Invalidate(TRUE);
}
break;
case VK_RIGHT:
if (m_pIRobot) {
m_pIRobot->GetCurrentHeading(&usHeading);
if (usHeading < 15)
usHeading = 360 + usHeading;
m_pIRobot->TurnTo(usHeading - 15);
Invalidate(TRUE);
}
break;
case VK_UP:
if (m_pIRobot) {
m_pIRobot->MoveForward(30);
Invalidate(TRUE);
}
break;
case VK_DOWN:
if (m_pIRobot) {
m_pIRobot->MoveBackward(30);
Invalidate(TRUE);
}
break;
case 'N':
if (m_pINoise)
m_pINoise->MakeNoise();
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -