Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma region Includes
- #include <iostream> // output to console
- #include <string>
- #include <thread>
- #include <windows.h>
- #pragma endregion
- #pragma region pragma / defs
- #pragma warning(disable: 4996)
- #define __SLEEP_TIME_MS 1000
- #pragma endregion
- #pragma region Functions
- void PassToNotepad(const char* str, HWND notepad, ...) { // sends string to notepad
- va_list ap;
- HWND edit;
- char buf[256];
- va_start(ap, str); // get the first argument
- vsprintf(buf, str, ap); // format the string
- va_end(ap); // clean up
- strcat(buf, ""); // add null terminator
- edit = FindWindowEx(notepad, NULL, ("EDIT"), NULL); // get edit control handle
- SendMessageA(edit, EM_REPLACESEL, 0, (LPARAM)buf); // send string to edit control
- }
- void PassToNotepad(const char* str, HWND notepad, bool newline, ...) { // sends string to notepad
- va_list ap;
- HWND edit;
- char buf[256];
- va_start(ap, str); // get the first argument
- vsprintf(buf, str, ap); // format the string
- va_end(ap); // clean up
- strcat(buf, ""); // add null terminator
- edit = FindWindowEx(notepad, NULL, ("EDIT"), NULL); // get edit control handle
- SendMessageA(edit, EM_REPLACESEL, 0, (LPARAM)buf); // send string to edit control
- if (newline)
- SendMessageA(edit, EM_REPLACESEL, 0, (LPARAM)"\r"); // send string to edit control
- }
- bool isNotepadReady(HWND* notepad) { // checks if notepad is ready
- HWND res;
- res = FindWindow(NULL, ("Untitled - Notepad")); // get notepad handle
- if (!res)
- res = FindWindow(NULL, ("*Untitled - Notepad")); // get notepad handle
- *notepad = res; // set notepad handle
- return res != NULL; // return true if notepad is ready
- }
- void WriteOnCurrentLine(int X = 1, int Y = 1) {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); // get console info
- for (int i = 0; i < X; i++)
- csbi.dwCursorPosition.X++;
- for (int i = 0; i < Y; i++)
- csbi.dwCursorPosition.Y--;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), csbi.dwCursorPosition); // set cursor position
- }
- #pragma endregion
- HWND global;
- int main() {
- while (true) {
- if (!isNotepadReady(&global)) { // if notepad is not ready
- std::cout << "Notepad is not ready... Waiting..." << std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(__SLEEP_TIME_MS)); // wait
- }
- else { // if notepad is ready
- std::string user_input;
- std::cout << ">";
- std::getline(std::cin, user_input); // get user input
- if (user_input.empty()) { // if user input is empty (means its a new line (user pressed enter))
- PassToNotepad("", global, true);
- WriteOnCurrentLine(); // write on current line
- std::cout << " (Sent newline to notepad)" << std::endl;
- }
- else {
- PassToNotepad(user_input.c_str(), global);
- WriteOnCurrentLine(user_input.length() + 1); // write on current line but add the length of the user input + 1 (for the ">")
- std::cout << " - (Sent to notepad)" << std::endl;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement