⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cnctnote.cpp

📁 上传的代码是WindowsCEProgramming第一版的源代码
💻 CPP
字号:
//======================================================================
// CnctNote - A simple application for Windows CE
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 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;
    int rc = 0;
    HWND hwndMain;

    // Initialize application.
    rc = InitApp (hInstance);
    if (rc) return rc;

    // Initialize this instance.
    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 TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitApp - Application initialization
//
int InitApp (HINSTANCE hInstance) {
    WNDCLASS wc;

    // 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_WINDOW + 1);
    wc.lpszMenuName =  NULL;                  // Menu name
    wc.lpszClassName = szAppName;             // Window class name

    if (RegisterClass (&wc) == 0) return 1;
    return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow){
    HWND hWnd;
    HRESULT hr;
    INT rc;

    // 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;
    }
    // Create main window.
    hWnd = CreateDialog (hInst, szAppName, NULL, NULL);
    rc = GetLastError();

    // 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 - Return a pointer to interface.
STDMETHODIMP MyDccSink::QueryInterface (REFIID riid, LPVOID * ppvObj) {

    if (IID_IUnknown==riid || IID_IDccManSink==riid)
        *ppvObj = (IDccManSink*)this;
    else {
        *ppvObj = NULL;
        return E_NOINTERFACE;
    }
    AddRef();
    return NO_ERROR;
}
//----------------------------------------------------------------------
//
STDMETHODIMP MyDccSink::OnLogIpAddr (DWORD dwIpAddr) {
    Add2List (hWnd, TEXT ("OnLogIpAddr %08x"), dwIpAddr);
    return NO_ERROR;
}
//----------------------------------------------------------------------
//
STDMETHODIMP MyDccSink::OnLogTerminated () {
    Add2List (hWnd, TEXT ("OnLogTerminated "));
    return NO_ERROR;
}
//----------------------------------------------------------------------
//
STDMETHODIMP MyDccSink::OnLogActive () {
    Add2List (hWnd, TEXT ("OnLogActive "));
    return NO_ERROR;
}
//----------------------------------------------------------------------
//
STDMETHODIMP MyDccSink::OnLogInactive () {
    Add2List (hWnd, TEXT ("OnLogInactive "));
    return NO_ERROR;
}
//----------------------------------------------------------------------
//
STDMETHODIMP MyDccSink::OnLogAnswered () {
    Add2List (hWnd, TEXT ("OnLogAnswered"));
    return NO_ERROR;
}
//----------------------------------------------------------------------
//
STDMETHODIMP MyDccSink::OnLogListen () {
    Add2List (hWnd, TEXT ("OnLogListen "));
    return NO_ERROR;
}
//----------------------------------------------------------------------
//
STDMETHODIMP MyDccSink::OnLogDisconnection () {
    Add2List (hWnd, TEXT ("OnLogDisconnection "));
    return NO_ERROR;
}
//----------------------------------------------------------------------
//
STDMETHODIMP MyDccSink::OnLogError () {
    Add2List (hWnd, TEXT ("OnLogError "));
    return NO_ERROR;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -