Home » C++ Programming

Using WinAPI, how can you pass formatted (new-line) characters to an "Edit" class child Window, pref

First I apologize if this is not the best forum for this, but I am using the VC++ software, and this is a Windows-programming question--I didn't see a place for coding in basic WinAPI...

Information:  I'm creating a patcher/installer for a current project.  Like most of these applications, mine has a window in the center of the app for displaying download/patch, etc. information as it runs through the process.  Everything's running smoothly except that the text runs together, it doesn't seem to accept escape-character formatting.  I've even taken the time to switch all variables away from wide-chars and functions into the "A" versions--using LPWSTR's and the functions for wide-characters, it seemed rare to have them accept formatted text.  Is it possible to make an "Edit" child window class accept text formatting?  Here's my current source for this portion:

void WriteInTextBox(LPSTR stringToWrite)
	int maxBufferSize = SendMessage(hInfoBox, EM_GETLIMITTEXT, 0, 0);
	LPSTR buffer = newchar[maxBufferSize];

	GetDlgItemTextA(hPatcher, H_INFO_BOX, buffer, maxBufferSize);

	strcat(buffer, stringToWrite);
	strcat(buffer, "\n");

	SetDlgItemTextA(hPatcher, H_INFO_BOX,buffer);
	SendMessage(hInfoBox, EM_LINESCROLL, 0, (SendMessage(hInfoBox, EM_GETLINECOUNT, 0, 0)));

Any help, pointers, suggestions or tips in the right direction would be GREATLY appreciated.  Thank you!

5 Answers Found


Answer 1

Use “\r\n” as a line separator. Make sure buffer is always large enough and is deleted. Appending can be also done by moving the caret to the end and executing EM_REPLACESEL (one for stringToWrite and another one for “\r\n”).


Answer 2

Works perfectly!  Thank you very much!!

Two questions in response to this. 
First, is the method for appending which you suggested more standard or common or appropriate?  Or is it a matter of preference?
Second, could you explain why both escape characters  are needed for this?


Answer 3

If you are not using MFC/ATL then the appropriate forum  is VC General.

Yes, EM_REPLACESEL is the normal way to append text  to an edit  control.

The "\r\n" line ending has always been needed by the edit control.  Probably to be compatible with standard Windows .txt files.  Such API interface details are never changed, to preserve compatibility with old programs.


Answer 4

I figured this was the closest thing to WinAPI here.


Anyway, I was actually asking the details behind why it requires both, but as far as being compatible with standard .txt, you can save information  into a text  file using just \n or \r seperate to create new lines.  I'm just curious why the edit  control needs both.


Answer 5

I figured this was the closest thing to WinAPI here.
Anyway, I was actually asking the details behind why it requires both, but as far as being compatible with standard .txt, you can save information  into a text  file using just \n or \r seperate to create new lines. I'm just curious why the edit  control needs both.
When you write to a file in text mode (the default), on Windows the \n is automatically replaced by \r\n in the file. And when you read a text file, the \r\n is replaced by \n.


If I'm setting the .Value property of a cell, how would I force Excel to insert a break in the text?

For example, if the .Value will be John Smith, how would I set the value so that its display as



The cells in question are merged and wrapped.



I am kind of new to Visual Basic and I got stuck doing a sample Windows Form program. I was trying to write a String into a text file. But I just couldnt get the command for the new line character whereby each time, the new String is concatenated to the next line instead of the same line. I know that in C the new line character \n is used. But what about in Visual Basic?

Any help would be appreciated.





What data type should be used for a column storing text as descripted in title? Please advise.


Hi there,

I'm testing out a CMS I've built in ASP.NET and I'm having issues validating a form textarea with a regular expression.

I've used a ReguarExpressionValidator control to make sure a maximum of 4000 characters are entered but have realised I'm not including line breaks in the regular expression and can't figure out how to include them?

Hopefully someone can give me a hand. I've included my code below so you can see where I'm going wrong :)

                <td><label for="txtSectionText">Text</label></td>
                <td><asp:TextBox id="txtSectionText" runat="server" TextMode="multiline" Rows="15" Columns="50" /></td>
                <td><asp:RegularExpressionValidator id="reSectionText" runat="server" ControlToValidate="txtSectionText" ValidationExpression="^+.\r\n{0,4000}$" ErrorMessage="Please enter no more than a maximum of 4000 characters" Display="Dynamic" /></td>

Thanks very much :)


I am using the following piece of code for an alert popup. I want to insert some new line characters to make the alert better readable, but when I insert an \n in the sentence, the alert popup just does not show up.

Please suggest what is the best way to break line after the pariod.

if (CaseOrders.Count < 1)
            alertString = "alert('Tests ordered did not match any Tests/Panels in ET. "+" MessageControl ID=" + MessageControlID + "');";
            Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "ALERT", alertString, true);

Thanks for your help.


Ok, let's see if this all makes sense. Today, as I began working on a small project, I ran into an error I can't seem to get over. The function of the program I am working on is to read data from a pipe (which is the output of another program) and update an HWND ("edit") control dialog using WinAPI. Now, I've been successful at updating the dialog: sort of. If I send the character buffer from the ::ReadFile() function to the dialog, I get the proper output but with a ton of extra characters I don't want. So how can I simply strip it to the output I'm looking for?


Here is what's happening:






Here is what I want to happen:


some output...'Hola Mondo' means...

Hello word!



Thanks for any help!

Dennis M.

I am trying to draw a line between two points on a panel in windows forms using the bellow code

Pen blackPen = new Pen(Color.Black, 2);
Graphics gr = pnl.CreateGraphics();

But the problem is there are few child controls on the path and the line is going behind those controls. Is there any property that I need to set to make it visible on top of other controls like z-index in webforms.

Kumar, Web Developer.

 Hi, im having trouble finding a way to pass a value from the parent to child window. ive seen many examples of child to parent but not the other way around. Any help would be appreciated



I'm writing to learn if it is possible to pass values from a MainPage to a Child Window. I have a property in the MainPage.xaml.cs, and I assign _nameText = fptZoneIDText.Text. 

In ChildWindow.xaml.cs, I create a new instance of MainPage, and I try assign the NameText property to a variable. However, when I step through the code in the Child Window, NameText is "null".  Any insight is appreciated! Best regards, Rob  

public event EventHandler SubmitClicked;

public string _nameText;

public string NameText{

 get { return _nameText; }

set { _nameText = value; }






I really hope you can help me with this - I have tried to solve this for several days now. It concerns the Sharepoint 2010 platform.

It is pretty simple actually. I have created a parent/child webpart relationship, and it works pretty well. However, when I add a new child item I would like the child item to automatically relate to the chosen parent.

The ideal would be this scenario:

A Form with with task-details and a list of persons and hours at the bottom of the form, with a new "Add hours" button.

Hope somebody can help.

Best regards....


hi All,

I have some problems when try to destroy dialog from thread.
thread is created when button click on dialog.
thread do some job and calls DestroyWindow(hDilaog);
The dialog is destroyed but still appears and flickers.
Why is that ?


hi all,

want to ask whem my app window is on desktop and if move other window over it, the content is wiped.

when i minimize and show window agin its ok, because i send wm_paint.

but how to solve the problem when other window move over mine ?

this happens when i stop theme service under win7




I am trying to get something work here. Now the issue is that one of the child window is minimized and active.  Now i need an winapi to check if the minimized window is active.

If active ...do something

if not active do something else

Note: The minimized window is like a child window where many windows are open and one might be minimized.

please help here.




we are opening window.open pop-up as child-window from parent window. When i click on parent window then child window is getting minimized.

Is there any solution for making child-window not to be minimized when i click on parent window.

And child-window should not have minimize and maximized buttons. It should only have close button.(ie., I should be similar as ShowModalDialog ie., look and feel and functionality also).


We have an application that has non-modal child dialogs with their own taskbar button.  The child dialogs are always forced to be on top of the main window.

However, we've noticed an odd behavior where if you have a child dialog open, then activate another window (say a Windows Explorer window), then click the dialog to activate it, and then close the child dialog, the Windows Explorer window will activate on top of the application's main window, instead of the main window being the foreground window.

Any ideas how to fix this?


Using MOSS and SPD, I have the need to;

when someone creates a new item (NewForm.aspx) or views an existing item (DispForm.aspx) for it to allow them to choose to copy the current item into the same list as a new item, and then take them to that new item in edit mode (EditForm.aspx) so they can modify the unique fields.

Has anyone done this with SharePoint Designer 2007 yet?

I have got it to copy the item into a new item but then can't automatically take them to the new item to edit.


Hi there!

I would like to build a second SaveButton next to the SharePoint:SaveButton inside my Custom List.
This neu Button can be a normal aspx Button and should run a Code behind onClick Event to save the ListItem at a seperate way.

I need help by editing and replacing the New/Edit Form with VisualStudio 2010. I dont know how.
The CustomActions should be still visible in New/Edit Form.

If it does not work this way, please help me to do it an other way =).

Thanks, The-Day


i found that the two phases are very similiar, i even can't distinguish these two passes.anyone can give a clear interpretation?


Hi Guys,

I'm creating workflows "on the fly", without any designers involved.

I have my own Sequence activity implementation which has one InArgument parameter, it is just a container. How I want to pass this parameter to the children. I have an interface IParamActivity so what I basically want is to check if the activity implements this interface => pass my InArgument to it.

Simple idea, ist't it? :)
But I couldn't figure out the implementation...

Here is my code (prototype):

//An interface Activity MIGHT implement
internal interface IParamActivity 
 InArguments<WFParameters> Parameters {get; set; } 
//Sequence Activity class
public sealed class MySequence : NativeActivity
 public InArgument<WFParameters> Parameters {get; set; }

 public Collection<Activity> Activities { ..... }

 //that's how it gets activities
 public MySequence(IEnumerable<Activity> activities)

 //activities are scheduled, everything works

And there are my tries:

//doesn't work
paramActivity = new InArgument<WFParameters>(x=>this.Parameters.Get(x));
//doesn't work either
paramActivity = this.Parameters;

I also have tried to declare a Variable<WFParameters>, set its value in the Execute method and do like:

protected override void CacheMetadata(NativeActivityMetadata metadata)

 foreach (var paramActivity in Activities.OfType<IParamActivity>())
 paramActivity.Parameters = new InArgument<WFParameters>(_parameters);

And guess what? Doesn't work :)
Could you please point me to the right direction?





Is there a way to get a range object or selection of the first character/word/line (or something) that represents the top of what is currently viewed on the screen? I know you can use the ScrollIntoView function to force a range to be viewable, but I basically want to do the opposite and use some sort of "GetRangeInView" function to return the range that is currently being viewed.



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure