Important alert: (current site time 7/15/2013 5:44:21 PM EDT)
 

VB icon

[ A Program That *Indirectly* Write Text To Notepad ]

Email
Submitted on: 1/28/2005 9:23:14 PM
By: Brenton Andrew Saunders 
Level: Intermediate
User Rating: By 8 Users
Compatibility: Microsoft Visual C++
Views: 10933
(About the author)
 
     A program that writes text to Notepad. The code is fully commented and easy to understand. PLEASE vote for it or comment for it. It takes very little time to comment and it is very appreciated. If you can't vote, the least you can do is comment on it. Tell me if it is bad or good.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :[ A Program That *Indirectly* Write Text To Notepad ]
//**************************************
windows.h
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: [ A Program That *Indirectly* Write Text To Notepad ]
// Description:A program that writes text to Notepad. The code is fully commented and easy to understand. PLEASE vote for it or comment for it. It takes very little time to comment and it is very appreciated. If you can't vote, the least you can do is comment on it. Tell me if it is bad or good.
// By: Brenton Andrew Saunders
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8871&lngWId=3//for details.//**************************************

#include <windows.h> // Very important. Otherwise this whole thing wouldn't work!
// WriteToNotepad()
// Indirectly writes text to Notepad
// Returns TRUE or FALSE depending on success
BOOL WriteToNotepad(LPCTSTR lpszText)
{
	HWND hwndNotepad; // A handle to the Notepad window
	HWND hwndEdit;// A handle to Notepad's work area
	// Where's Notepad?
	hwndNotepad = FindWindow("Notepad", NULL);
	// Did we find it?
	if(!hwndNotepad)
	{
		// Guess not
		// Try to run it from the Windows directory
		WinExec("Notepad", SW_SHOWNORMAL);
		// Wait a few milliseconds (If your computer is slow it may take a while)
		Sleep(100);
		// Find it again
		hwndNotepad = FindWindow("Notepad", NULL);
		// Did we find it this time?
		if(!hwndNotepad)
		{
			// Nope.
			// Return FALSE indicating failure :(
			return FALSE;
		}
	}
	// Good, we found Notepad.
	// Now lets find it's the edit control
	// that makes up it work area
	// We'll use FindWindowEx this time because it is used to find
	// child windows
	
	hwndEdit = FindWindowEx(hwndNotepad, NULL, "Edit", NULL);
	// Success?
	if(!hwndEdit)
	{
		// Oops. Busted.
		// Nothing we can do here. Return FALSE indicating failure (:
	
		return FALSE;
	}
	// Good, we found the edit control
	// Now we're going to loop through the characters in the lpszText (defined by user)
	// and display them individually
	for(int i = 0; i < lstrlen(lpszText); i ++)
	{
		LRESULT lRet; // We use this to recieve the return value of SendMessage (just in case it fails)
		// Write the characters one by one by sending a WM_CHAR message to
		// the edit control
		lRet = SendMessage(hwndEdit, WM_CHAR, lpszText[i], NULL);
		
		// Any problems?
		if(!lRet)
		{
			// Nothing we can really do. There's no point in breaking
			// the loop, so ignore it.
		}
	}
	// Good! Everything worked out.
	// Return TRUE indicating success :)
	return TRUE;
}
// main()
// Marks the beginning and end of program execution
// No return value
VOID main()
{
	// Let's give it a try
	// Display "Hello World!" to the user via Notepad
	WriteToNotepad("Hello World!\n");
	/*
	The only drawback of this function is that in order to see what
	was written to Notepad, the user must manually go at bring the
	window to the foreground. While there is a function that can do
	this automatically (SetForegroundWindow()), it won't work because
	the console must be in the foreground in order to, well, RUN!
	
	Note:
	This function works just as if you were to use cout<< or printf()
	meaning in order to start a new line, you must include a '\n' character
	in the text (as shown above). To delete text, use '\b'
	*/
}


Other 46 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/22/2005 3:28:51 AMThomas Natale

Nice! I see you used this in your shooter game! 5 Globes!
(If this comment was disrespectful, please report it.)

 
8/15/2006 5:06:40 AMDeminish

thasnks, im an uber beginner so i didnt really understand but i see how you have commented and explained everything nicely. GJ!
(If this comment was disrespectful, please report it.)

 
8/27/2006 10:55:58 AMEric Pierce

Great commenting. Helps a lot in learning the language. Thanks!
(If this comment was disrespectful, please report it.)

 
8/27/2006 12:01:51 PMMatt. Stevens

Excellent code, great idea, nicely commented...I love it!
(If this comment was disrespectful, please report it.)

 
8/28/2006 5:25:32 AMAlexMark

Yes, good.

But try to add this to your code:


LRESULT lRet; // We use this etc..
// Write the characters etc...
lRet = SendMessage(hwndEdit, WM_CHAR, lpszText[i], NULL); Sleep(100); //Sleep the app for a while <--
// Any problems?
if(!lRet) { // Nothing we can really do etc.
}



You will have a better effect ;D
(If this comment was disrespectful, please report it.)

 
5/14/2008 6:45:45 AMSidhaanth

thnks..gr8 code
(If this comment was disrespectful, please report it.)

 
8/2/2008 5:40:58 AMKarthikeyan

Thank you very much
It helps too much form my studies
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.