sending data to opened notepad file in desktop

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

can mfc application, send text data to opened notepad file in desktop?(live
transfer of data) . can anybody help
 
Sandy said:
can mfc application, send text data to opened notepad
file in desktop?(live transfer of data) . can anybody help

I'm not sure if I understand what the crux of your question is. Is it how to
manipulate Notepad or how to do data transfer?

The former question is easy to answer. This little hack will do the job. It
is a command line application whose first parameter is the title (caption)
of the instance of Notepad to which you want to deliver the text and the
second is the text itself. If either contains spaces you'll need to quote
it.

#include <windows.h>
#include <iostream>

int main(int argc, char **argv)
{
HWND hNotepad, hEdit;

if ( argc != 3 )
{
std::cout << "Enter xxx \"title\" \"text\"" << std::endl;
std::cout << " where title is the caption of Notepad's window" <<
std::endl;
std::cout << " and text is the text you want to insert" << std::endl;
}

hNotepad = FindWindow("Notepad", argv[1]);

if ( hNotepad == 0 )
{
std::cout << "Failed to find Notepad's main window" << std::endl;
exit(0);
}

hEdit = FindWindowEx(hNotepad, NULL, "edit", NULL);

if ( hEdit == 0 )
{
std::cout << "Failed to find Notepad's edit control" << std::endl;
exit(0);
}

SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM) argv[2]);

return 0;
}

If you are asking about live data transfer in general then the answer could
(and has) filled entire books. The example above uses Windows to do the
heavy lifting - its WM_SETTEXT message is designed to move character data
into a Window. If you want to move something other than text to something
other than a window post some more details for some ideas.

Regards,
Will
 
Dear William

1. Suppose "abc.txt"(notepad) file is already open in desktop.
2. My VC++6 mfc, application wants to write the data in abc.txt.

example...when u open abc.txt then... in notepad there are 5line of text ,
then new data should be display in 6th line... that 6th line coming in
notepad is seen by user. that 6th line data is coming from your vc++ appln.

Thanks



William DePalo said:
Sandy said:
can mfc application, send text data to opened notepad
file in desktop?(live transfer of data) . can anybody help

I'm not sure if I understand what the crux of your question is. Is it how to
manipulate Notepad or how to do data transfer?

The former question is easy to answer. This little hack will do the job. It
is a command line application whose first parameter is the title (caption)
of the instance of Notepad to which you want to deliver the text and the
second is the text itself. If either contains spaces you'll need to quote
it.

#include <windows.h>
#include <iostream>

int main(int argc, char **argv)
{
HWND hNotepad, hEdit;

if ( argc != 3 )
{
std::cout << "Enter xxx \"title\" \"text\"" << std::endl;
std::cout << " where title is the caption of Notepad's window" <<
std::endl;
std::cout << " and text is the text you want to insert" << std::endl;
}

hNotepad = FindWindow("Notepad", argv[1]);

if ( hNotepad == 0 )
{
std::cout << "Failed to find Notepad's main window" << std::endl;
exit(0);
}

hEdit = FindWindowEx(hNotepad, NULL, "edit", NULL);

if ( hEdit == 0 )
{
std::cout << "Failed to find Notepad's edit control" << std::endl;
exit(0);
}

SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM) argv[2]);

return 0;
}

If you are asking about live data transfer in general then the answer could
(and has) filled entire books. The example above uses Windows to do the
heavy lifting - its WM_SETTEXT message is designed to move character data
into a Window. If you want to move something other than text to something
other than a window post some more details for some ideas.

Regards,
Will
 
Hello !

There is a small misunderstanding. Writing data to 'abc.txt' is not the same
thing as writing data to Notepad's edit control.

To hack Notepad, use the code piece that William posted. But before sending
WM_SETTEXT, send WM_GETTEXT, which will retrieve the currently written text
on Notepad's edit control. Then to this local string, append the results you
want to write (the 6th line) and use WM_SETTEXT to update changes in
Notepad. This result is an effect of 'new line appearing in Notepad',
although what you actually did was rewrite the entire contents of the edit
control.

Note that Notepad will not save the changes into 'abc.txt' unless you
command it seperately (via UI or sending another message) to save the file.
The process outlined above will only update the text displayed in Notepad's
edit control. It will not affect the actual file unless changes are
explicitly saved.

-Antti Keskinen


Sandy said:
Dear William

1. Suppose "abc.txt"(notepad) file is already open in desktop.
2. My VC++6 mfc, application wants to write the data in abc.txt.

example...when u open abc.txt then... in notepad there are 5line of text
,
then new data should be display in 6th line... that 6th line coming in
notepad is seen by user. that 6th line data is coming from your vc++
appln.

Thanks



William DePalo said:
Sandy said:
can mfc application, send text data to opened notepad
file in desktop?(live transfer of data) . can anybody help

I'm not sure if I understand what the crux of your question is. Is it how
to
manipulate Notepad or how to do data transfer?

The former question is easy to answer. This little hack will do the job.
It
is a command line application whose first parameter is the title
(caption)
of the instance of Notepad to which you want to deliver the text and the
second is the text itself. If either contains spaces you'll need to quote
it.

#include <windows.h>
#include <iostream>

