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

📄 mysquirt.cpp

📁 《Windows CE 6.0开发者参考》(《Programming Windows Embedded CE 6.0 Developer Reference》)第四版书中的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//======================================================================
// MySquirt - A simple IrSock application for Windows CE
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#ifndef _WIN32_WCE
#define _WIN32_WINNT 0x500
#endif
#include <windows.h>                 // For all that Windows stuff
#include <stdlib.h>
#include <stdio.h>
#include <af_irda.h>                 // IrDA includes
#include <winsock.h>                 // Socket includes
#include "MySquirt.h"                // Program-specific stuff
#ifndef _WIN32_WCE
#include <process.h>                 // Desktop multithread includes
#include <tchar.h>
#endif
#if defined(WIN32_PLATFORM_PSPC)
#include <aygshell.h>                // Add Pocket PC includes.
#pragma comment( lib, "aygshell" )   // Link Pocket PC lib for menu bar.
#endif

#ifdef _WIN32_WCE
#pragma comment( lib, "Winsock.lib" )   // Winsock lib for CE
#else
#pragma comment( lib, "Ws2_32.lib" )    // Winsock lib for desktop
#endif
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("MySquirt");
const char chzAppName[] = "MySquirt";
HINSTANCE hInst;                     // Program instance handle
HWND hMain;                          // Main window handle
BOOL fContinue = TRUE;               // Server thread continue flag
BOOL fFirstSize = TRUE;              // First WM_SIZE flag
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
SHACTIVATEINFO sai;                  // Needed for P/PC helper functions
#endif
wchar_t bob;
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
    WM_CREATE, DoCreateMain,
    WM_SIZE, DoSizeMain,
    WM_COMMAND, DoCommandMain,
    WM_SETTINGCHANGE, DoPocketPCShell,
    WM_ACTIVATE, DoPocketPCShell,
    WM_DESTROY, DoDestroyMain,
};
// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
    IDOK, DoMainCommandExit,
#else
    IDOK, DoMainCommandSend,
