4 Replies - 13937 Views - Last Post: 16 February 2009 - 09:12 PM Rate Topic: -----

#1 yamaki308  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 06-February 09

Editbox setting in Win32 api

Posted 16 February 2009 - 02:44 PM

I'm writing a simple chat program using winsock 2.0 and win32 api. I'm trying to use the read-only edit control to print texts line by line like this:

"blah, blah"
"blah, blah"
.
.
.
"blah, blah, blah"

For the edit control to type texts I'm using these following styles:
WS_VISIBLE
WS_BORDER
WS_CHILD
WS_VSCROLL
ES_MULTILINE

// create an edit box to type messages
editBoxHandle = CreateWindow(_T("EDIT"),
						NULL,
						WS_VISIBLE|WS_BORDER|
						WS_CHILD|WS_VSCROLL|
						ES_MULTILINE,
						10,350,
						200,50,
						mWnd,
						0,
						hInstance,
						0);



For the edit control to print texts I'm using these following styles:
WS_VISIBLE
WS_BORDER
WS_CHILD
WS_VSCROLL
ES_MULTILINE
ES_READONLY

// create a message-output box
msgBoxHandle = CreateWindow(_T("EDIT"),
						NULL,
						WS_VISIBLE|WS_BORDER|
						WS_CHILD|WS_VSCROLL|
						ES_MULTILINE|ES_READONLY,
							10,10,
						295,330,
						mWnd,
						0,
						hInstance,
						0);



I try to look into msdn.microsoft.com for edit control references but it doesn't help me. I also look into other posts here in this site but it doesn't help me as well. Here is a snippet of my code that displays a text on the read-only edit control box:

// get length of the text from the edit box
int len = GetWindowTextLength(editBoxHandle);
											
if(len > 0)
{
	   char * buffer;

	// allocate memory of the buffer from the heap
	// and fill it with zeroes. this is similiar to
	// the c-style function, calloc()
		buffer = (char *)GlobalAlloc(GPTR, len + 1);

	// get the text from the edit box
	SendMessage(editBoxHandle,WM_GETTEXT,
				(WPARAM)len + 1, (LPARAM)buffer);

	// set the text to the message box
	SendMessage(msgBoxHandle, 
				WM_SETTEXT,
				0,
				(LPARAM)buffer);
						
	// deallocate memory of the buffer
	GlobalFree((HANDLE)buffer);
	buffer = NULL;
}



This code is fine but it overwrites the last text with the new one. Is there a solution to this problem? Also, how can I print messages to the read-only edit control by pressing the ENTER key.

Language: C++
IDE: Visual Studio 2005
API: Winsock 2 and Win32

This post has been edited by yamaki308: 16 February 2009 - 02:57 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Editbox setting in Win32 api

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Editbox setting in Win32 api

Posted 16 February 2009 - 04:50 PM

I'm guessing what you want to do is append the text in editBoxHandle to the text in msgBoxHandle.

The best way I can think of doing that is by, first, getting the text from msgBoxHandle and storing it in a string (msgText):

// get length of the text from the edit box
int len = GetWindowTextLength(editBoxHandle);
											
if(len > 0)
{
	   char * buffer;
	   char *msgText;
		int msgTextLen;

	// get the length of the text in the message box
	msgTextLen = GetWindowTextLength(msgBoxHandle);

	// allocate memory to store the text of the message box
	// plus the edit box text that is going to be appended to it
	msgTextLen += len;

	msgText = (char *)GlobalAlloc(GPTR, msgTextLen + 1);

   // get the text from the message box
	SendMessage(msgBoxHandle,WM_GETTEXT,
				(WPARAM)msgTextLen + 1, (LPARAM)msgText);


	// allocate memory of the buffer from the heap
	// and fill it with zeroes. this is similiar to
	// the c-style function, calloc()
		buffer = (char *)GlobalAlloc(GPTR, len + 1);

	// get the text from the edit box
	SendMessage(editBoxHandle,WM_GETTEXT,
				(WPARAM)len + 1, (LPARAM)buffer);
	//...
.

Then, you can append the edit box text (buffer) to the message box text (msgText):

//...
	// add the edit box text to the message box text string
	lstrcat(msgText, buffer);

	//...
,

and send the next message box text string to the message box:

	// set the text to the message box
	SendMessage(msgBoxHandle, 
				WM_SETTEXT,
				0,
				(LPARAM)msgText);
						
	// deallocate memory of the buffers
	GlobalFree((HANDLE)buffer);
	GlobalFree((HANDLE)msgText);
	buffer = NULL;
	msgText = NULL;
}
.
Was This Post Helpful? 0
  • +
  • -

#3 yamaki308  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 06-February 09

Re: Editbox setting in Win32 api

Posted 16 February 2009 - 06:53 PM

It works, but it prints them on the same line. What I want is to print the texts line after line.
Was This Post Helpful? 0
  • +
  • -

#4 yamaki308  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 06-February 09

Re: Editbox setting in Win32 api

Posted 16 February 2009 - 08:05 PM

I figured it out now! I have to use this following code to print the text line after line:

SendMessage(msgBoxHandle, EM_REPLACESEL,0,(LPARAM)buffer);
SendMessage(msgBoxHandle, EM_REPLACESEL,0,(LPARAM)"\r\n");


This post has been edited by yamaki308: 16 February 2009 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Editbox setting in Win32 api

Posted 16 February 2009 - 09:12 PM

That definitely a better way! I'm going to have to keep that in mind.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1