📄 fortune3b.cpp
字号:
// Get needed include files
#define INITGUID
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include "Fortune3.h"
#include "Utility.h"
// Support dynamic creation
IMPLEMENT_DYNCREATE(ComFortuneTeller, CCmdTarget)
// Support a class factory
IMPLEMENT_OLECREATE(ComFortuneTeller, "FortuneTeller3",
0x28200682, 0x35b6, 0x11d0, 0x8b, 0xbd, 0xfa, 0x6e, 0xaa, 0x2d, 0x62, 0xd);
// Map the various interfaces onto the nested interface classes
BEGIN_INTERFACE_MAP(ComFortuneTeller, CCmdTarget)
INTERFACE_PART(ComFortuneTeller, IID_IFortuneTeller, CInnerFortune)
INTERFACE_PART(ComFortuneTeller, IID_IQuotation, CInnerQuote)
END_INTERFACE_MAP()
// Our list of fortunes
static CString g_astrFortunes[] =
{
"You are strong in body and spirit.",
"You are strong in perspiration and odor.",
"Good fortune smiles upon the distributed.",
"VB Programmer for Hire -- Cheap!!",
"I never met an object I didn't like.",
"Edit, Compile, Link, Crash, Sigh.",
"Never judge a stack dump by its arguments.",
"Card carrying member of the Bug Police.",
"You make Dilbert look charming and sophisticated.",
"BMW, The Ultimate Driving Machine."
};
// Constant that tracks the number of fortunes we know about
static const ULONG g_ulNumFortunes =
sizeof(g_astrFortunes) / sizeof(CString);
// Our list of quotations
struct CQuoteTuple
{
// Ctor
CQuoteTuple(CString strNewQuote, CString strNewAuthor)
: m_strQuotation(strNewQuote),
m_strAuthor(strNewAuthor)
{ }
// Member data
CString m_strQuotation;
CString m_strAuthor;
};
static CQuoteTuple g_aQuotations[] =
{
{ CQuoteTuple("Most writers steal a good thing when they can,\n"
" And when 'tis safely got 'tis worth the winning.\n"
" The worst of 't is we now and then detect 'em,\n"
" Before they ever dream that we suspect 'em.",
"Bryan Waller Proctor") },
{ CQuoteTuple("Husbands never become good;\n"
" they merely become proficient.",
"H. L. Mencken") },
{ CQuoteTuple("Americans may have no identity,\n"
" but they do have wonderful teeth.",
"Jean Baudrillard") },
{ CQuoteTuple("Justice ... limps along,\n"
" but it gets there all the same.",
"Gabriel Garc韆 M醨quez") },
{ CQuoteTuple("In politics if you want anything said, ask a man.\n"
" If you want anything done, ask a woman.",
"Margaret Thatcher") },
{ CQuoteTuple("This sucks!",
"Chris Corry") }
};
// Constant that tracks the number of fortunes we know about
static const ULONG g_ulNumQuotations =
sizeof(g_aQuotations) / sizeof(CQuoteTuple);
//
// Constructor and destructor
//
ComFortuneTeller::ComFortuneTeller()
{
VerboseMsg("In FortuneTeller constructor.\n");
// Seed the random number generator
struct _timeb timebuffer;
_ftime(&timebuffer);
srand(timebuffer.millitm);
AfxOleLockApp();
}
ComFortuneTeller::~ComFortuneTeller()
{
VerboseMsg("In ComFortuneTeller destructor.\n");
AfxOleUnlockApp();
}
//
// Nested FortuneTeller class
//
IMPLEMENT_NESTED_IUNKNOWN(ComFortuneTeller, CInnerFortune)
STDMETHODIMP
ComFortuneTeller::XCInnerFortune::GetFortune(PBSTR pbstrFortune)
{
METHOD_PROLOGUE(ComFortuneTeller, CInnerFortune)
VerboseMsg("In GetFortune.\n");
// Initialize the output string
*pbstrFortune = NULL;
// Create a BSTR out of the fortune
*pbstrFortune = g_astrFortunes[rand() % g_ulNumFortunes].AllocSysString();
return NOERROR;
}
//
// Nested Quotation class
//
IMPLEMENT_NESTED_IUNKNOWN(ComFortuneTeller, CInnerQuote)
STDMETHODIMP
ComFortuneTeller::XCInnerQuote::GetQuotation(PBSTR pbstrQuotation,
PBSTR pbstrAuthor)
{
METHOD_PROLOGUE(ComFortuneTeller, CInnerQuote)
VerboseMsg("In GetQuotation.\n");
// Initialize the output strings
*pbstrQuotation = NULL;
*pbstrAuthor = NULL;
// Select our quote
CQuoteTuple *pQuote = &g_aQuotations[rand() % g_ulNumQuotations];
// Create BSTRs out of the quotation and author strings
*pbstrQuotation = pQuote->m_strQuotation.AllocSysString();
*pbstrAuthor = pQuote->m_strAuthor.AllocSysString();
return NOERROR;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -