VB icon

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

Email
Submitted on: 1/7/2015 3:51:00 AM
By: Brenton Andrew Saunders (from psc cd)  
Level: Intermediate
User Rating: By 10 Users
Compatibility: Microsoft Visual C++
Views: 2819
 
     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!
				
//**************************************
// 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 (from psc cd)
//**************************************

#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 5 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


 There are no comments on this submission.
 

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.