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

📄 wince_client.cpp

📁 swain-0.5.2.zip的源代码,比较好用,希望大家喜欢.
💻 CPP
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006  Daniel Lindstr鰉 and Daniel Nilsson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA  02110-1301, USA.
*/
// wince_client.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "wince_client.h"
#include "wince_drawing.h"
#include "../common/defines.h"
#include "../common/network.h"

#define CONFIG_FILE "config.txt"
//#define ADDRESS "192.168.0.1"

// Global Variables:
HINSTANCE			g_hInst;			// current instance
HWND				g_hWndMainWindow;		// window handle
TCHAR szTitle[MAX_LOADSTRING];		// title bar text
TCHAR szMainWindowClass[MAX_LOADSTRING];	// main window class name
TCHAR szDrawingWindowClass[MAX_LOADSTRING];	// drawing window class name

char name[NAME_BUF_SIZE], password[PASSWORD_BUF_SIZE], server_address[256];

// Module Variables:
static HWND				g_hWndMainMenuBar;		// menu bar handle


// Forward declarations of functions included in this code module:
static ATOM			MyRegisterClass(HINSTANCE, LPTSTR);
static BOOL			InitInstance(HINSTANCE, int);
static LRESULT CALLBACK	MainWndProc(HWND, UINT, WPARAM, LPARAM);
static INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

bool readConfig(void);


int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	MSG msg;

	if(!readConfig())
		return(FALSE);

	// Perform application initialization:
	if (!InitInstance(hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	HACCEL hAccelTable;
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINCE_CLIENT));


	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	destroyNetwork();
	return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
static ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szMainWindowClass, LPTSTR szDrawingWindowClass)
{
	WNDCLASS wc;
	ATOM ret;

	wc.style         = CS_HREDRAW | CS_VREDRAW;  // | CS_DBLCLKS;
	wc.lpfnWndProc   = MainWndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINCE_CLIENT));
	wc.hCursor       = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = szMainWindowClass;

	ret = RegisterClass(&wc);
	if (!ret) {
		return ret;
	}

	wc.style         = CS_HREDRAW | CS_VREDRAW;  // | CS_DBLCLKS;
	wc.lpfnWndProc   = DrawingWndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINCE_CLIENT));
	wc.hCursor       = 0;
	wc.hbrBackground = NULL;	//(HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = szDrawingWindowClass;

	return RegisterClass(&wc);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;

    g_hInst = hInstance; // Store instance handle in our global variable

    // SHInitExtraControls should be called once during your application's initialization to initialize any
    // of the device specific controls such as CAPEDIT and SIPPREF.
    SHInitExtraControls();

    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_WINCE_CLIENT, szMainWindowClass, MAX_LOADSTRING);
    LoadString(hInstance, IDC_WINCE_DRAWING, szDrawingWindowClass, MAX_LOADSTRING);

    //If it is already running, then focus on the window, and exit
    hWnd = FindWindow(szMainWindowClass, szTitle);	
    if (hWnd) 
    {
        // set focus to foremost child window
        // The "| 0x00000001" is used to bring any owned windows to the foreground and
        // activate them.
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
        return 0;
    } 

	if (!initNetwork(server_address, name, password)){
		return FALSE;
	}

    if (!MyRegisterClass(hInstance, szMainWindowClass, szDrawingWindowClass))
    {
    	return FALSE;
    }

    hWnd = CreateWindow(szMainWindowClass, szTitle, WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }

	g_hWndMainWindow = hWnd;

    // When the main window is created using CW_USEDEFAULT the height of the menubar (if one
    // is created is not taken into account). So we resize the window after creating it
    // if a menubar is present
    if (g_hWndMainMenuBar)
    {
        RECT rc;
        RECT rcMenuBar;

        GetWindowRect(hWnd, &rc);
        GetWindowRect(g_hWndMainMenuBar, &rcMenuBar);
        rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
		
        MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);


    return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
static LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    static SHACTIVATEINFO s_sai;
	
    switch (message) 
    {
        case WM_COMMAND:
            wmId    = LOWORD(wParam); 
            wmEvent = HIWORD(wParam); 
            // Parse the menu selections:
            switch (wmId)
            {
                case IDM_MAIN_MENU_ABOUT:
                    DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
                    break;
                case IDM_MAIN_MENU_EXIT:
                    SendMessage (hWnd, WM_CLOSE, 0, 0);				
                    break;
				case IDM_MAIN_SESSION_PHOTO:
					StartNewSession(connectionhandler, packethandler, server_cid, IMAGE_CAMERA);
					break;
				case IDM_MAIN_SESSION_OPEN:
					StartNewSession(connectionhandler, packethandler, server_cid, IMAGE_FILE);
					break;
				case IDM_MAIN_SESSION_BLANK:
					StartNewSession(connectionhandler, packethandler, server_cid, IMAGE_NONE);

					break;
                default:
                    return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;
		case WM_CREATESESSION:
			CreateSession(connectionhandler, packethandler, 2);
			break;
        case WM_CREATE:
            SHMENUBARINFO mbi;

            memset(&mbi, 0, sizeof(SHMENUBARINFO));
            mbi.cbSize     = sizeof(SHMENUBARINFO);
			mbi.dwFlags    = SHCMBF_HIDESIPBUTTON;
            mbi.hwndParent = hWnd;
            mbi.nToolBarId = IDR_MAIN_MENU;
            mbi.hInstRes   = g_hInst;

            if (!SHCreateMenuBar(&mbi)) 
            {
                g_hWndMainMenuBar = NULL;
            }
            else
            {
                g_hWndMainMenuBar = mbi.hwndMB;
            }

			CreateWindow(L"BUTTON", L"Take a Photo",
				WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 
				10, 100, 220, 30,
				hWnd, (HMENU)IDM_MAIN_SESSION_PHOTO, g_hInst, NULL);
			CreateWindow(L"BUTTON", L"Open an Image",
				WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 
				10, 150, 220, 30,
				hWnd, (HMENU)IDM_MAIN_SESSION_OPEN, g_hInst, NULL);
			CreateWindow(L"BUTTON", L"Empty Background",
				WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 
				10, 200, 220, 30,
				hWnd, (HMENU)IDM_MAIN_SESSION_BLANK, g_hInst, NULL);
	
            // Initialize the shell activate info structure
            memset(&s_sai, 0, sizeof (s_sai));
            s_sai.cbSize = sizeof (s_sai);			
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            
            // TODO: Add any drawing code here...
			WCHAR buf[1024];
			WCHAR server[256];
			WCHAR username[NAME_BUF_SIZE];
			mbstowcs(server, server_address, 255);
			mbstowcs(username, name, NAME_BUF_SIZE-1);
			wsprintf(buf, L"Server: %s\r\nUsername: %s", server, username);
			RECT rc;
			GetClientRect(hWnd, &rc);
			rc.top += 10;
			rc.left += 10;
			DrawText(hdc, buf, -1, &rc, 0);
            
            EndPaint(hWnd, &ps);
            break;

        case WM_DESTROY:
            CommandBar_Destroy(g_hWndMainMenuBar);
            PostQuitMessage(0);
            break;

        case WM_ACTIVATE:
			switch (LOWORD(wParam)) {
				case WA_ACTIVE:
				case WA_CLICKACTIVE:
					changeMyStatus(STATUS_ONLINE);
					break;
				case WA_INACTIVE:
					changeMyStatus(STATUS_AWAY);
					break;
			}
            // Notify shell of our activate message
            SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
            break;
        case WM_SETTINGCHANGE:
            SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
            break;
#ifdef _DEVICE_RESOLUTION_AWARE
        case WM_SIZE:
			break;
#endif
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
static INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
            {
                // Create a Done button and size it.  
                SHINITDLGINFO shidi;
                shidi.dwMask = SHIDIM_FLAGS;
                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
                shidi.hDlg = hDlg;
                SHInitDialog(&shidi);
            }
            return (INT_PTR)TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;

        case WM_CLOSE:
            EndDialog(hDlg, message);
            return TRUE;

#ifdef _DEVICE_RESOLUTION_AWARE
        case WM_SIZE:
            {
		DRA::RelayoutDialog(
			g_hInst, 
			hDlg, 
			DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
            }
            break;
#endif
    }
    return (INT_PTR)FALSE;
}



bool readConfig(void){
	FILE *file;
	if((file = fopen(CONFIG_FILE, "r"))==NULL)
		return false;

	fscanf(file, "%s\n", server_address);
	fscanf(file, "%s\n", name);
	fscanf(file, "%s\n", password);

	printf("%s   %s   %s\n", server_address, name, password);

	return true;
}

bool askYesNo(const WCHAR *title, const WCHAR *fmt, ...) {
	WCHAR buf[1024];
	va_list args;

	va_start(args, fmt);
	wvsprintf(buf, fmt, args);
	va_end(args);
	
	return (IDYES == MessageBox(g_hWndMainWindow, buf, title, MB_YESNO));
}

void showMessage(const WCHAR *title, const WCHAR *fmt, ...) {
	WCHAR buf[1024];
	va_list args;

	va_start(args, fmt);
	wvsprintf(buf, fmt, args);
	va_end(args);

	MessageBox(g_hWndMainWindow, buf, title, MB_OK);
}

⌨️ 快捷键说明

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