int main(int argc, char **argv)
{
HWND hNotepad, hEdit;

if ( argc != 3 )
{
std::cout << "Enter xxx \"title\" \"text\"" << std::endl;
std::cout << " where title is the caption of Notepad's window" <<
std::endl;
std::cout << " and text is the text you want to insert" << std::endl;
}

hNotepad = FindWindow("Notepad", argv[1]);

if ( hNotepad == 0 )
{
std::cout << "Failed to find Notepad's main window" << std::endl;
exit(0);
}

hEdit = FindWindowEx(hNotepad, NULL, "edit", NULL);

if ( hEdit == 0 )
{
std::cout << "Failed to find Notepad's edit control" << std::endl;
exit(0);
}

SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM) argv[2]);

return 0;
}

If you are asking about live data transfer in general then the answer
could
(and has) filled entire books. The example above uses Windows to do the
heavy lifting - its WM_SETTEXT message is designed to move character data
into a Window. If you want to move something other than text to something
other than a window post some more details for some ideas.

Regards,
Will
 
Sandy said:
can mfc application, send text data to opened notepad file in desktop?(live
transfer of data) . can anybody help
to be most general, why don't you simply offer to copy the text to the
clipboard and the user can do whatever they want with it?

/steveA
 
Hi!

Writing to opened (.txt file) notepad is solved.
PROBLEM : when vc++ application, is writing data to opened notepad, then
small
------------ rectangle are coming in .txt file & "\n" is also not working.

Using following code:

::SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);
CString temp = textData;
temp += "\n";
temp += "MFC";
sprintf(textData,"%s",temp);
::SendMessage(hEdit, WM_SETTEXT, 0,(LPARAM)textData);
 
Sandy said:
Writing to opened (.txt file) notepad is solved.
PROBLEM : when vc++ application, is writing data to opened notepad, then
small
------------ rectangle are coming in .txt file

Those are likely to be "control" codes
"\n" is also not working.

Try using

"\r\n"

instead.

Regards,
Will
 
Hi!

If more than one notepad is opened in desktop.. then its work for "top"
notepad only
Want to ... write a data in specfic .txt file. for example abc.txt, but if
"top.txt" is at top at desktop then, the data is written in top.txt.... not
in abc.txt.

code: working for only when one notepad is opend / for top notepad at desktop
HWND hNotepad, hEdit;
hNotepad = ::FindWindow("Notepad",NULL);
hEdit = ::FindWindowEx(hNotepad, NULL,"edit", NULL);

int iLength = (int)::SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
::SendMessage(hEdit, WM_GETTEXT, iLength+1, (LPARAM)textData);

CString temp = textData;
temp += "\r\n";
temp += "MFC";
::SendMessage(hEdit, WM_SETTEXT,0,(LPARAM)textData);

I had try..... ( Not working.. if more then one / abc.txt is not at top in
desktop)
hNotepad = ::FindWindow("Notepad","abc.txt");
hNotepad = ::FindWindow("Notepad","abc");

HWND FindWindow( LPCTSTR lpClassName, LPCTSTR lpWindowName); //syntax
If the lpWindowName parameter is not NULL, FindWindow calls the
GetWindowText function to retrieve the window name for comparison.

GetWindowText cannot retrieve the text of a control in another application.

How to select specfic txt file... if more then one is opened!
I want to write the data in specfic file that i supply in program
Help Me Please
 
Sandy said:
If more than one notepad is opened in desktop.. then its work for "top"
notepad only
Want to ... write a data in specfic .txt file. for example abc.txt, but if
"top.txt" is at top at desktop then, the data is written in top.txt....
not
in abc.txt.

OK.

To answer the question you asked, what you need to do is to change this
line:

hNotepad = ::FindWindow("Notepad",NULL);


to

hNotepad = ::FindWindow("Notepad","abc.txt - Notepad");

Notepad is little more than a frame window with an edit control child and a
menu. You might want to think about using your own frame and edit control so
that you could maintain an association between window handles and file names
and note have to use FindWindow().

Regards,
Will
 
Hi

In the same discussion... Can i get current cursor position in open notepad
hNotepad = ::FindWindow("Notepad","abc.txt - Notepad");

Bcoz i want insert data at current cursor position..
Example; if notepad file having 10 line and my cursor is at 5 line... can i
get it?
 
Sandy said:
In the same discussion... Can i get current cursor position in open
notepad
hNotepad = ::FindWindow("Notepad","abc.txt - Notepad");

Bcoz i want insert data at current cursor position..
Example; if notepad file having 10 line and my cursor is at 5 line... can
i
get it?

Notepad contians an edit control. Edit controls provide messages both to
report on and change the current selection.

Check the docs for EM_GETSEL. It gets the "selection range". The end of the
range is where the caret (what you call cursor) is. You can use
EM_LINEFROMCHAR and the end of the selection range to get the line number
containing the caret. EM_LINEINDEX can be used to get the first character of
a given line. Subtracting the two positions will yield the offset from the
start of the line.

You should either post follow-up in a UI group or check the docs for the
messages that edit controls support. The messages that are specific to edit
controls have names that begin with "EM_"

Regards,
Will
 
Back
Top