#endif
    IDCANCEL, DoMainCommandExit,
    IDD_SENDFILE, DoMainCommandSend,
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPCMDLINE lpCmdLine, int nCmdShow) {
    MSG msg;
    int rc = 0;

    // Initialize application.
    hMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
    if (hMain == 0)
        return TermInstance (hInstance, 0x10);

    // Application message loop
    while (GetMessage (&msg, NULL, 0, 0)) {
        if ((hMain == 0) || !IsDialogMessage (hMain, &msg)) {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }
    // Instance cleanup
    return TermInstance (hInstance, (int)msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPCMDLINE lpCmdLine, 
                   int nCmdShow){
    HWND hWnd;
    HANDLE hThread;
    WNDCLASS wc;
    WSADATA wsaData;
    int rc;

    hInst = hInstance;                // Save program instance handle.

    // For all systems, if previous instance exists, activate it instead     
    // of starting a new one.
    hWnd = FindWindow (szAppName, NULL);
    if (hWnd) {
        SetForegroundWindow ((HWND)((DWORD)hWnd | 0x01));
        return 0;
    }
    // Init Winsock
    rc = WSAStartup (1, &wsaData);
    if (rc) {
        MessageBox (NULL, TEXT("Error in WSAStartup"), 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 = LoadCursor (NULL, IDC_ARROW);// Default cursor
    wc.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH);
    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 0 if window not created.
    if (!IsWindow (hWnd)) return 0;

    // Create secondary threads for interprocess communication.
    hThread = MyCreateThread (NULL, 0, MonitorThread, hWnd, 0, 0);
    if (hThread == 0) {
        DestroyWindow (hWnd);
        return 0;
    }
    CloseHandle (hThread);

    ShowWindow (hWnd, nCmdShow);      // Standard show and update calls
    UpdateWindow (hWnd);
    SetFocus (GetDlgItem (hWnd, IDD_OUTTEXT));
    return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
    return nDefRC;
}
//======================================================================
// Message handling procedures for main window
TCHAR szTitle[128];
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
                              LPARAM lParam) {
    INT i;
    //
    // Search message list to see if we need to handle this
    // message. If in list, call procedure.
    //
    for (i = 0; i < dim(MainMessages); i++) {
        if (wMsg == MainMessages[i].Code)
            return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
    }
    return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                      LPARAM lParam) {

#if defined(WIN32_PLATFORM_PSPC)
    SHINITDLGINFO shidi;
    SHMENUBARINFO mbi;                      // For Pocket PC, create
    memset(&mbi, 0, sizeof(SHMENUBARINFO)); // menu bar so that we
    mbi.cbSize = sizeof(SHMENUBARINFO);     // have a sip button.
    mbi.dwFlags = SHCMBF_EMPTYBAR;
    mbi.hwndParent = hWnd;
    SHCreateMenuBar(&mbi);
    SendMessage(mbi.hwndMB, SHCMBM_GETSUBMENU, 0, 100);

    // For WinMobile, make dialog box full screen.
    shidi.dwMask = SHIDIM_FLAGS;
    shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLG | SHIDIF_SIPDOWN;
    shidi.hDlg = hWnd;
    SHInitDialog(&shidi);

    sai.cbSize = sizeof (sai);
    SHHandleWMSettingChange(hWnd, wParam, lParam, &sai);
#endif
    GetWindowText (hWnd, szTitle, dim (szTitle));
    return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                    LPARAM lParam) {
#if defined(WIN32_PLATFORM_PSPC)
    static RECT rectListbox;
    RECT rect;

    GetClientRect (hWnd, &rect);
    if (fFirstSize) {
        // First time through, get the position of the list box for 
        // resizing later. Store the distance from the sides of 
        // the list box control to the side of the parent window.
        if (IsWindow (GetDlgItem (hWnd, IDD_INTEXT))) {
            fFirstSize = FALSE;
            GetWindowRect (GetDlgItem (hWnd, IDD_INTEXT), &rectListbox);
            MapWindowPoints (HWND_DESKTOP, hWnd, (LPPOINT)&rectListbox, 2);
            rectListbox.right = rect.right - rectListbox.right;
            rectListbox.bottom = rect.bottom - rectListbox.bottom;
        }
    }
    SetWindowPos (GetDlgItem (hWnd, IDD_INTEXT), 0, rect.left + 5, 
                  rectListbox.top, rect.right - 10, 
                  rect.bottom - rectListbox.top - 5,
                  SWP_NOZORDER);
#endif
    return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    WORD idItem, wNotifyCode;
    HWND hwndCtl;
    INT i;

    // Parse the parameters.
    idItem = (WORD) LOWORD (wParam);
    wNotifyCode = (WORD) HIWORD (wParam);
    hwndCtl = (HWND) lParam;

    // Call routine to handle control message.
    for (i = 0; i < dim(MainCommandItems); i++) {
        if (idItem == MainCommandItems[i].Code)
            return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
                                              wNotifyCode);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoPocketPCShell - Process Pocket PC-required messages.
//
LRESULT DoPocketPCShell (HWND hWnd, UINT wMsg, WPARAM wParam,
                         LPARAM lParam) {
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
    if (wMsg == WM_SETTINGCHANGE) 
        return SHHandleWMSettingChange(hWnd, wParam, lParam, &sai);
    if (wMsg == WM_ACTIVATE) 
        return SHHandleWMActivate(hWnd, wParam, lParam, &sai, 0);
#endif 
    return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    fContinue = FALSE;                       // Shut down server thread.
    Sleep (0);                               // Pass on timeslice.
    PostQuitMessage (0);
    return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {

    SendMessage (hWnd, WM_CLOSE, 0, 0);
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandSend - Process Program Send File command.
//
LPARAM DoMainCommandSend (HWND hWnd, WORD idItem, HWND hwndCtl,
                          WORD wNotifyCode) {
    HANDLE hTh;
    static TCHAR szName[MAX_PATH];

    GetDlgItemText (hWnd, IDD_OUTTEXT, szName, dim(szName));
    hTh = MyCreateThread (NULL, 0, SendFileThread, (PVOID)szName, 0, 
                          NULL);
    CloseHandle (hTh);
    return 0;
}
//----------------------------------------------------------------------
// Add2List - Add string to the report list box.
//
void Add2List (HWND hWnd, LPTSTR lpszFormat, ...) {
    int i, nBuf;
    wchar_t szBuffer[512];

    va_list args;
    va_start(args, lpszFormat);

    nBuf = StringCchVPrintf(szBuffer, dim (szBuffer), 
                            (wchar_t *)lpszFormat, args);

    i = (int)SendDlgItemMessage (hWnd, IDD_INTEXT, LB_ADDSTRING, 0,
                            (LPARAM)(LPCTSTR)szBuffer);
    if (i != LB_ERR)
        SendDlgItemMessage (hWnd, IDD_INTEXT, LB_SETTOPINDEX, i,
                            (LPARAM)(LPCTSTR)szBuffer);
    va_end(args);
}
//----------------------------------------------------------------------
// MySetWindowText - Set window title to passed printf style string.
//
void MySetWindowText (HWND hWnd, LPTSTR lpszFormat, ...) {
    int nBuf;
    wchar_t szBuffer[512];

⌨️ 快捷键说明

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