📄 obexsquirt.cpp
字号:
//======================================================================
// ObexSqurt - A simple Obex 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 <obex.h>
#include <Msgqueue.h>
#if defined(WIN32_PLATFORM_PSPC)
#include <aygshell.h> // Add Pocket PC includes
#pragma comment( lib, "aygshell" ) // Link Pocket PC lib for menubar
#endif
#include "obexsquirt.h" // Program-specific stuff
#include "MyObexSink.cpp" // IObexSink class
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("obexsquirt");
const TCHAR szTitleText[] = TEXT ("OBEX Squirt");
TCHAR szTitle[128];
HINSTANCE hInst; // Program instance handle
HWND hMain; // Main window handle
BOOL fFirstSize = TRUE; // First WM_SIZE flag
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300)
SHACTIVATEINFO sai; // Needed for PPC helper funcs
#endif
// The GUID strings below are defined numerically in bt_sdp.h
const TCHAR g_szIrMCSyncGuid[] =
TEXT("{00001104-0000-1000-8000-00805f9b34fb}");
const TCHAR g_szObjPushGuid[] =
TEXT("{00001105-0000-1000-8000-00805f9b34fb}");
const TCHAR g_szFtpGuid[] =
TEXT("{00001106-0000-1000-8000-00805f9b34fb}");
const TCHAR g_szTransIrDA[] =
TEXT("{30a7bc02-59b6-40bb-aa2b-89eb49ef274e}");
const TCHAR g_szTransBth[] =
TEXT("{30a7bc03-59b6-40bb-aa2b-89eb49ef274e}");
// {F9ec7bc4-953c-11d2-984e-525400dc9e09}
GUID CLSID_FileExchange_NetOrder = {0xc47becf9, 0x3c95, 0xd211,
{0x98, 0x4e, 0x52, 0x54, 0x00, 0xdc, 0x9e, 0x09}};
HANDLE hQRead = 0; // Msg queues are used to sync
HANDLE hQWrite = 0; // output to the listbox
CRITICAL_SECTION csPrintf;
CRITICAL_SECTION csLock;
IObex *pObex = NULL; // Obex main interface ptr
BOOL fObex2IF = FALSE;
IConnectionPointContainer *pContainer = NULL;
IConnectionPoint *pConPt = NULL;
DWORD dwCookie;
MYOBEXDEVICEINFO obDevs[MAX_DEVS];
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_SIZE, DoSizeMain,
WM_COMMAND, DoCommandMain,
MYMSG_OBEXEVENT, DoObexEventMain,
MYMSG_PRINTF, DoPrintfNotifyMain,
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,
IDD_DEVICES, DoMainCommandDevList,
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPCMDLINE lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
// Initialize this instance.
hMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hMain) {
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
if ((hMain == 0) || !IsDialogMessage (hMain, &msg)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPCMDLINE lpCmdLine,
int nCmdShow){
WNDCLASS wc;
HWND hWnd;
hInst = hInstance; // Save program instance handle.
// For all systesms, if previous instance, activate it instead of us.
hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)((DWORD)hWnd | 0x01));
return 0;
}
// Init COM
CoInitializeEx (NULL, COINIT_MULTITHREADED);
// Init device structure
memset (&obDevs, 0, sizeof (obDevs));
// Create message queues for async string out to listbox
MSGQUEUEOPTIONS mqo;
mqo.dwSize = sizeof (mqo);
mqo.dwFlags = MSGQUEUE_ALLOW_BROKEN;
mqo.dwMaxMessages = 16;
mqo.cbMaxMessage = 512;
mqo.bReadAccess = TRUE;
hQRead = CreateMsgQueue (TEXT ("MSGQUEUE\\ThTead"), &mqo);
mqo.bReadAccess = FALSE;
hQWrite = CreateMsgQueue (TEXT ("MSGQUEUE\\ThTead"), &mqo);
// 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 (HWND)0;
// Create main window.
hWnd = CreateDialog (hInst, szAppName, NULL, NULL);
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Init obex
if (!InitObex (hWnd)) {
return 0;
}
ShowWindow (hWnd, nCmdShow); // Standard show and update calls
UpdateWindow (hWnd);
SetFocus (GetDlgItem (hWnd, IDD_OUTTEXT));
return hWnd;
}
//----------------------------------------------------------------------
// ResetDevList - Clean up the list of devices
//
int ResetDevList (void) {
int i;
// Clean up property bags
for (i = 0; i < dim (obDevs); i++) {
if (obDevs[i].pDevBag) {
obDevs[i].pDevBag->Release();
obDevs[i].pDevBag = 0;
}
}
return 0;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
if (pObex) {
pObex->StopDeviceEnum();
ResetDevList ();
if (pConPt) {
pConPt->Unadvise(dwCookie);
Sleep(100);
pConPt->Release();
}
if (pContainer) {
pContainer->Release();
}
pObex->Shutdown();
pObex->Release();
}
CoUninitialize();
return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//----------------------------------------------------------------------
// 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) && (_WIN32_WCE >= 300)
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 Pocket PC, make dialog box full screen with PPC
// specific call.
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) && (_WIN32_WCE >= 300)
static RECT rectListbox;
RECT rect;
GetClientRect (hWnd, &rect);
if (fFirstSize) {
// First time through, get the position of the listbox for
// resizeing later. Store the distance from the sides of
// the listbox control to the side of the parent window
if (IsWindow (GetDlgItem (hWnd, IDD_INTEXT))) {
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
if (fFirstSize) {
EnableWindow (GetDlgItem (hWnd, IDD_SENDFILE), FALSE);
int i = 40;
SendDlgItemMessage (hWnd, IDD_DEVICES, LB_SETTABSTOPS, 1,
(LPARAM)&i);
fFirstSize = FALSE;
}
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -