?? cnctnote.cpp
字號:
//====================================================================
// CnctNote - A simple application for Windows CE
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//====================================================================
#include <windows.h> // For all that Windows stuff
#include <stdio.h>
#include <initguid.h>
#include "dccole.h"
#include "CnctNote.h" // Program-specific stuff
//-----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("CnctNote");
HINSTANCE hInst; // Program instance handle
BOOL fFirst = TRUE;
IDccMan *pDccMan;
MyDccSink *pMySink; // Notification interface
DWORD g_Context; // Context variable
//====================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
HWND hwndMain;
// Initialize application.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return TermInstance (hInstance, 0x10);
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
if ((hwndMain == 0) || !IsDialogMessage (hwndMain, &msg)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
// Instance cleanup
return (int)TermInstance (hInstance, msg.wParam);
}
//-----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow){
WNDCLASS wc;
HWND hWnd;
HRESULT hr;
// Save program instance handle in global variable.
hInst = hInstance;
// Initialize COM.
hr = CoInitialize(NULL);
if (FAILED(hr)) {
MessageBox (NULL, "CoInitialize failed.", szAppName, MB_OK);
return 0;
}
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = DLGWINDOWEXTRA; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 0;
// Create main window.
hWnd = CreateDialog (hInst, szAppName, NULL, NULL);
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//-----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
// Release COM.
CoUninitialize();
return nDefRC;
}
//====================================================================
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
switch (wMsg) {
case WM_SIZE:
if (fFirst) {
HRESULT hr;
IDccManSink *pdms;
fFirst = FALSE;
// Get a pointer to the IDccMan COM interface.
hr = CoCreateInstance (CLSID_DccMan, NULL, CLSCTX_SERVER,
IID_IDccMan, (LPVOID*)&pDccMan);
if (FAILED(hr)) {
Add2List (hWnd, "CoCreateInstance failed");
break;
}
// Create new notification object.
pMySink = new MyDccSink(hWnd, pDccMan);
pMySink->QueryInterface (IID_IDccManSink, (void **)&pdms);
// Ask to be advised of connect state changes.
pDccMan->Advise (pdms, &g_Context);
}
break;
case WM_COMMAND:
switch (LOWORD (wParam)) {
case IDOK:
case IDCANCEL:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
}
break;
case WM_DESTROY:
// Stop receiving notifications.
pDccMan->Unadvise (g_Context);
// Release the DccMan object.
pDccMan->Release();
PostQuitMessage (0);
break;
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//-----------------------------------------------------------------------
// Add2List - Add string to the report list box.
//
void Add2List (HWND hWnd, LPTSTR lpszFormat, ...) {
int nBuf, i;
TCHAR szBuffer[512];
va_list args;
va_start(args, lpszFormat);
nBuf = vsprintf(szBuffer, lpszFormat, args);
i = SendDlgItemMessage (hWnd, IDC_RPTLIST, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)szBuffer);
if (i != LB_ERR)
SendDlgItemMessage (hWnd, IDC_RPTLIST, LB_SETTOPINDEX, i,
(LPARAM)(LPCTSTR)szBuffer);
va_end(args);
}
//********************************************************************
// Constructor
MyDccSink::MyDccSink (HWND hwndMain, IDccMan *pDccMan) {
m_pDccMan = pDccMan;
hWnd = hwndMain;
m_pDccMan->AddRef();
return;
}
//-----------------------------------------------------------------------
// Destructor
MyDccSink::~MyDccSink () {
m_pDccMan->Release();
return;
}
//-----------------------------------------------------------------------
// AddRef - Increment object ref count.
STDMETHODIMP_(ULONG) MyDccSink::AddRef (THIS) {
return (ULONG)InterlockedIncrement (&m_lRef);
}
//-----------------------------------------------------------------------
// Release - Decrement object ref count.
STDMETHODIMP_(ULONG) MyDccSink::Release (THIS) {
ULONG cnt;
cnt = (ULONG)InterlockedDecrement (&m_lRef);
if (cnt == 0) {
delete this;
return 0;
}
return cnt;
}
//-----------------------------------------------------------------------
// QueryInterface
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -