kalendara
September 29th, 2009, 09:25 AM
Hi!
First of all I would like to tell you, that I'm not a profressional programmer nor I want to be one, just have to deal with some stuff at work. And also, I have learned a bit C programming language, but now I'm dealing with C++ (i'm using Microsoft Visual C++ 2008 Express edition).
The main task was to write a code that inputs text in other softwares within the same PC. I started with inputing text in notepad and right now I'm about this far with the code, which works. It's good for a start but not really what I need. So, here is what I have
#include <windows.h>
#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HWND hWndNotepad = FindWindowA((LPCSTR)"Notepad", NULL);
if(!hWndNotepad)
{
cout << "Notepad not found!" << endl;
system("PAUSE");
return 0;
}
HWND hWndEdit = FindWindowExA(hWndNotepad, NULL, "Edit", NULL);
if(!hWndEdit)
{
cout << "Notepad not found!" << endl;
system("PAUSE");
return 0;
}
SetForegroundWindow(hWndEdit);
wchar_t *int = L"Hello!";
int size = wcslen(int);
for(int i=0; i<size; i++)
SendMessage(hWndEdit, WM_CHAR, int[i], 0);
system("PAUSE");
return 0;
}
What it does is that it checks whether notepad is running (i don't need to launch it from the code of my program), and ends if notepad isn't running. If notepad is running, then I get text "Hello!" entered in notepad. Good for start.
But I have several questions about how to modify the code so that I get this program to do what I need to do.
First thing I need to know is how do I enter text to be entered in notepad from my application (i'm making this a console application). For instance, a text appears in my console that is asking me to enter text and then after entering it, that text is typed in notepad.
Second, is there a possibility to send any keystrokes to notepad? I have read about SendKeys (Send, SendWait) but didn't find anything useful about how to use it. Here, for instance, I need to send ENTER after the text I send to notepad. Is it even possible in MS Visual C++?
Also, if I need to send only numbers (integers) to notepad, will the structure of a code be simplier? Because I will need to make that software to send numbers (integers or floating point) in a cycle (i know how to make those in C), for example send integers from 400 to 800 with a step 5 and press enter after each cycle (not to make it a new line, but to press ENTER. i believe there is a difference)
And finally, is there a possibility to make program to access sub-menus and to enter numbers there? For example, in notepad it could be like moving through menus "Edit" then choose "Go To ..." and enter number (integer) there and press ENTER. It is possible
Hope I'm not making too much questions, but just can't find much help about this stuff which I really need.
Also, if it's not possible to make SendKeys work in MS Visual C++ (i've read something about it), maybe I need to write this program in some other language? Suggestions welcome about which language to use to make this program as easy as possible.
First of all I would like to tell you, that I'm not a profressional programmer nor I want to be one, just have to deal with some stuff at work. And also, I have learned a bit C programming language, but now I'm dealing with C++ (i'm using Microsoft Visual C++ 2008 Express edition).
The main task was to write a code that inputs text in other softwares within the same PC. I started with inputing text in notepad and right now I'm about this far with the code, which works. It's good for a start but not really what I need. So, here is what I have
#include <windows.h>
#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HWND hWndNotepad = FindWindowA((LPCSTR)"Notepad", NULL);
if(!hWndNotepad)
{
cout << "Notepad not found!" << endl;
system("PAUSE");
return 0;
}
HWND hWndEdit = FindWindowExA(hWndNotepad, NULL, "Edit", NULL);
if(!hWndEdit)
{
cout << "Notepad not found!" << endl;
system("PAUSE");
return 0;
}
SetForegroundWindow(hWndEdit);
wchar_t *int = L"Hello!";
int size = wcslen(int);
for(int i=0; i<size; i++)
SendMessage(hWndEdit, WM_CHAR, int[i], 0);
system("PAUSE");
return 0;
}
What it does is that it checks whether notepad is running (i don't need to launch it from the code of my program), and ends if notepad isn't running. If notepad is running, then I get text "Hello!" entered in notepad. Good for start.
But I have several questions about how to modify the code so that I get this program to do what I need to do.
First thing I need to know is how do I enter text to be entered in notepad from my application (i'm making this a console application). For instance, a text appears in my console that is asking me to enter text and then after entering it, that text is typed in notepad.
Second, is there a possibility to send any keystrokes to notepad? I have read about SendKeys (Send, SendWait) but didn't find anything useful about how to use it. Here, for instance, I need to send ENTER after the text I send to notepad. Is it even possible in MS Visual C++?
Also, if I need to send only numbers (integers) to notepad, will the structure of a code be simplier? Because I will need to make that software to send numbers (integers or floating point) in a cycle (i know how to make those in C), for example send integers from 400 to 800 with a step 5 and press enter after each cycle (not to make it a new line, but to press ENTER. i believe there is a difference)
And finally, is there a possibility to make program to access sub-menus and to enter numbers there? For example, in notepad it could be like moving through menus "Edit" then choose "Go To ..." and enter number (integer) there and press ENTER. It is possible
Hope I'm not making too much questions, but just can't find much help about this stuff which I really need.
Also, if it's not possible to make SendKeys work in MS Visual C++ (i've read something about it), maybe I need to write this program in some other language? Suggestions welcome about which language to use to make this program as easy as possible.