📄 autocli.cpp
字号:
/*
* AUTOCLI.CPP
* Automation Client Chapter 15
*
* Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Microsoft
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#define INITGUIDS
#include "autocli.h"
/*
* WinMain
*
* Purpose:
* Main entry point of application.
*/
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev
, LPSTR pszCmdLine, int nCmdShow)
{
MSG msg;
PCApp pApp;
SETMESSAGEQUEUE;
pApp=new CApp(hInst, hInstPrev, nCmdShow);
if (NULL==pApp)
return -1;
if (pApp->Init())
{
while (GetMessage(&msg, NULL, 0,0 ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
delete pApp;
return msg.wParam;
}
/*
* AutoClientWndProc
*
* Purpose:
* Window class procedure. Standard callback.
*/
LRESULT APIENTRY AutoClientWndProc(HWND hWnd, UINT iMsg
, WPARAM wParam, LPARAM lParam)
{
PCApp pApp;
WORD wID;
DISPID dispID, dispIDParam;
DISPPARAMS dp;
VARIANTARG va;
EXCEPINFO exInfo;
UINT uErr;
HRESULT hr;
TCHAR szMsg[80];
pApp=(PCApp)GetWindowLong(hWnd, AUTOCLIWL_STRUCTURE);
switch (iMsg)
{
case WM_NCCREATE:
pApp=(PCApp)(((LPCREATESTRUCT)lParam)->lpCreateParams);
SetWindowLong(hWnd, AUTOCLIWL_STRUCTURE, (LONG)pApp);
return (DefWindowProc(hWnd, iMsg, wParam, lParam));
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
wID=LOWORD(wParam);
switch (wID)
{
case IDM_GETSOUND:
//Find the dispID we need
hr=pApp->NameToID(OLETEXT("Sound"), &dispID);
if (FAILED(hr))
break;
//Get the property
SETNOPARAMS(dp);
VariantInit(&va);
hr=pApp->Invoke(dispID, DISPATCH_PROPERTYGET
, &dp, &va, &exInfo, NULL);
if (SUCCEEDED(hr))
{
wsprintf(szMsg, TEXT("Current 'Sound' is 0x%lX")
, va.lVal);
}
else
{
wsprintf(szMsg
, TEXT("Get 'Sound' failed with 0x%lX")
, hr);
}
pApp->Message(szMsg);
break;
case IDM_SETSOUNDDEFAULT:
case IDM_SETSOUNDHAND:
case IDM_SETSOUNDQUESTION:
case IDM_SETSOUNDEXCLAMATION:
case IDM_SETSOUNDASTERISK:
case IDM_SETSOUNDBOGUS:
//Find the dispID we need
hr=pApp->NameToID(OLETEXT("Sound"), &dispID);
if (FAILED(hr))
break;
/*
* Call IDispatch::Invoke passing wID which
* is a sound identifier (IDM_SETSOUNDDEFAULT
* has to set the sound to zero). The "bogus"
* sound should cause an exception.
*/
//Initialize arguments
VariantInit(&va);
va.vt=VT_I4;
va.lVal=(IDM_SETSOUNDDEFAULT==wID)
? 0L : (long)(wID);
/*
* Passing a named DISPID_PROPERTYPUT
* is required when setting properties.
*/
dispIDParam=DISPID_PROPERTYPUT;
SETDISPPARAMS(dp, 1, &va, 1, &dispIDParam);
hr=pApp->Invoke(dispID, DISPATCH_PROPERTYPUT
, &dp, NULL, &exInfo, NULL);
if (SUCCEEDED(hr))
{
/*
* Note that if the automation object can't
* raise an exception, setting the bogus
* value will return success although there
* is no change in the actual property.
*/
lstrcpy(szMsg, TEXT("Set 'Sound' succeeded"));
}
else
{
wsprintf(szMsg
, TEXT("Set 'Sound' failed with 0x%lX"), hr);
}
pApp->Message(szMsg);
break;
case IDM_BEEP:
if (NULL==pApp->m_pIDispatch)
break;
hr=pApp->NameToID(OLETEXT("Beep"), &dispID);
if (FAILED(hr))
break;
SETNOPARAMS(dp);
VariantInit(&va);
hr=pApp->Invoke(dispID, DISPATCH_METHOD, &dp
, &va, &exInfo, &uErr);
/*
* va will have the sound played as the return
* value of the Beep method.
*/
if (SUCCEEDED(hr))
{
wsprintf(szMsg, TEXT("'Beep' played 0x%lX")
, va.lVal);
}
else
{
wsprintf(szMsg
, TEXT("'Beep' failed with 0x%lX"), hr);
}
pApp->Message(szMsg);
break;
case IDM_EXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0L);
break;
}
break;
default:
return (DefWindowProc(hWnd, iMsg, wParam, lParam));
}
return 0L;
}
/*
* CApp::CApp
* CApp::~CApp
*
* Constructor Parameters: (from WinMain)
* hInst HINSTANCE of the application.
* hInstPrev HINSTANCE of a previous instance.
* nCmdShow UINT specifying how to show the app window.
*
*/
CApp::CApp(HINSTANCE hInst, HINSTANCE hInstPrev
, UINT nCmdShow)
{
m_hInst=hInst;
m_hInstPrev=hInstPrev;
m_nCmdShow=nCmdShow;
m_hWnd=NULL;
m_fInitialized=FALSE;
/*
* Get the current LCID to send to the automation object.
* Note that depending on the Beeper installed, this may or
* may not work, especially if you are in a non-english or
* non-German speaking locale.
*/
m_lcid=GetUserDefaultLCID();
m_szHelpDir[0]=(TCHAR)0;
m_pIDispatch=NULL;
return;
}
CApp::~CApp(void)
{
if (NULL!=m_pIDispatch)
{
m_pIDispatch->Release();
m_pIDispatch=NULL;
}
if (IsWindow(m_hWnd))
DestroyWindow(m_hWnd);
if (m_fInitialized)
CoUninitialize();
return;
}
/*
* CApp::Init
*
* Purpose:
* Initializes an CApp object by registering window classes,
* creating the main window, and doing anything else prone to
* failure such as calling CoInitialize. If this function fails
* the caller should insure that the destructor is called.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if successful, FALSE otherwise.
*/
BOOL CApp::Init(void)
{
WNDCLASS wc;
HRESULT hr;
CHECKVER_OLE;
if (FAILED(CoInitialize(NULL)))
return FALSE;
m_fInitialized=TRUE;
if (!m_hInstPrev)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = AutoClientWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = CBWNDEXTRA;
wc.hInstance = m_hInst;
wc.hIcon = LoadIcon(m_hInst, TEXT("Icon"));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.lpszClassName = TEXT("AUTOCLI");
if (!RegisterClass(&wc))
return FALSE;
}
m_hWnd=CreateWindow(TEXT("AUTOCLI")
, TEXT("Automation Object Client"), WS_OVERLAPPEDWINDOW
, 35, 35, 450, 250, NULL, NULL, m_hInst, this);
if (NULL==m_hWnd)
return FALSE;
//Create the beeper object we want to manipulate.
hr=CoCreateInstance(CLSID_Beeper, NULL, CLSCTX_INPROC_SERVER
, IID_IDispatch, (PPVOID)&m_pIDispatch);
if (FAILED(hr))
{
Message(TEXT("Failed to create object--terminating"), MB_OK);
return FALSE;
}
//Try to get the help directory for the object's type information
HelpDirFromCLSID(CLSID_Beeper, m_szHelpDir);
ShowWindow(m_hWnd, m_nCmdShow);
UpdateWindow(m_hWnd);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -