Text Handling in GDI

Started by
1 comment, last by Ozmo 21 years, 10 months ago
I am making a client that will connect to a server and retrieve data simmilar to a telnet client. I do this using Windows GDI. I was wondering how I could make it so that I could highlight the text that I display as well as to copy it. I currently use TextOut(). I was also wondering how to best make a text engine with GDI in general. What i mean is should i use a linked list or vector to hold all of the lines, how do I best redraw the text after the window is covered up, and how to best set up a way to be able to scroll back to see past text that scroll up off the screen. Thanks in advance!
Uhhhhh........ Forget the quantinum torpedoes! Full speed ahead!
Advertisement
these are all things you will have to expierement with. generally speaking, linked lists that house dynamically allocated arrays (ie vectors) of lines are the best space wise. a circular buffer (vector) could also be used, but requires more complex handling of drawing text since you have to parse through the text finding the new lines.

hilight is as simple as mapping the mouse to the text window to actual text. one way is to use a fixed width font and use rects that bound each character on the screen. then its a matter of knowing the start position and the length (positive or negative) of the selection. with this info you can handle copying. cut is slightly more diffiuclt since you must remove the text and then "compact" the buffer so the void is filled. look up clipboard functions for how to handle placing data in the clipboard.

scroling is just a matter of keeping track of where you are in your buffer, and moving the offset of where you draw the buffer. the simplest approach could be using the pgup and pgdn keys to scroll the buffer one line. more complex approaches include creating your own scrollbar which handles movement (ie changing the draw offset) by using the percentage of how far the thumb (ie the box thing you move) is within the scroll bar.

drawing should be done only during WM_PAINT. any updates to the buffer should call InvalidateRect(), UpdateWindow() to force a redraw if the window. an optimization could be using the paintstruct rect to draw only the text that was drawn over. maybe even have a backbuffer which is the same size as the window in which you pre draw the text when it changes, then only blit that to the window. this helps reduce flicker that may occer when updating the window, as well as be quicker then redrawing the text each time. though at the cost of having to maintain the buffer and ensure that you handle it properly (ie delete objects when required, properly resize the buffer.

test your code throughly and make sure you dont have memory leaks. any time you use selectobject() save the handle it returns so you can selectobject() it back when you are done with the resource you used with selectobject().

example:

oldHandle = SelectObject(MyDC, MyHandle);
// do stuff
SelectObject(MyDC, OldHandle);

// during deletion of objects you can
DeleteObject(MyHandle);

read the forum for more examples.
I think that you can simply use Edit Control or Rich Edit Control to display your text.

This topic is closed to new replies.

Advertisement