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

📄 chatserver.c

📁 比较完善的局域网聊天系统..内容很详细.值得学习
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <windows.h>
#include "srvfile.h"
#include "resource.h"
#include "string.h"
#include "time.h"


#include <DLGS.h>
#include <commdlg.h>
#define ID_RECEIVEEDIT	1
#define ID_SENDEDIT	2
#define ID_BUTTONSEND 3
#define ID_BUTTONSENDFILE 4
#define ID_BUTTONFONTSIZE 5
#define ID_BUTTONRECVFILE 6


#define SERVER_PORT 1111	
#define WM_SOCKET (WM_USER+1)
HFONT hfont,hfontPrev;

COLORREF g_rgbText = RGB(0, 0, 0);
static DWORD rgbCurrent;   // current text color

DWORD rgbPrev;


LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//字体颜色对话框声明
void  OnFontDialog(HWND );


static LOGFONT s_lfSelected;

const int ID_TIMER = 1;

const int BALL_MOVE_DELTA = 2;
//小球结构体
typedef struct _BALLINFO 
{
	int width;
	int height;
	int x;
	int y;
	
	int dx;
	int dy;
}BALLINFO;

BALLINFO g_ballInfo;
HBITMAP g_hbmBall = NULL;
HBITMAP g_hbmMask = NULL;
//选定位图
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
	HDC hdcMem, hdcMem2;
	HBITMAP hbmMask;
	BITMAP bm;
	
	GetObject(hbmColour, sizeof(BITMAP), &bm);
	hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
	
	hdcMem = CreateCompatibleDC(0);
	hdcMem2 = CreateCompatibleDC(0);
	
	SelectObject(hdcMem, hbmColour);
	SelectObject(hdcMem2, hbmMask);
	

	
	BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
	
	BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
	
	DeleteDC(hdcMem);
	DeleteDC(hdcMem2);
	
	return hbmMask;
}
//画小球
void DrawBall(HDC hdc, RECT* prc)
{
	HDC hdcBuffer = CreateCompatibleDC(hdc);
	HBRUSH hbrush=CreateSolidBrush(RGB(100,180,100));

	HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
	HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);
	
	HDC hdcMem = CreateCompatibleDC(hdc);
	HBITMAP hbmOld = SelectObject(hdcMem, g_hbmMask);
	
	FillRect(hdcBuffer, prc, hbrush);
	
	BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width,
		g_ballInfo.height, hdcMem, 0, 0, SRCAND);
	
	SelectObject(hdcMem, g_hbmBall);
	BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT);
	
	BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);
	
	SelectObject(hdcMem, hbmOld);
	DeleteDC(hdcMem);
	
	SelectObject(hdcBuffer, hbmOldBuffer);
	DeleteDC(hdcBuffer);
	DeleteObject(hbmBuffer);
}
//更新小球
void UpdateBall(RECT* prc)
{
	g_ballInfo.x += g_ballInfo.dx;
	g_ballInfo.y += g_ballInfo.dy;
	
	if(g_ballInfo.x < 0)
	{
		g_ballInfo.x = 0;
		g_ballInfo.dx = BALL_MOVE_DELTA;
	}
	else if(g_ballInfo.x + g_ballInfo.width > prc->right)
	{
		g_ballInfo.x = prc->right - g_ballInfo.width;
		g_ballInfo.dx = -BALL_MOVE_DELTA;
	}
	
	if(g_ballInfo.y < 0)
	{
		g_ballInfo.y = 0;
		g_ballInfo.dy = BALL_MOVE_DELTA;
	}
	else if(g_ballInfo.y + g_ballInfo.height > prc->bottom)
	{
		g_ballInfo.y = prc->bottom - g_ballInfo.height;
		g_ballInfo.dy = -BALL_MOVE_DELTA;
	}
}

//帮助对话框
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
	case WM_INITDIALOG:
		
		return TRUE;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd, IDOK);
			break;
		case IDCANCEL:
			EndDialog(hwnd, IDCANCEL);
			break;
		}
		break;
		default:
			return FALSE;
	}
	return TRUE;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpcmdLine, int nCmdShow)
{
	static TCHAR szAppName[]= TEXT("ChatServer");
	static TCHAR szClassName[]= TEXT("ChatServerClass");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;
	
	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc	= WndProc;
	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInstance;
	wndclass.hIcon			= (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
	wndclass.hCursor		= LoadCursor( NULL,IDC_ARROW );
	wndclass.hbrBackground	= (HBRUSH)GetStockObject( WHITE_BRUSH );
	wndclass.lpszMenuName	= MAKEINTRESOURCE(IDR_MENU1);;
	wndclass.lpszClassName	= szClassName;
	
	if ( !RegisterClass( &wndclass ) )
	{
		MessageBox( NULL, TEXT("This program requires Windows NT!"),
			szAppName, MB_ICONERROR );
		
		return 0;
	}
	
	hwnd = CreateWindow( szClassName, TEXT("Simple WinSock Chat Servere"),
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL,	hInstance, NULL	);	
	
	ShowWindow( hwnd, nCmdShow );
	UpdateWindow( hwnd );
	
	while ( GetMessage( &msg, NULL, 0, 0 ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
	
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HINSTANCE		hInstance;
	static HWND		hwndReceiveEdit, hwndSendEdit,hwndButtonSend,hwndButtonSendFile,
	hwndFontSize,hwndButtonRecvFile;
	int				nSize;
	WSADATA			WSAData;
	static struct	sockaddr_in server,from;
	static SOCKET	local_server, local_listen;
	int				nError;
	WORD			wEvent, wError;
	static TCHAR	szReceive[10000];
	static TCHAR	szSend[10000],szRecvBuf[10000],szShow[10000]="";
	HDC hdc,hdcClientSend,hdcClientRecv;
	
	RECT rectSend,rectRecv;
	PAINTSTRUCT ps,psSend,psRecv;
	DWORD dwFileSize,dwRead,dwWrite;
	
	byte szChar[10000]="";

	UINT ret;
	
	BITMAP bm;
	RECT rcClient;

	static int		cxClient,cyClient;
	static HPEN		hpen;
	static HRGN		hrgn,hrgnx,hrgny;
	int x,y;
	
	char tbuffer[10];
	
	
	
	HANDLE hf,hfrecv; 
	OPENFILENAME ofn;       // common dialog box structure
	char szFile[260]="";       // buffer for filename


	OPENFILENAME recvfile;
	char szFileName[260] = "";
	//发送文件结构体初始化
	
	//设定客户区
	rcClient.bottom=600;
	rcClient.top=0;
	rcClient.left=0;
	rcClient.right=200;
	
	
	
	
	
	
	switch( message )
	{
	case WM_CREATE:
ZeroMemory(&ofn, sizeof(OPENFILENAME));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFile = szFile;
	
	ofn.nMaxFile = sizeof(szFile);
	
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = NULL;
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST|OFN_EXPLORER;
	ofn.nFileOffset=0;
	ofn.nFileExtension=0;
	ofn.lpstrDefExt=NULL;
	ofn.lCustData=0;
	ofn.lpfnHook=NULL;

	
	
	//GetOpenFileName(&ofn);
	//接收文件结构体初始化

	ZeroMemory(&recvfile, sizeof(OPENFILENAME));
	
	recvfile.lStructSize = sizeof(OPENFILENAME);
	recvfile.hwndOwner = hwnd;
	recvfile.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
	recvfile.lpstrFile = szFileName;
	recvfile.nMaxFile = MAX_PATH;
	recvfile.lpstrDefExt = "txt";
	recvfile.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
	
		g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
		if(g_hbmBall == NULL)
			MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION);
		
		g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(0, 0, 0));
		if(g_hbmMask == NULL)
			MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);
		
		GetObject(g_hbmBall, sizeof(bm), &bm);
		
		ZeroMemory(&g_ballInfo, sizeof(g_ballInfo));
		g_ballInfo.width = bm.bmWidth;
		g_ballInfo.height = bm.bmHeight;
		
		g_ballInfo.dx = BALL_MOVE_DELTA;
		g_ballInfo.dy = BALL_MOVE_DELTA;
		
		ret = SetTimer(hwnd, ID_TIMER, 50, NULL);
		if(ret == 0)
			MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);



		hInstance = (HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE );
		//接受显示
		hwndReceiveEdit = CreateWindow( TEXT("edit"), NULL,
			WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_HSCROLL | WS_VSCROLL | WS_BORDER
			| ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_LEFT,
			0, 0, 0, 0, hwnd, (HMENU) ID_RECEIVEEDIT, hInstance, NULL );
		//发送显示
		hwndSendEdit = CreateWindow( TEXT("edit"), NULL,
			WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_HSCROLL | WS_VSCROLL | WS_BORDER
			| ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_LEFT,
			0, 0, 0, 0, hwnd, (HMENU) ID_SENDEDIT, hInstance, NULL );
		//发送按钮

⌨️ 快捷键说明

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