简体   繁体   中英

WinAPI - How to print text to edit control?

REVISION 1:

I can't get a good grip on WinAPI's AllocConsole(); function. I either need option one, or option 2. It's better to have both though :). Thanks in advance. Here's option 1:

Make a console window from a WinAPI program when BUTTON_1 is clicked. The console needs to be able to output text (like cout ), and sleep/delay text from being printed. (Something like Sleep() )

Option 2:

Instead of printing text to the console window like in option 1, the program prints text to a user-inaccessible editbox. This also needs to be able to use something like Sleep() to delay text from being printed.

Assuming you're appending strings to an edit control, perhaps to record TTY-style output from some lengthy process:

Use the EM_SETSEL message to select the range just after the last character, then use EM_REPLACESEL to replace it with the string to append. The edit control scrolls when this happens. For example, if hEdit is the handle for the edit control, and str the 0-terminated string to append:

int idx=GetWindowTextLength(hEdit);
SendMessage(hEdit,EM_SETSEL,idx,idx);

SendMessage(hEdit,EM_REPLACESEL,0,(LPARAM)str);

This is the process recommended by this Knowledge Base article: http://support.microsoft.com/kb/109550

My suggestions is to use

SendMessage(hEditBox,EM_SETSEL,-2,-2);

to move the caret to the end of text in the edit control. It saves you one function call and it works.

About the Sleep related part, I think it would be better to use timers, that should work without any problems.

Ex.

// somewhere in the code where you decide to make changes to the edit control
...
SetTimer(hWnd,TIMER_ID,1000,0);  // TIMER_ID is arbitrary, the delay is set to 1000 ms
...

// main procedure message 
switch (uMsg) {
   ...
   case WM_TIMER:
      SendMessage(hEditBox,EM_SETSEL,-2,-2);
      SendMessage(hEditBox,EM_REPLACESEL,0,buffer);
      KillTimer(hWnds,TIMER_ID);
   ...
}

You may want to put a bit more work into this, for instance to reuse the SetTimer call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM