Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "resource.h"
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
- {
- MSG msg;
- WNDCLASSW wc = { 0 };
- wc.lpszClassName = L"FoDChat";
- wc.hInstance = hInstance;
- wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- wc.lpfnWndProc = WndProc;
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
- RegisterClassW(&wc);
- 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
- WNDCLASSW wcB = { 0 };
- wcB.lpszClassName = L"Settings";
- wcB.hInstance = hInstance;
- wcB.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- wcB.lpfnWndProc = WndProcB;
- wcB.hCursor = LoadCursor(0, IDC_ARROW);
- wcB.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
- RegisterClassW(&wcB);
- 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
- ShowWindow(hWindowb, SW_HIDE);
- if (RegisterHotKey(NULL, 1, MOD_NOREPEAT, 0x0D))
- {
- OutputDebugStringW(L"Hotkey 'Return' registered, using MOD_NOREPEAT flag\n");
- }
- WSAStartup(MAKEWORD(2, 0), &WSAData);
- server = socket(AF_INET, SOCK_STREAM, 0);
- InetPton(AF_INET, "192.168.1.16", &addr.sin_addr.s_addr);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(5555);
- sock = server;
- int * newSocket = static_cast<int*>(malloc(1));
- *newSocket = sock;
- if (connect(server, (SOCKADDR *)&addr, sizeof(addr)) == 0)
- {
- OutputDebugStringW(L"\nConnected to server!\n");
- sendThread = thread(sendMessage, (void *)newSocket);
- receiveThread = thread(receiveMessage, (void *)newSocket);
- //sendThread.join();//hangs undefinitely
- //receiveThread.join();//hangs undefinitely, rest of code don't follow
- }
- else
- {
- OutputDebugStringW(L"\nCouldn't connect to server.\n");
- }
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (showWindowB)
- {
- ShowWindow(hWindowb, nCmdShow);
- }
- else if (!showWindowB)
- {
- ShowWindow(hWindowb, SW_HIDE);
- }
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- if (msg.message == WM_HOTKEY)
- {
- SendText(hwndEditA, hwndEditB);
- sendCheck = true;
- //OutputDebugStringW(L"lol\n");
- }
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- TCHAR lpszGreetings[] = "Welcome to FoD";
- switch (msg)
- {
- case WM_KEYDOWN:
- if (wParam == VK_ESCAPE)
- {
- int ret = MessageBoxW(hwnd, L"Quit?", L"Message", MB_OKCANCEL);
- if (ret == IDOK)
- {
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- }
- }
- else if (LOWORD(wParam) == VK_RETURN)
- {
- SendText(hwndEditA, hwndEditB);
- sendCheck = true;
- }
- break;
- case WM_CREATE:
- CenterWindow(hwnd);
- AddMenus(hwnd);
- 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);
- //SendMessage(hwndEditA, WM_SETTEXT, 0, (LPARAM)lpszGreetings);
- hwndTextA = CreateWindowEx(0, "STATIC", "Chat : ", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 5, 310, 80, 25, hwnd, (HMENU)IDC_STATICA, GetModuleHandle(NULL), 0);
- 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
- hwndButtonA = CreateWindowW(L"Button", L"SEND", WS_VISIBLE | WS_CHILD, 595, 325, 84, 32, hwnd, (HMENU)ID_BUTTONA, NULL, NULL);
- break;
- case WM_COMMAND:
- /*if (LOWORD(wParam) == ID_EDITB)// && IDOK
- {
- SendText(hwndEditA, hwndEditB);
- sendCheck = true;
- }*/
- if (LOWORD(wParam) == ID_BUTTONA)
- {
- SendText(hwndEditA, hwndEditB);
- sendCheck = true;
- }
- else if (LOWORD(wParam) == IDM_CONFIGURE)
- {
- showWindowB = true;
- SetActiveWindow(hWindowb);
- break;
- }
- else if (LOWORD(wParam) == IDM_ABOUT)
- {
- MessageBox(hwnd, "FoDChat is a TCP chat coded in C++.\n"
- "01/12/19 - It uses a GPL.\n"
- "\n"
- "You can change the network settings\n"
- "\n"
- "Please contact me at maxime.p.jolly@gmail.com\n"
- "if you have any questions or suggestions.\n",
- "Informations", MB_ICONQUESTION);
- }
- break;
- case WM_CHAR:
- if ((LOWORD(wParam) == VK_RETURN) && !showWindowB)
- {
- SendText(hwndEditA, hwndEditB);
- sendCheck = true;
- }
- break;
- case WM_HOTKEY:
- //OutputDebugStringW(L"lol\n");
- break;
- case WM_DESTROY:
- if (sendThread.joinable())
- {
- sendThread.join();
- }
- if (receiveThread.joinable())
- {
- receiveThread.join();
- }
- UnregisterHotKey(NULL, 1);
- closesocket(server);
- WSACleanup();
- OutputDebugStringW(L"\nServer has closed.\n");
- PostQuitMessage(0);
- break;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- LRESULT CALLBACK WndProcB(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- TCHAR buffAddress[1024] = "";
- TCHAR buffPort[1024] = "";
- TCHAR buffUsername[1024] = "";
- _tcscat_s(buffAddress, 1024, address.c_str());
- _tcscat_s(buffPort, 1024, port.c_str());
- _tcscat_s(buffUsername, 1024, username.c_str());
- switch (msg)
- {
- case WM_CREATE:
- {
- CenterWindow(hwnd);
- hwndTextC = CreateWindowEx(0, "STATIC", "Address : ", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 5, 5, 80, 25, hwnd, (HMENU)IDC_STATICC, GetModuleHandle(NULL), 0);
- 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);
- SendMessage(hwndEditC, WM_SETTEXT, 0, (LPARAM)buffAddress);
- hwndTextD = CreateWindowEx(0, "STATIC", "Port : ", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 125, 5, 80, 25, hwnd, (HMENU)IDC_STATICD, GetModuleHandle(NULL), 0);
- 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);
- SendMessage(hwndEditD, WM_SETTEXT, 0, (LPARAM)buffPort);
- hwndTextE = CreateWindowEx(0, "STATIC", "Username : ", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 5, 60, 80, 25, hwnd, (HMENU)IDC_STATICE, GetModuleHandle(NULL), 0);
- 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);
- //SendMessage(hwndEditE, WM_SETTEXT, 0, (LPARAM)TEXT(stringToLPWSTR(username)));
- SendMessage(hwndEditE, WM_SETTEXT, 0, (LPARAM)buffUsername);
- hwndButtonB = CreateWindowW(L"Button", L"Save", WS_VISIBLE | WS_CHILD, 121, 78, 60, 28, hwnd, (HMENU)ID_BUTTONB, NULL, NULL);
- }
- break;
- case WM_COMMAND:
- if (LOWORD(wParam) == ID_BUTTONB)
- {
- TCHAR buffAddress[1024];
- TCHAR buffPort[1024];
- TCHAR buffUsername[1024];
- GetWindowText(hwndEditC, buffAddress, 1024);
- GetWindowText(hwndEditD, buffPort, 1024);
- GetWindowText(hwndEditE, buffUsername, 1024);
- address = buffAddress;
- port = buffPort;
- username = buffUsername;
- //save config to file?
- showWindowB = false;
- SetActiveWindow(hWindowa);
- return 0;
- break;
- }
- break;
- case WM_CLOSE:
- showWindowB = false;
- SetActiveWindow(hWindowa);
- return 0;
- break;
- default:
- break;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- void CenterWindow(HWND hwnd)
- {
- RECT rc = { 0 };
- GetWindowRect(hwnd, &rc);
- int win_w = rc.right - rc.left;
- int win_h = rc.bottom - rc.top;
- int screen_w = GetSystemMetrics(SM_CXSCREEN);
- int screen_h = GetSystemMetrics(SM_CYSCREEN);
- SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w) / 2, (screen_h - win_h) / 2, 0, 0, SWP_NOSIZE);
- }
- void SendText(HWND chatBox, HWND sendBox)
- {
- TCHAR buff[1024];
- GetWindowText(sendBox, buff, 1024);
- messageInABottle = username + " : " + buff;
- SendMessage(sendBox, WM_SETTEXT, 0, (LPARAM)"");
- }
- LPWSTR stringToLPWSTR(const std::string& instr)
- {
- int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
- LPWSTR widestr = new WCHAR[bufferlen + 1];
- ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
- widestr[bufferlen] = 0;
- return widestr;
- }
- void AddMenus(HWND hwnd)
- {
- HMENU hMenubar;
- HMENU hMenu;
- HMENU hMenuB;
- hMenubar = CreateMenu();
- hMenu = CreateMenu();
- hMenuB = CreateMenu();
- AppendMenuW(hMenu, MF_STRING, IDM_CONFIGURE, L"&Configure");
- AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenu, L"&Settings");
- AppendMenuW(hMenuB, MF_STRING, IDM_ABOUT, L"About");
- AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenuB, L"&?");
- SetMenu(hwnd, hMenubar);
- }
- void *sendMessage(void *sock_desc)
- {
- while (TRUE)
- {
- if (sendCheck)
- {
- if (send(*((int *)sock_desc), messageInABottle.c_str(), sizeof(messageInABottle) + 1, 0) < 0)
- {
- OutputDebugStringW(L"\nSend fail.\n");
- }
- sendCheck = false;
- }
- }
- }
- void *receiveMessage(void *sock_desc)
- {
- char responce[2000];
- while (TRUE)
- {
- memset(responce, 0, sizeof responce);
- if (recv(*((int *)sock_desc), responce, sizeof(responce), 0) < 0)
- {
- OutputDebugStringW(L"\nRCV fail.\n");
- }
- //OutputDebugStringW(L"\nResponce : ");
- OutputDebugStringW(L"\n");
- OutputDebugStringW(stringToLPWSTR(responce));
- TCHAR buff[1024];
- GetWindowText(hwndEditA, buff, 1024);
- _tcscat_s(buff, 1024, "\r\n");
- _tcscat_s(buff, 1024, responce);
- SendMessage(hwndEditA, WM_SETTEXT, 0, (LPARAM)buff);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement