Click to See Complete Forum and Search --> : How to display a text file EASY
kuijer
May 27th, 1999, 05:14 AM
Hi,
I'm trying to show a (only one) text file in a window in a procedure oriented C-style program.
I've created a window with the registerclass/createclass functions, and i have a lowlevel file-read routine. I only need to put the text in de window-client area (?).
How can i do this? Is there a beter/other way?
Thnx,
Paul Kuijer
ric
May 27th, 1999, 07:04 AM
why don't you use TextOut()
spg
May 27th, 1999, 04:01 PM
One way ... but not the best way
//include this header file
#include <fstream.h>
void CYourProjectNameView::OnDraw(CDC* pDC)
{
ifstream infile(m_filename, ios::nocreate); //open the file
char current_line[256]; //line that is displayed to the screen
int current_y = 0; //starting y position
if(!infile) //does the file exist???
{
MessageBox("Cannot Open "+ m_filename);
return;
}
//file exist
//read a line at a time till end of file
while(! infile.eof())
{
infile.getline(current_line, 256);//get next line from file
pDC->TextOut(0, current_y, current_line);//display current line
current_y = current_y + 14;//get next y coordinate
}
infile.close();//close file
}
Paul McKenzie
May 27th, 1999, 08:56 PM
The easy part is displaying the text initially. That can be done with TextOut() or DrawText(). The hard part is:
a) if the text is more than what can fit in the window. You will need to handle scrollbars and scrolling.
b) Repainting. If another window gets placed on top of your output window, the text has to be repainted when the "foreign" window is moved or hidden. Also, if the user moves the output window so that it is partially outside of the screen boundaries, you will have to repaint if the user moves the window back within view. You will need to handle the WM_PAINT message for this.
There really is no "easy" way to do this with your own window.
Solution:
Since I'm assuming that you are doing 'C' API programming as opposed to MFC, one solution would be to create a giant multi-line edit window. You do this by creating a window (as you already have done), but use the predefined "EDIT" class type. Also make sure that the ES_MULTILINE flag is set for the class style. With this, you don't worry about the painting or the scrolling, since the "EDIT" window class handles all of the details. All you have to do is call SendMessage(hWnd, WM_SETTEXT, 0, ptrToText), where ptrToText would be an LPSTR that holds all of the text that you want to assign to the window (hWnd).
Hope this helps.
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.