Advertisement
Combreal

FoDChat.cpp

Dec 1st, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.56 KB | None | 0 0
  1. #include "resource.h"  
  2.  
  3. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  4. {
  5.     MSG msg;
  6.     WNDCLASSW wc = { 0 };
  7.     wc.lpszClassName = L"FoDChat";
  8.     wc.hInstance = hInstance;
  9.     wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  10.     wc.lpfnWndProc = WndProc;
  11.     wc.hCursor = LoadCursor(0, IDC_ARROW);
  12.     wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  13.     RegisterClassW(&wc);
  14.     hWindowa = CreateWindowW(wc.lpszClassName, L"FoDChat", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE, 0, 0, 700, 422, 0, 0, hInstance, 0);//WS_OVERLAPPEDWINDOW
  15.  
  16.     WNDCLASSW wcB = { 0 };
  17.     wcB.lpszClassName = L"Settings";
  18.     wcB.hInstance = hInstance;
  19.     wcB.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  20.     wcB.lpfnWndProc = WndProcB;
  21.     wcB.hCursor = LoadCursor(0, IDC_ARROW);
  22.     wcB.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  23.     RegisterClassW(&wcB);
  24.     hWindowb = CreateWindowW(wcB.lpszClassName, L"", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, 0, 0, 204, 155, 0, 0, hInstance, 0); //WS_OVERLAPPEDWINDOW | WS_VISIBLE
  25.     ShowWindow(hWindowb, SW_HIDE);
  26.  
  27.     if (RegisterHotKey(NULL, 1, MOD_NOREPEAT, 0x0D))
  28.     {
  29.         OutputDebugStringW(L"Hotkey 'Return' registered, using MOD_NOREPEAT flag\n");
  30.     }
  31.     WSAStartup(MAKEWORD(2, 0), &WSAData);
  32.     server = socket(AF_INET, SOCK_STREAM, 0);
  33.     InetPton(AF_INET, "192.168.1.16", &addr.sin_addr.s_addr);
  34.     addr.sin_family = AF_INET;
  35.     addr.sin_port = htons(5555);
  36.     sock = server;
  37.     int * newSocket = static_cast<int*>(malloc(1));
  38.     *newSocket = sock;
  39.     if (connect(server, (SOCKADDR *)&addr, sizeof(addr)) == 0)
  40.     {
  41.         OutputDebugStringW(L"\nConnected to server!\n");
  42.         sendThread = thread(sendMessage, (void *)newSocket);
  43.         receiveThread = thread(receiveMessage, (void *)newSocket);
  44.         //sendThread.join();//hangs undefinitely
  45.         //receiveThread.join();//hangs undefinitely, rest of code don't follow
  46.     }
  47.     else
  48.     {
  49.         OutputDebugStringW(L"\nCouldn't connect to server.\n");
  50.     }
  51.     while (GetMessage(&msg, NULL, 0, 0))
  52.     {
  53.         if (showWindowB)
  54.         {
  55.             ShowWindow(hWindowb, nCmdShow);
  56.         }
  57.         else if (!showWindowB)
  58.         {
  59.             ShowWindow(hWindowb, SW_HIDE);
  60.         }
  61.         TranslateMessage(&msg);
  62.         DispatchMessage(&msg);
  63.         if (msg.message == WM_HOTKEY)
  64.         {
  65.             SendText(hwndEditA, hwndEditB);
  66.             sendCheck = true;
  67.             //OutputDebugStringW(L"lol\n");
  68.         }
  69.     }
  70.     return (int)msg.wParam;
  71. }
  72.  
  73. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  74. {
  75.     TCHAR lpszGreetings[] = "Welcome to FoD";
  76.     switch (msg)
  77.     {
  78.     case WM_KEYDOWN:
  79.         if (wParam == VK_ESCAPE)
  80.         {
  81.             int ret = MessageBoxW(hwnd, L"Quit?", L"Message", MB_OKCANCEL);
  82.             if (ret == IDOK)
  83.             {
  84.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  85.             }
  86.         }
  87.         else if (LOWORD(wParam) == VK_RETURN)
  88.         {
  89.             SendText(hwndEditA, hwndEditB);
  90.             sendCheck = true;
  91.         }
  92.         break;
  93.     case WM_CREATE:
  94.         CenterWindow(hwnd);
  95.         AddMenus(hwnd);
  96.         hwndEditA = CreateWindowW(L"EDIT", NULL, WS_CHILD | WS_BORDER | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_LEFT | ES_MULTILINE | ES_READONLY, 5, 5, 675, 300, hwnd, (HMENU)ID_EDITA, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
  97.         //SendMessage(hwndEditA, WM_SETTEXT, 0, (LPARAM)lpszGreetings);
  98.         hwndTextA = CreateWindowEx(0, "STATIC", "Chat : ", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 5, 310, 80, 25, hwnd, (HMENU)IDC_STATICA, GetModuleHandle(NULL), 0);
  99.         hwndEditB = CreateWindowW(L"EDIT", NULL, WS_CHILD | WS_BORDER | WS_VISIBLE | ES_LEFT | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE, 5, 330, 585, 28, hwnd, (HMENU)ID_EDITB, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); //ES_WANTRETURN | ES_MULTILINE
  100.         hwndButtonA = CreateWindowW(L"Button", L"SEND", WS_VISIBLE | WS_CHILD, 595, 325, 84, 32, hwnd, (HMENU)ID_BUTTONA, NULL, NULL);
  101.         break;
  102.     case WM_COMMAND:
  103.         /*if (LOWORD(wParam) == ID_EDITB)// && IDOK
  104.         {
  105.             SendText(hwndEditA, hwndEditB);
  106.             sendCheck = true;
  107.         }*/
  108.         if (LOWORD(wParam) == ID_BUTTONA)
  109.         {
  110.             SendText(hwndEditA, hwndEditB);
  111.             sendCheck = true;
  112.         }
  113.         else if (LOWORD(wParam) == IDM_CONFIGURE)
  114.         {
  115.             showWindowB = true;
  116.             SetActiveWindow(hWindowb);
  117.             break;
  118.         }
  119.         else if (LOWORD(wParam) == IDM_ABOUT)
  120.         {
  121.             MessageBox(hwnd, "FoDChat is a TCP chat coded in C++.\n"
  122.                 "01/12/19 - It uses a GPL.\n"
  123.                 "\n"
  124.                 "You can change the network settings\n"
  125.                 "\n"
  126.                 "Please contact me at maxime.p.jolly@gmail.com\n"
  127.                 "if you have any questions or suggestions.\n",
  128.                 "Informations", MB_ICONQUESTION);
  129.         }
  130.         break;
  131.     case WM_CHAR:
  132.         if ((LOWORD(wParam) == VK_RETURN) && !showWindowB)
  133.         {
  134.             SendText(hwndEditA, hwndEditB);
  135.             sendCheck = true;
  136.         }
  137.         break;
  138.     case WM_HOTKEY:
  139.         //OutputDebugStringW(L"lol\n");
  140.         break;
  141.     case WM_DESTROY:
  142.         if (sendThread.joinable())
  143.         {
  144.             sendThread.join();
  145.         }
  146.         if (receiveThread.joinable())
  147.         {
  148.             receiveThread.join();
  149.         }
  150.         UnregisterHotKey(NULL, 1);
  151.         closesocket(server);
  152.         WSACleanup();
  153.         OutputDebugStringW(L"\nServer has closed.\n");
  154.         PostQuitMessage(0);
  155.         break;
  156.     }
  157.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  158. }
  159.  
  160. LRESULT CALLBACK WndProcB(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  161. {
  162.     TCHAR buffAddress[1024] = "";
  163.     TCHAR buffPort[1024] = "";
  164.     TCHAR buffUsername[1024] = "";
  165.     _tcscat_s(buffAddress, 1024, address.c_str());
  166.     _tcscat_s(buffPort, 1024, port.c_str());
  167.     _tcscat_s(buffUsername, 1024, username.c_str());
  168.     switch (msg)
  169.     {
  170.     case WM_CREATE:
  171.     {
  172.         CenterWindow(hwnd);
  173.         hwndTextC = CreateWindowEx(0, "STATIC", "Address : ", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 5, 5, 80, 25, hwnd, (HMENU)IDC_STATICC, GetModuleHandle(NULL), 0);
  174.         hwndEditC = CreateWindowW(L"EDIT", NULL, WS_CHILD | WS_BORDER | WS_VISIBLE | ES_LEFT, 5, 30, 100, 21, hwnd, (HMENU)ID_EDITC, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
  175.         SendMessage(hwndEditC, WM_SETTEXT, 0, (LPARAM)buffAddress);
  176.         hwndTextD = CreateWindowEx(0, "STATIC", "Port : ", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 125, 5, 80, 25, hwnd, (HMENU)IDC_STATICD, GetModuleHandle(NULL), 0);
  177.         hwndEditD = CreateWindowW(L"EDIT", NULL, WS_CHILD | WS_BORDER | WS_VISIBLE | ES_LEFT, 125, 30, 55, 21, hwnd, (HMENU)ID_EDITD, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
  178.         SendMessage(hwndEditD, WM_SETTEXT, 0, (LPARAM)buffPort);
  179.         hwndTextE = CreateWindowEx(0, "STATIC", "Username : ", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 5, 60, 80, 25, hwnd, (HMENU)IDC_STATICE, GetModuleHandle(NULL), 0);
  180.         hwndEditE = CreateWindowW(L"EDIT", NULL, WS_CHILD | WS_BORDER | WS_VISIBLE | ES_LEFT, 5, 85, 100, 21, hwnd, (HMENU)ID_EDITE, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
  181.         //SendMessage(hwndEditE, WM_SETTEXT, 0, (LPARAM)TEXT(stringToLPWSTR(username)));
  182.         SendMessage(hwndEditE, WM_SETTEXT, 0, (LPARAM)buffUsername);
  183.         hwndButtonB = CreateWindowW(L"Button", L"Save", WS_VISIBLE | WS_CHILD, 121, 78, 60, 28, hwnd, (HMENU)ID_BUTTONB, NULL, NULL);
  184.     }
  185.         break;
  186.     case WM_COMMAND:
  187.         if (LOWORD(wParam) == ID_BUTTONB)
  188.         {
  189.             TCHAR buffAddress[1024];
  190.             TCHAR buffPort[1024];
  191.             TCHAR buffUsername[1024];
  192.             GetWindowText(hwndEditC, buffAddress, 1024);
  193.             GetWindowText(hwndEditD, buffPort, 1024);
  194.             GetWindowText(hwndEditE, buffUsername, 1024);
  195.             address = buffAddress;
  196.             port = buffPort;
  197.             username = buffUsername;
  198.             //save config to file?
  199.             showWindowB = false;
  200.             SetActiveWindow(hWindowa);
  201.             return 0;
  202.             break;
  203.         }
  204.         break;
  205.     case WM_CLOSE:
  206.         showWindowB = false;
  207.         SetActiveWindow(hWindowa);
  208.         return 0;
  209.         break;
  210.     default:
  211.         break;
  212.     }
  213.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  214. }
  215.  
  216. void CenterWindow(HWND hwnd)
  217. {
  218.     RECT rc = { 0 };
  219.     GetWindowRect(hwnd, &rc);
  220.     int win_w = rc.right - rc.left;
  221.     int win_h = rc.bottom - rc.top;
  222.     int screen_w = GetSystemMetrics(SM_CXSCREEN);
  223.     int screen_h = GetSystemMetrics(SM_CYSCREEN);
  224.     SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w) / 2, (screen_h - win_h) / 2, 0, 0, SWP_NOSIZE);
  225. }
  226.  
  227. void SendText(HWND chatBox, HWND sendBox)
  228. {
  229.     TCHAR buff[1024];
  230.     GetWindowText(sendBox, buff, 1024);
  231.     messageInABottle = username + " : " + buff;
  232.     SendMessage(sendBox, WM_SETTEXT, 0, (LPARAM)"");
  233. }
  234.  
  235. LPWSTR stringToLPWSTR(const std::string& instr)
  236. {
  237.     int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
  238.     LPWSTR widestr = new WCHAR[bufferlen + 1];
  239.     ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
  240.     widestr[bufferlen] = 0;
  241.     return widestr;
  242. }
  243.  
  244. void AddMenus(HWND hwnd)
  245. {
  246.     HMENU hMenubar;
  247.     HMENU hMenu;
  248.     HMENU hMenuB;
  249.     hMenubar = CreateMenu();
  250.     hMenu = CreateMenu();
  251.     hMenuB = CreateMenu();
  252.     AppendMenuW(hMenu, MF_STRING, IDM_CONFIGURE, L"&Configure");
  253.     AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenu, L"&Settings");
  254.     AppendMenuW(hMenuB, MF_STRING, IDM_ABOUT, L"About");
  255.     AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenuB, L"&?");
  256.     SetMenu(hwnd, hMenubar);
  257. }  
  258.  
  259. void *sendMessage(void *sock_desc)
  260. {
  261.     while (TRUE)
  262.     {
  263.         if (sendCheck)
  264.         {
  265.             if (send(*((int *)sock_desc), messageInABottle.c_str(), sizeof(messageInABottle) + 1, 0) < 0)
  266.             {
  267.                 OutputDebugStringW(L"\nSend fail.\n");
  268.  
  269.             }
  270.             sendCheck = false;
  271.         }
  272.     }
  273. }
  274.  
  275. void *receiveMessage(void *sock_desc)
  276. {
  277.     char responce[2000];
  278.     while (TRUE)
  279.     {
  280.         memset(responce, 0, sizeof responce);
  281.         if (recv(*((int *)sock_desc), responce, sizeof(responce), 0) < 0)
  282.         {
  283.             OutputDebugStringW(L"\nRCV fail.\n");
  284.         }
  285.         //OutputDebugStringW(L"\nResponce : ");
  286.         OutputDebugStringW(L"\n");
  287.         OutputDebugStringW(stringToLPWSTR(responce));
  288.  
  289.         TCHAR buff[1024];
  290.         GetWindowText(hwndEditA, buff, 1024);
  291.         _tcscat_s(buff, 1024, "\r\n");
  292.         _tcscat_s(buff, 1024, responce);
  293.         SendMessage(hwndEditA, WM_SETTEXT, 0, (LPARAM)buff);
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement