#include "main.h"
/ * Declare Dialog Procedure * / Bool Callback DLGProc (HWND, UINT, WPARAM, LPARAM);
/ * Make the class name Into a global variable * /
Hinstance Hinst; HMenu Menu; Bool Server; Int WinStance Hprevinstance, LPSTR LPSZARGUMENT, INT NCMDSHOW)
{HWND hwnd; / * This is the handle for our window * / MSG messages; / * Here messages to the application are saved * / hinst = hThisInstance; hwnd = CreateDialog (hThisInstance, MAKEINTRESOURCE (IDD_DEMO), // identifies dialog box template Name 0, // hwnd_desktop, // Handle To Owner Window (DLGPROC) DLGPROC / / POINTER TO DIALOG BOX Procedure; if (! hw) {char Buf [100]; // Formats and Stores A Series of Characters and Values in A Buffer WSPrintf (BUF, "Error X% X", getLastError ()); MessageBox (0, BUF, SZDLGTILE, MB_ICONEXCLAMATION | MB_OK); RETURN 1;} / * Run the message loop. It will be unsteil getMessage () Returns 0 * / int status; while (status = getMess, NULL, 0, 0))! = 0) {if (! IsDialogmessage (hwnd, & message)) {if (status == - 1) Return -1; / * Translate Virtual-Key ME Ssages INTO Character Messages * / TranslateMessage (& Messages); / * send message to windowprocedure * / dispatchMessage (& Messages);}}
/ * The Program Return-Value IS 0 - The Value That PostquitMessage () Gave * / Return Messages.wParam;
/ * This function is called by the Windows function DispatchMessage () * / BOOL CALLBACK DlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {HWND hSend; switch (message) / * handle the messages * / {case WM_INITDIALOG: Menu = loadmenu (hinst, makeintresource (id_menu)); setmenu (hwnd, menu); hsend = getdlgitem (hwnd, idb_send); // setWindowText (HSEND, "DSDS"); setdlgitemtext (hwnd, idb_lc, "lc"); EnableWindow (Getdlgitem (Idb_lc), False); EnableWindow (Getdlgitem (iDC_IP), False); EnableWindow (Getdlgitem (Hwnd, IDC_Port), False);
Setdlgitemtext (hwnd, idc_ip, "192.168.4"); setdlgitemtext (hwnd, idc_port, "7007"); // Create a timer setTimer (hwnd, 1, 1000, null); // setfocus (Getdlgitem (hwnd, idc_sendxtXT )); break; case WM_COMMAND: switch (wParam) {case IDM_FILENEW: HttpClient (); break; case IDM_FILEOPEN: case IDM_FILELISTEN: server = TRUE; SetDlgItemText (hwnd, IDB_LC, "listening (& L)"); SetWindowText (hwnd, "server"); EnableWindow (GetDlgItem (hwnd, IDB_LC), TRUE); EnableWindow (GetDlgItem (hwnd, IDC_IP), FALSE); EnableWindow (GetDlgItem (hwnd, IDC_PORT), FALSE); EnableMenuItem (menu, IDM_FILECONNECT, MF_GRAYED) ; break; case IDM_FILECONNECT: server = FALSE; SetDlgItemText (hwnd, IDB_LC, "connection server (& C)"); SetWindowText (hwnd, "client"); EnableWindow (GetDlgItem (hwnd, IDB_LC), TRUE); EnableWindow (GetDlgItem (hwnd, idc_ip), true); enableWind ow (GetDlgItem (hwnd, IDC_PORT), TRUE); EnableMenuItem (menu, IDM_FILELISTEN, MF_GRAYED); break; case IDM_EDITUNDO: case IDM_EDITCUT: case IDM_EDITCOPY: case IDM_EDITPASTE: case IDM_EDITDELETE: case IDM_TRANSPORTFILE: MessageBox (hwnd, (LPSTR) "function No implementation ", (lpstr) szdlgtitle, MB_ICONITIONFORMATION | MB_OK; Break; Case IDM_HELPHELP: WinHelp (HWnd, (lpstr)" Helpfile.hlp ", Help_helponhelp, 0L); Break;
Case IDM_FILEEXIT: SendMessage (hwnd, wm_close, 0, 0L); Break; Case IDM_HelPabout: MessageBox (hwnd, "Welcome to chat programs 1.0 version / n / n ** University Beard / N2004 (C) Copyright", (LPSTR ) SZDLGTILE, MB_ICONITIONFORMATION | MB_OK; Break; Case IDM_HelpMyblog: Shellexecute (hwnd, "open", "http://blog.9cbs.net/huyoo", null, null, sw_normal; break;
Case IDCancel: // MessageBox (hwnd, "You click Exit, the dialog will close ./n/n Goodbye!", szdlgtitle, 0); enddialog (hwnd, true); sendMessage (hwnd, wm_close, 0, 0L); Break; Case IDB_Send: Char Sztext [128], BUF [256]; Getdlgitemtext (hwnd, idc_sendtxt, sztext, sizeof (sztext)); WSPrintf (buf, "You click Send message ./n/n you The input text is:% s ", sztext); if (server) {IF (send (connection_skt, buf, strlen (buf), 0) == Socket_ERROR) // client is a blocking {MessageBox (hwnd," Send message error! ", Szdlgtitle, mb_ok; break;}} else {IF (SEND (SKT, BUF, STRLEN (BUF), 0) == SOCKET_ERROR) // Client is a blocking {MessageBox (hwnd," Send message error! ", Szdlgtitle, mb_ok; break;} // RECV (SKT, BUF, Strlen (BUF), 0); // It will stop here until the data is received} Setdlgitemtext (hwnd, idc_revtxt, sztext); Break; Case IDB_QQ: Shellexecute (HWND, "Open", "QQ.EXE", NULL, NULL, SW_NORMAL); Break; Case IDB_LC: IF (Server) {// MessageBox (hwnd , "listen", szdlgtitle, MB_OK; if (! CreateServer (hwnd)) {MessageBox (HWND, Server Creating Failed ", (LPSTR) Szdlgtitle, MB_ICONInformation | MB_OK; Break;}} else {// MessageBox (hwnd , "Connect", Szdlgtitle, MB_OK; if (! CreateClient (hwnd)) {MessageBox (hwnd, "Client Creation Failure"
(Lpstr) szdlgtitle, MB_ICONInformation | MB_OK; Break;} char szip [127], szport [127], buf [256]; getdlgitemtext (hwnd, idc_ip, szip, sizeof (szip)); getdlgitemtext (hwnd, idc_port, Szport, SizeOf (Szport); WSPrintf (BUF, ">>>>>> Connect to Server:% S Port:% S", SZIP, Szport); setdlgitemtext (hwnd, idc_revtxt, buf);} Break;
} Break; Case WM_TIMER: SystemTime Ti; Char BUF [256]; GetLocalTime (& Ti); WSPrintf (BUF, "Today is% 04D% 02D: 02D Da% 02D:% 02D:% 02D, Zhou% D" , Ti.wyear, Ti.WMONTH, TI.WDAY, TI.WHOUR, TI.WMINUTE, TI.WSECOND, TI.WDAYOFWEEK; setWindowText (Getdlgitem (HWnd, IDC_TIME), BUF); Break; Case Socketmsg: // IF (WSAGETSELECTERROR (lParam)) // break; switch (WSAGETSELECTEVENT (lParam)) {case FD_READ: char buf [256]; recv (connected_skt, buf, strlen (buf), 0); SetDlgItemText (hwnd, IDC_REVTXT, buf); // Display acceptable message Break; Case FD_WRITE: CLOSESOCKET (Connected_Skt); Break; Case FD_Connect: setdlgitemtext (hwnd, idc_revtxt, "already connected"); // Display acceptable message BREAK; CASE FD_ACCEPT: INT LEN = SIZEOF (CLIENTDR); // Establish a connection, the resulting address is stored in the client clientaddr address to IF ((Connected_skt = Accept (STRUCT SOCKADDR *) & ClientDr, & Len) == Invalid_Socket) {MessageBox (hwnd, "Accept Client's socket connection failed ", szdlgtitle, mb_ok); return false;} // Connect, then listen to the client's fd_read and turn off WSaasyncselect (connect_skt, hwnd, socketmsg, fd_read | fd_close); char * Welcome =" Welcome Chat room !! "; Send (WPARAM, Welcome, Strlen (Welcome), 0); Break;} setdlgitemtext (hwnd, idc_revtxt," client send message "); // Display acceptable message Break; Case WM_Close : If (connect_skt! = Invalid_socket) {CloseSocket (Connected_skt);} if (Skt! =
INVALID_SOCKET) {CloseSocket (SKT);} IF (wsacleanup ()! = 0) {MessageBox (hwnd, "can't release Winsocket", szdlgtitle, mb_ok;} killtimer (hwnd, 1); DestroyWindow (hwnd);
Break; Case WM_DESTROY: PostquitMessage (0); / * Send a wm_quit to the message queue * / break; default: returnaf false;}
Return True;}