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

📄 remotelookup.cpp

📁 通过在远端主机上搭建一个远程字典服务服务器
💻 CPP
字号:
// 我真诚地保证:
// 我自己独立地完成了整个程序从分析、设计到编码的所有工作。
// 如果在上述过程中,我遇到了什么困难而求教于人,那么,我将在程序实习报告中
// 详细地列举我所遇到的问题,以及别人给我的提示。
// 在此,我感谢张老师对我的启发和帮助。下面的报告中,我还会具体地提到
// 他们在各个方法对我的帮助。
// 我的程序里中凡是引用到其他程序或文档之处,
// 例如教材、课堂笔记、网上的源代码以及其他参考书上的代码段,
// 我都已经在程序的注释里很清楚地注明了引用的出处。
// 我从未没抄袭过别人的程序,也没有盗用别人的程序,
// 不管是修改式的抄袭还是原封不动的抄袭。
// 我编写这个程序,从来没有想过要去破坏或妨碍其他计算机系统的正常运转。
// 胡峰令


#include "sock_common.h"
#include <windows.h>


LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
const int xSize = 450;
const int ySize = 330;
const int ID_CONNECT = 1;
const int ID_SEARCH = 2;
SOCKET ClientSocket;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   	   LPSTR lpCmdLine, int nCmdShow)
{
		static TCHAR szAppName[]= TEXT("RemoteLookup");
		static TCHAR szClassName[]= TEXT("RemoteLookupClass");
		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			= LoadIcon( NULL, IDI_APPLICATION );
		wndclass.hCursor		= LoadCursor( NULL,IDC_ARROW );
		wndclass.hbrBackground	= (HBRUSH)GetStockObject( WHITE_BRUSH );
		wndclass.lpszMenuName	= NULL;
		wndclass.lpszClassName	= szClassName;
	
		if ( !RegisterClass( &wndclass ) )
		{
			MessageBox( NULL, TEXT("This program requires Windows NT!"),
						szAppName, MB_ICONERROR );
			return 0;
		}

		hwnd = CreateWindow(szClassName,
							TEXT("在线查单词"),
							WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX ,
							CW_USEDEFAULT,
							CW_USEDEFAULT,
							xSize,
							ySize,
							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)
{
		HDC			hdc;
		PAINTSTRUCT	ps;
		RECT rect;

		static HWND hwndLabel[2] , hwndBt[3] , hwndEdit[3] , hwndList;
		static char *szLabelText[] = {"服务器IP" , "单词" } , 
			        *szBtText[]      = {"连接"     , "查询" , "简明意思"};
		int i;

		static HBRUSH hBrushStatic;
		HFONT ghStaticFont;
		HINSTANCE hInstance;

		char ServerAddr[20] , Word[80] , RecBuf[80];
		

		switch( message )
		{
		case WM_CREATE:
			hInstance = (HINSTANCE) GetWindowLong(hwnd , GWL_HINSTANCE);

			
			for(i = 0 ; i < 2 ; i++)
			{

				hwndLabel[i] = CreateWindow(TEXT("static"),szLabelText[i],
					                        WS_CHILD | WS_VISIBLE | SS_CENTER ,
											10 , 10 + 30 * i , 70 , 22 , 
											hwnd , (HMENU)i,
											hInstance , NULL);

				if(i == 1)
					hwndBt[i] = CreateWindow(TEXT("button") , szBtText[i] , 
											 WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON ,
											 300 , 10 + 30 * i , 70 , 23 ,
											 hwnd , (HMENU)(i+1) ,
											 hInstance , NULL);

				hwndEdit[i] = CreateWindow(TEXT("edit") , NULL ,
					                       WS_CHILD | WS_BORDER | ES_LEFT   |WS_VISIBLE,
										   87 , 10 + 30 * i , 190 , 22 ,
										   hwnd , (HMENU)1 , 
										   hInstance , NULL);
				ghStaticFont =  (HFONT)GetStockObject(SYSTEM_FIXED_FONT);
			    SendMessage(hwndLabel[i],WM_SETFONT,(WPARAM)ghStaticFont,MAKELPARAM(TRUE,0));
				SendMessage(hwndBt[i],WM_SETFONT,(WPARAM)ghStaticFont,MAKELPARAM(TRUE,0));
				
					                     
			}

			hwndBt[2] = CreateWindow(TEXT("button") , szBtText[2] , 
					                     WS_CHILD | WS_VISIBLE | BS_GROUPBOX ,
										 10 , 70 , 400 , 200 ,
										 hwnd , (HMENU)i ,
										 hInstance , NULL);

			hwndList = CreateWindow(TEXT("edit") , NULL , 
					                     WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_READONLY,
										 15 , 100 , 350 , 160 ,
										 hwnd , (HMENU)i ,
										 hInstance , NULL);

			hBrushStatic = CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT));

			ghStaticFont =  (HFONT)GetStockObject(SYSTEM_FIXED_FONT);
			SendMessage(hwndBt[2] , WM_SETFONT , (WPARAM)ghStaticFont , MAKELPARAM(TRUE , 0));
 
			SendMessage(hwndList , WM_SETFONT , (WPARAM)ghStaticFont , MAKELPARAM(TRUE , 0));



			return 0;

		case WM_SIZE:
			return 0;
		case WM_SETFOCUS:
			SetFocus(hwndEdit[0]);
			return 0;

		case WM_MOUSEMOVE:
			return 0;
		case WM_COMMAND:
			switch(wParam)
			{
			case ID_CONNECT:
			//	GetWindowText(hwndEdit[0] , ServerAddr , 20);
			//	ConnectToServer(ServerAddr , &ClientSocket);
				break;
			case ID_SEARCH:
				GetWindowText(hwndEdit[0] , ServerAddr , 20);
				if(!CheckAddr(ServerAddr))
				{
					MessageBox(hwnd , "IP地址格式错误" , "错误" , MB_OK);
					return 0;
				}
				if(!ConnectToServer(ServerAddr , &ClientSocket))
				{
					MessageBox(hwnd , "服务器连接失败" , "失败" , MB_OK);
					return 0;
				}
				GetWindowText(hwndEdit[1] , Word , 80);
				Trim(Word);
				strcat(Word,"\r\n");
				if(!SeachWord(ClientSocket , Word , RecBuf))
				{
					MessageBox(hwnd , "单词查找失败" , "失败" , MB_OK);
					return 0;
				}

				SetWindowText(hwndList , RecBuf);
				break;
			default :
				break;
			}
			return 0;

		case WM_PAINT:
			hdc = BeginPaint( hwnd, &ps );

			GetClientRect( hwnd, &rect );
		//	DrawText( hdc, TEXT("Hello,Windows 98!"), -1, &rect,
		//		  	DT_SINGLELINE | DT_CENTER | DT_VCENTER );

			EndPaint( hwnd, &ps );

			return 0;

		case WM_CTLCOLORSTATIC:
			SetTextColor((HDC)wParam , RGB(0 , 0 , 0));
			SetBkColor((HDC)wParam , GetSysColor(COLOR_BTNHIGHLIGHT));
			return (LRESULT)hBrushStatic;

		case WM_DESTROY:
			PostQuitMessage( 0 );
			return 0;
		}
		return DefWindowProc( hwnd, message, wParam, lParam );
}

⌨️ 快捷键说明

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