我送你樣品類cConsole
的一些方法:
class cConsole {
private:
//-------------------
int lines;
int max_lines; // Init it with your choise (300)
//-------------------
char* buf;
int buf_size;
//-------------------
int CheckMemory(int size);
void NewLine(int new_lines);
void InternalPrint(char* msg, int size);
public:
HWND hWin;
void Print(char* msg); // Add text data through this methods
void Print(char* msg, int size);
cConsole();
~cConsole();
};
int cConsole::CheckMemory(int size) {
int rv = 1;
if(size + 16 >= buf_size) {
int new_buf_size = size + 1024;
char* new_buf = (char*)realloc(buf, new_buf_size);
if(new_buf != NULL) {
buf = new_buf;
buf_size = new_buf_size;
} else {
rv = 0;
}
}
return rv;
}
void cConsole::NewLine(int new_lines) {
int rem_lines = (new_lines + lines + 1) - max_lines;
if(rem_lines <= 0) {
lines += new_lines;
} else {
int sel = SendMessage(hWin, EM_LINEINDEX, rem_lines, 0);
SendMessage(hWin, EM_SETSEL, 0, (LPARAM)sel);
SendMessage(hWin, EM_REPLACESEL, FALSE, (LPARAM)"");
SendMessage(hWin, WM_VSCROLL, SB_BOTTOM, NULL);
lines = max_lines - 1;
}
}
void cConsole::Print(char* msg) { InternalPrint(msg, -1); }
void cConsole::Print(char* msg, int size) { if(size < 0) size = 0; InternalPrint(msg, size); }
void cConsole::InternalPrint(char* msg, int size) {
int s, t = 0;
int new_lines = 0;
char* tb;
// msg only mode
if(size == -1) size = 0x7fffffff;
if(msg != NULL && size && CheckMemory(t)) {
for(s = 0; msg[ s ] && (s < size); s++) {
if(msg[ s ] == '\r') continue;
if(!CheckMemory(t)) break;
if(msg[ s ] == '\n') {
++new_lines;
buf[ t++ ] = '\r';
}
buf[ t++ ] = msg[ s ];
}
buf[ t ] = '\0';
}
if(t && msg != NULL) {
tb = buf;
} else {
++new_lines;
tb = "\r\n";
}
SendMessage(hWin, EM_SETSEL, (WPARAM)-2, (LPARAM)-1);
SendMessage(hWin, EM_REPLACESEL, FALSE, (LPARAM)tb);
SendMessage(hWin, WM_VSCROLL, SB_BOTTOM, NULL);
if(new_lines) NewLine(new_lines);
}
內置自己的類,並檢查該!
我使用了-1作爲'EM_SETSEL'的開始和結束位置來獲得結束,它似乎已經工作。它是否正確?另外,我如何有效地找到第一個換行符來刪除RichEdit控件中的第一行? – Sydius 2010-02-06 00:19:39
是的,我通常使用-1作爲起點和終點。我相信你應該能夠用EM_FINDTEXT找到一個換行符(你可能需要查找「\ r \ n」),儘管我想我必須檢查確定。 – 2010-02-06 00:23:52