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

📄 win32_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.
*/
// win32_client.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "win32_client.h"
#include "win32_drawing.h"
#include "../common/defines.h"
#include "../common/network.h"

#define MAX_LOADSTRING 100
#define CONFIG_FILE "config.txt"

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

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



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

bool readConfig(void);


int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;


	if(!readConfig()){
		MessageBox(NULL, L"Could not read config.txt.", L"Error.", MB_OK);
		return(FALSE);
	}


	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_WIN32_CLIENT, szWindowClass, MAX_LOADSTRING);
	LoadString(hInstance, IDC_WIN32_DRAWING, szDrawingWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

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

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32_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:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	ATOM ret;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32_CLIENT));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDR_MAIN_MENU);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	ret = RegisterClassEx(&wcex);
	if (!ret) {
		return ret;
	}

	wcex.style			= CS_HREDRAW | CS_VREDRAW;  // | CS_DBLCLKS;
	wcex.lpfnWndProc	= DrawingWndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32_CLIENT));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);;
	wcex.hbrBackground	= NULL;	//(HBRUSH) GetStockObject(WHITE_BRUSH);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDR_DRAWING_MENU);
	wcex.lpszClassName	= szDrawingWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   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.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

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

	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, 320, 198, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}
	g_hWndMainWindow = hWnd;
	if (!initNetwork(server_address, name, password)){
		MessageBox(hWnd, L"Could not initialize network.", L"Network Error", MB_OK);
		return 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
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(g_hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			SendMessage (hWnd, WM_CLOSE, 0, 0);
			break;
		case IDM_MAIN_SESSION_OPEN:
			changeMyStatus(STATUS_AWAY);
			StartNewSession(connectionhandler, packethandler, server_cid, IMAGE_FILE);
			break;
		case IDM_MAIN_SESSION_BLANK:
			changeMyStatus(STATUS_AWAY);
			StartNewSession(connectionhandler, packethandler, server_cid, IMAGE_NONE);
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_CREATESESSION:
		changeMyStatus(STATUS_AWAY);
		CreateSession(connectionhandler, packethandler, 2);
		break;
	case WM_CREATE:
		break;

	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
	
		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);
		DrawText(hdc, buf, -1, &rc, 0);
 
		
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_CLOSE:

		DestroyWindow(hWnd);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	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 + -