Пример #1
0
LRESULT CALLBACK EditBoxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_KEYDOWN:
		{
			if (LOWORD(wParam) == VK_RETURN)
			{
				if (serverConnection)
				{
					static char buf[50000];
					SendMessage(editBoxHwnd, WM_GETTEXT, sizeof buf, reinterpret_cast<LPARAM>(buf));
					buf[(sizeof buf) - 1] = '\0';

					commandHistory.push_back(buf);
					currentCommandHistoryPos = commandHistory.end();

					Words words;
					tokenize(buf, words);

					serverConnection->sendRequest(words);

					SendMessage(editBoxHwnd, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(""));
				}

				return TRUE;
			}
			else if (LOWORD(wParam) == VK_UP)
			{
				if (currentCommandHistoryPos != commandHistory.begin())
				{
					currentCommandHistoryPos--;
					const char* command = currentCommandHistoryPos->c_str();
					SendMessage(editBoxHwnd, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(command));
					size_t commandLength = strlen(command);
					SendMessage(editBoxHwnd, EM_SETSEL, commandLength, commandLength);
				}
				return TRUE;
			}
			else if (LOWORD(wParam) == VK_DOWN)
			{
				if (currentCommandHistoryPos != commandHistory.end())
				{
					currentCommandHistoryPos++;
					const char* command = (currentCommandHistoryPos != commandHistory.end()) ? currentCommandHistoryPos->c_str() : "";
					SendMessage(editBoxHwnd, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(command));
					size_t commandLength = strlen(command);
					SendMessage(editBoxHwnd, EM_SETSEL, commandLength, commandLength);
				}
				return TRUE;
			}
		}
		break;
	}

	return CallWindowProc(editBoxOriginalWndProc, hWnd, message, wParam, lParam);
}
Пример #2
0
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
	currentCommandHistoryPos = commandHistory.end();

	static TCHAR szWindowClass[] = _T("win32app");
	static TCHAR szTitle[] = _T("RCON Console");

	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = szWindowClass;
	wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

	if (!RegisterClassEx(&wcex))
	{
		MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Error"), NULL);

		return 1;
	}

	::hInstance = hInstance; // Store instance handle in our global variable

	// The parameters to CreateWindow explained:
	// szWindowClass: the name of the application
	// szTitle: the text that appears in the title bar
	// WS_OVERLAPPEDWINDOW: the type of window to create
	// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
	// 500, 100: initial size (width, length)
	// NULL: the parent of this window
	// NULL: this application does not have a menu bar
	// hInstance: the first parameter from WinMain
	// NULL: not used in this application
	RECT windowRect = { 0, 0, InitialWindowWidth, InitialWindowHeight };
	AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
	HWND hWnd = CreateWindow(
		szWindowClass,
		szTitle,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		windowRect.right - windowRect.left, windowRect.bottom - windowRect.top,
		NULL,
		NULL,
		hInstance,
		NULL
	);
	if (!hWnd)
	{
		MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Error"), NULL);

		return 1;
	}

	// The parameters to ShowWindow explained:
	// hWnd: the value returned from CreateWindow
	// nCmdShow: the fourth parameter from WinMain
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	serverConnection = new ServerConnectionThread(hWnd);

	// Main message loop:
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	serverConnection->requestQuit();

	delete serverConnection;

	return (int) msg.wParam;
}