Archived

This topic is now archived and is closed to further replies.

tHiSiSbOb

Putting line breaks in win32 edit box

Recommended Posts

I am trying to put some text in a message box in multiple lines by putting \n where I want the new line. This isn''t working though...All I get is a little black rectangle. How can I put the line breaks in??

Share this post


Link to post
Share on other sites
Hi tHiSiSbOb,

I think I have the answer to your question.

Here is how it goes, nice and simple. First, make sure your edit box supports multi-line. (ES_MULTILINE):

#include <windows.h>

void createEditBox(HWND hWnd, HWND hControl, HINSTANCE hInstance,
char tempText[255], int x, int y, int width, int height, int identifier ) {
hControl = CreateWindow("edit", tempText, WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL|ES_MULTILINE,
x, y, width, height, hWnd, (HMENU)identifier, NULL, NULL);
}

// In main code

#define IDEDIT 110
HWND hEdit;
HINSTANCE hInstance;

createEditBox(hWnd, hEdit, hInstance, "My Edit Box", 24, 65, 100, 250, IDEDIT);


Now, as you might see above createEditBox() has ES_MULTILINE in the CreateWindow() code. Also I made sure that the height of the edit box was large enough to fit multilines in there without having to scroll down and the alike. Now to also make sure you can add more lines to the edit box I'll paste a code that lets you append text to your existing text box:

void appendText( HWND hwndEditBox, char *lpszText ) {
int iLineLen;
int iLineCount;
int iCurTextLen;

if( SendMessage( hwndEditBox, EM_GETLINECOUNT, 0, 0 ) > 128 ) {
iLineLen = SendMessage( hwndEditBox, EM_LINELENGTH, 0, 0 );
SendMessage( hwndEditBox, EM_SETSEL, 0, iLineLen+2 );
SendMessage( hwndEditBox, EM_REPLACESEL, FALSE, (long)"" );
}

iCurTextLen = SendMessage( hwndEditBox, WM_GETTEXTLENGTH, 0, 0);
SendMessage( hwndEditBox, EM_SETSEL, iCurTextLen, iCurTextLen );
SendMessage( hwndEditBox, EM_REPLACESEL, FALSE, (long)lpszText );

iLineCount = SendMessage( hwndEditBox, EM_GETLINECOUNT, 0, 0);
SendMessage( hwndEditBox, EM_LINESCROLL, 0, iLineCount );
}


Now all you have to do is appendText( hEdit, "My Text..." ); and it will automatically append the text with whatever is already there. Now back to your question about adding a new line without seeing that big black block, I think I have the answer.

Dont just use "\n", use "\r\n". For example:

appendText( hEdit, "My Text...\r\n" );


I hope this helps!,
[BDS]StackOverflow

[edited by - BlueDev on March 22, 2004 2:18:15 PM]

Share this post


Link to post
Share on other sites