2013-03-16 39 views
3

我已创建编辑窗口。 我希望一行显示一个字符串,另一行显示另一个字符串,但我正在执行的代码仅显示第二个字符串。下面是我的代码片段:使用SetWindowText()添加新行功能

hWndEdit = CreateWindow("EDIT", // We are creating an Edit control 
           NULL, // Leave the control empty 
           WS_CHILD | WS_VISIBLE | WS_HSCROLL | 
           WS_VSCROLL | ES_LEFT | ES_MULTILINE | 
           ES_AUTOHSCROLL | ES_AUTOVSCROLL, 
           10, 10,1000, 1000, 
           hWnd, 
           0, 
           hInst,NULL); 
SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n")); 

SetWindowText(hWndEdit, TEXT("\r\nSecond string")); 

OUTPUT: OUTPUT

+3

我建议[SetWindowText函数]的彻底审查(http://msdn.microsoft.com/en-us/library/windows/desktop/ms633546(V = vs.85)的.aspx),特别是该部分从根本上描述了函数如何与其同名:SetWindowText()。它不是AppendWindowText()。由于这是一个编辑控件,我会推测访问[编辑控件消息](http://msdn.microsoft.com/en-us/library/windows/desktop/ff485923(v = vs.85).aspx)和他们能为你做的事情可能是一个有趣的阅读。 – WhozCraig 2013-03-16 08:01:09

+1

@WhozCraig好像我一直在阅读你的评论。请开始张贴他们作为答案,以便我可以开始upvoting他们! – 2013-03-16 08:10:44

+1

@CodyGray谢谢,科迪。你的头像是*真棒*,顺便说一句。我一直在这样做了大约9个活跃的月份,但有时我只是忘记张贴答案。或者我不认为这是一个真正的贡献作为答案。有时RTFM是适当的,但我只是放下评论。习惯,我想。 – WhozCraig 2013-03-16 08:14:01

回答

8

你只看到最后一行,因为SetWindowText()替换窗口中的全部内容一气呵成。

如果你想在同一时间显示两行,只需在一次调用一起将它们连接起来,以SetWindowText()

SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n\r\nSecond string")); 

在另一方面,如果你想在不同的时间插入它们,你得使用EM_SETSEL消息以将编辑符号在窗口的末端,然后使用EM_REPLACESEL消息在当前插入位置插入文本,如本文中描述:

How To Programatically Append Text to an Edit Control

例如:

void AppendText(HWND hEditWnd, LPCTSTR Text) 
{ 
    int idx = GetWindowTextLength(hEditWnd); 
    SendMessage(hEditWnd, EM_SETSEL, (WPARAM)idx, (LPARAM)idx); 
    SendMessage(hEditWnd, EM_REPLACESEL, 0, (LPARAM)Text); 
} 

AppendText(hWndEdit, TEXT("\r\nFirst string\r\n")); 
AppendText(hWndEdit, TEXT("\r\nSecond string")); 
+2

+1,尤其是对于控制消息的建设性使用(我试图在开场白的评论中不太羞涩地提示一下)。 – WhozCraig 2013-03-16 08:17:09

+0

+1,谢谢你的帮助,好答案。真的帮了我:) – Ayse 2013-03-18 05:00:03

2
hWndEdit = CreateWindow("EDIT", // We are creating an Edit control 
           NULL, // Leave the control empty 
           WS_CHILD | WS_VISIBLE | WS_HSCROLL | 
           WS_VSCROLL | ES_LEFT | ES_MULTILINE | 
           ES_AUTOHSCROLL | ES_AUTOVSCROLL, 
           10, 10,1000, 1000, 
           hWnd, 
           0, 
           hInst,NULL); 
     SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n\r\nSecond string")); 

 SetWindowText(hWndEdit, TEXT("\r\nFirst string\r\n")); 

     char* buf = malloc(100); 
     memset(buf, '\0', 100); 

     GetWindowText(hWndEdit, (LPTSTR)buf, 100); 
     strcat(buf, "\r\nSecond string"); 
     SetWindowText(hWndEdit, (LPTSTR)buf); 
+0

谢谢你的帮助:)这两个答案都是我使用的:) – Ayse 2013-03-18 05:00:44