?? remotelookup.cpp
字號(hào):
// 我真誠(chéng)地保證:
// 我自己獨(dú)立地完成了整個(gè)程序從分析、設(shè)計(jì)到編碼的所有工作。
// 如果在上述過(guò)程中,我遇到了什么困難而求教于人,那么,我將在程序?qū)嵙?xí)報(bào)告中
// 詳細(xì)地列舉我所遇到的問(wèn)題,以及別人給我的提示。
// 在此,我感謝張老師對(duì)我的啟發(fā)和幫助。下面的報(bào)告中,我還會(huì)具體地提到
// 他們?cè)诟鱾€(gè)方法對(duì)我的幫助。
// 我的程序里中凡是引用到其他程序或文檔之處,
// 例如教材、課堂筆記、網(wǎng)上的源代碼以及其他參考書(shū)上的代碼段,
// 我都已經(jīng)在程序的注釋里很清楚地注明了引用的出處。
// 我從未沒(méi)抄襲過(guò)別人的程序,也沒(méi)有盜用別人的程序,
// 不管是修改式的抄襲還是原封不動(dòng)的抄襲。
// 我編寫(xiě)這個(gè)程序,從來(lái)沒(méi)有想過(guò)要去破壞或妨礙其他計(jì)算機(jī)系統(tǒng)的正常運(yùn)轉(zhuǎn)。
// 胡峰令
#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[] = {"服務(wù)器IP" , "單詞" } ,
*szBtText[] = {"連接" , "查詢" , "簡(jiǎn)明意思"};
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地址格式錯(cuò)誤" , "錯(cuò)誤" , MB_OK);
return 0;
}
if(!ConnectToServer(ServerAddr , &ClientSocket))
{
MessageBox(hwnd , "服務(wù)器連接失敗" , "失敗" , 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 );
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -