?? console.h
字號:
nSize = chPos[i+1] - chPos[i] + 1;
}
nSize = max(nSize, (int)strlen(caption)+2);
_SaveSettings(); // 保存以前的設置
// 計算窗口
int x1, y1, x2, y2;
x1 = 0;
y1 = 0;
x2 = x1 + nSize + 2;
y2 = y1 + nNum + 3;
_CalCenterWindow(&x1, &y1, &x2, &y2);
_DefineWindow(x1, y1, x2+1, y2+1); // 重新定義窗口
_SaveWindow();
_DefineWindow(x1+1, y1+1, x2+1, y2+1); // 重新定義窗口
_SetBackColor(8);
_FillBox(0,0, x2-x1+1, y2-y1+1, false); // 陰影
_DefineWindow(x1, y1, x2, y2); // 重新定義窗口
int nColor[3];
bool bShow = true;
switch (mode) {
case 1:
nColor[0] = 2; nColor[1] = 15; nColor[2] = 15;
bShow = false;
break;
case 2:
nColor[0] = 3; nColor[1] = 14; nColor[2] = 14;
break;
case 3:
nColor[0] = 4; nColor[1] = 14; nColor[2] = 14;
break;
default:
nColor[0] = m_nFrameColor[0];
nColor[1] = m_nFrameColor[1];
nColor[2] = m_nControlColor[1];
bShow = false;
break;
}
_SetBackColor(nColor[0]);
_SetForeColor(nColor[1]);
_ShowCursor(bShow);
_ClearWindow();
_DrawBox(0, 0, x2-x1+1, y2-y1+1, 1);
_SetForeColor(nColor[2]);
// 輸出標題
_OutTextXY((nSize - strlen(caption))/2 + 2, 0, caption);
unsigned int nStart, nDispNum;
for (i=0; i<nNum; i++) // 輸出消息文本
{
nStart = chPos[i];
nDispNum = chPos[i+1]-nStart;
_OutTextXY((nSize-nDispNum)/2+2, i+2, &str[nStart],nDispNum);
}
chRes = _getch();
// 恢復原來的設置
_LoadSettings();
_PutWindow(x1, y1);
return chRes;
}
char* CConUI::_InputBox(char *name, int x, int y, int nchars, bool center)
{
_SaveSettings(); // 保存以前的設置
if (nchars<(int)strlen(name)) nchars = (int)strlen(name);
// 計算窗口
int x1, y1, x2, y2;
x1 = x;
y1 = y;
x2 = x1 + nchars + 2;
y2 = y1 + 2;
if (center)
_CalCenterWindow(&x1, &y1, &x2, &y2);
_DefineWindow(x1, y1, x2+1, y2+1); // 重新定義窗口
_SaveWindow();
_DefineWindow(x1+1, y1+1, x2+1, y2+1); // 重新定義窗口
_SetBackColor(8);
_FillBox(0,0, x2-x1+1, y2-y1+1,false); // 陰影
_DefineWindow(x1, y1, x2, y2); // 重新定義窗口
// 繪制輸入框
_SetBackColor(m_nFrameColor[0]);
_SetForeColor(m_nFrameColor[1]);
_ShowCursor(true);
_ClearWindow();
_DrawBox(0, 0, x2-x1+1, y2-y1+1);
// 繪制提示文本
_OutTextXY((nchars - strlen(name))/2 + 2, 0, name, strlen(name));
_LoadSettings();
int res, num=0;
char strRes[256];
//m_nCharNum = 0;
res = _InputLine(x1+1, y1+1, nchars, m_nControlColor[0], m_nControlColor[1], strRes, &num);
_PutWindow(x1, y1);
if (res == 0) {
strncpy(m_InputChars, strRes, num);
return m_InputChars;
} else return "";
}
int CConUI::_InputLine(int x, int y, int nlength, int bkcolor, int focolor,
char *str, int *nchars)
{
_SaveSettings(); // 保存以前的設置
// 計算窗口
int x1, y1, x2, y2;
x1 = x;
y1 = y;
x2 = x1 + nlength - 1;
y2 = y1;
_DefineWindow(x1, y1, x2, y2); // 重新定義窗口
// 繪制輸入框
_SetBackColor(bkcolor);
_SetForeColor(focolor);
_ClearWindow();
int nInputCharNum = 0;
int nCharPos = 0; // 字符位置
int nCursorPos; // 光標位置,范圍從0到nchars-1
int nLeft = 0; // 滾動的字符個數
unsigned int ch;
int i;
if (m_nCharNum>0) {
_OutTextXY(0, 0, &m_InputChars[0], min(m_nCharNum, nlength));
nInputCharNum = m_nCharNum;
}
_ShowCursor(true);
_SetCursorPos(0, 0);
// 控制鍵盤輸入,當按ENTER鍵或ESC鍵結束輸入
// 按ESC鍵后,返回NULL
for(;;)
{
bool bChanged = false;
ch = _GetKeyChar();
// 當按下ESC、ENETR和TAB鍵退出
if ((ch == VK_ESCAPE)||(ch == VK_RETURN)||(ch == VK_TAB)) break;
if ((ch == VK_UP)||(ch == VK_DOWN)) break;
switch (ch)
{
case VK_HOME: // HOME鍵
nCharPos = 0;
nLeft = 0;
bChanged = true;
break;
case VK_END: // END鍵
nCharPos = nInputCharNum;
bChanged = true;
nLeft = nCharPos - nlength;
if (nLeft<0) nLeft = 0;
break;
case VK_LEFT: // 向左
nCharPos--;
if (nCharPos<0) nCharPos = 0;
else
bChanged = true;
break;
case VK_RIGHT: // 向右
nCharPos++;
if (nCharPos>nInputCharNum)
nCharPos = nInputCharNum;
else
bChanged = true;
break;
case VK_BACK: // Back Space
nCharPos--;
if (nCharPos<0) {
nCharPos = 0;
break;
}
case VK_DELETE: // DEL鍵,刪除當前字符
for (i=nCharPos+1; i<=nInputCharNum; i++) {
m_InputChars[i-1] = m_InputChars[i];
bChanged = true;
}
if (bChanged){
nInputCharNum--;
if (nInputCharNum<0) {
nInputCharNum = 0;
bChanged = false;
}
}
break;
}
// 顯示和記錄字符以及相應的光標
if (isprint(HIBYTE(ch))) { // 判斷是否是有效字符
for (i = nInputCharNum; i>nCharPos; i--)
m_InputChars[i] = m_InputChars[i-1];
m_InputChars[nCharPos] = HIBYTE(ch);
nCharPos++;
nInputCharNum++;
if (nInputCharNum >= 252) nInputCharNum = 252;
if (nCharPos >= 252) nCharPos = 252;
bChanged = true;
}
if ((nCharPos - nLeft)>=nlength) nLeft++;
int nCmp = 0;
if (ch == VK_BACK) nCmp = 1;
if ((nCharPos - nLeft)< nCmp) {
nLeft--;
if (nLeft<0) nLeft = 0;
}
if (nLeft>0) bChanged = true;
if (bChanged) {
_ClearWindow();
int nNum = min(nlength-1, nInputCharNum); // 可以顯示的字符
if ((nCharPos<nInputCharNum)&&(nInputCharNum>=nlength))
nNum++;
_OutTextXY(0, 0, &m_InputChars[nLeft], nNum);
bChanged = false;
nCursorPos = nCharPos - nLeft;
_SetCursorPos(nCursorPos, 0);
}
}
_LoadSettings();
*nchars = nInputCharNum;
m_nCharNum = nInputCharNum;
if (m_nCharNum>0) {
strncpy(str, m_InputChars, nInputCharNum);
}
else
str = NULL;
if (ch == VK_ESCAPE) return -1;
if (ch == VK_RETURN) return 0;
if (ch == VK_TAB) return 1;
if (ch == VK_UP) return 2;
if (ch == VK_DOWN) return 3;
return -1;
}
int CConUI::_GetOptions(char **str, int x, int y, int num, int *chpos, bool center)
{
// 計算最長選項文本的字符個數
unsigned int nNameSize = 10, nTitleCharNum;
nTitleCharNum = strlen(m_strOptionsTitle);
if (nNameSize<nTitleCharNum) nNameSize = nTitleCharNum;
for (int i=0; i<num; i++) {
if (nNameSize<strlen(str[i])) nNameSize = strlen(str[i]);
}
// 計算所需要的窗口大小
int x1, x2, y1, y2;
x1 = x; y1 = y;
x2 = x1 + nNameSize + 3;
y2 = y1 + num + 1;
if (center)
_CalCenterWindow(&x1, &y1, &x2, &y2);
_SaveSettings();
_DefineWindow(x1, y1, x2+1, y2+1); // 重新定義窗口
_SaveWindow();
_DefineWindow(x1+1, y1+1, x2+1, y2+1); // 重新定義窗口
_SetBackColor(8);
_FillBox(0,0, x2-x1+1, y2-y1+1, false); // 陰影
_DefineWindow(x1, y1, x2, y2); // 重新定義窗口
_ShowCursor(false);
// 繪制窗口
_SetBackColor(m_nFrameColor[0]);
_SetForeColor(m_nFrameColor[1]);
_ClearWindow();
_DrawBox(0, 0, x2-x1+1, y2-y1+1);
// 輸出標題
_OutTextXY((nNameSize - nTitleCharNum)/2 + 2, 0, m_strOptionsTitle);
int posX = 2, posY = 1, n;
// 繪制并解析菜單
m_nMenuItemNum = 0;
for (i=0; i<num; i++) {
if ((str[i][0] == '-')||(str[i][0] == '_')){
_SetForeColor(m_nFrameColor[1]);
_DrawCharLine(1,posY,nNameSize+2, (char)0xc4);
} else {
_SetForeColor(m_nControlColor[1]);
_OutTextXY(posX, posY, str[i]);
if (chpos)
n = chpos[i];
else
n = 0;
char chItem = (char)toupper(str[i][n]);
_SetForeColor(m_nMenuColor[2]);
_OutTextXY(posX + n, posY, &chItem, 1);
menuItem[m_nMenuItemNum].chPos = n;
menuItem[m_nMenuItemNum].chItem = chItem;
menuItem[m_nMenuItemNum].itemPos = posY;
strcpy(menuItem[m_nMenuItemNum].itemName, str[i]);
m_nMenuItemNum++;
}
posY++;
}
// 控制鍵盤輸入,當按ENTER鍵或ESC鍵或有效的字符結束輸入
// 按ESC鍵后,返回-1
static int nSelectItemNext = 0;
if (nSelectItemNext<0) nSelectItemNext = 0;
if (nSelectItemNext>(m_nMenuItemNum-1)) nSelectItemNext = 0;
int nSelectItem = nSelectItemNext, ch, nBak = nSelectItem;
bool bOK = false;
// 繪制選擇的文本
_SetBackColor(m_nMenuColor[0]);
_SetForeColor(m_nMenuColor[1]);
posY = menuItem[nSelectItem].itemPos;
_FillBox(posX - 1, posY, nNameSize+2, 1, false);
for(;;)
{
bool bChanged = false;
ch = _GetKeyChar();
// 當按下ESC、ENETR和TAB鍵退出
if ((ch == VK_ESCAPE)||(ch == VK_RETURN)) break;
if ((ch == VK_LEFT)||(ch == VK_RIGHT)) break;
char c = HIBYTE(ch);
if (isprint(c)) { // 判斷是否是有效字符
c = (char)toupper(c);
for (int i=0; i<m_nMenuItemNum; i++)
{
if (c == menuItem[i].chItem) {
bOK = TRUE;
nSelectItem = i;
break;
}
}
if (bOK) break;
}
switch (ch)
{
case VK_UP: // 向上
nSelectItem--;
if (nSelectItem<0)
nSelectItem = m_nMenuItemNum - 1;
bChanged = true;
break;
case VK_DOWN: // 向下
nSelectItem++;
if (nSelectItem>(m_nMenuItemNum-1))
nSelectItem = 0;
bChanged = true;
break;
}
if (bChanged){
bChanged = false;
// 恢復原來的文本
_SetBackColor(m_nFrameColor[0]);
_SetForeColor(m_nControlColor[1]);
posY = menuItem[nBak].itemPos;
_FillBox(posX - 1, posY, nNameSize+2, 1, false);
_SetForeColor(m_nMenuColor[2]);
char chItem = menuItem[nBak].chItem;
_OutTextXY(posX + menuItem[nBak].chPos, posY, &chItem, 1);
// 繪制選擇的文本
nBak = nSelectItem;
posY = menuItem[nBak].itemPos;
_SetBackColor(m_nMenuColor[0]);
_SetForeColor(m_nMenuColor[1]);
_FillBox(posX - 1, posY, nNameSize+2, 1, false);
}
}
_PutWindow(x1, y1);
_LoadSettings();
nSelectItemNext = nSelectItem;
if (ch == VK_RETURN) bOK = true;
if (bOK) {
return nSelectItem;
}
else
return -1;
}
void CConUI::_CalCenterWindow(int *x1, int *y1, int *x2, int *y2)
{
int nSizeX, nSizeY;
_GetConwinSize(&nSizeX, &nSizeY);
int w = (*x2 - *x1);
int h = (*y2 - *y1);
*x1 = (nSizeX - w)/2;
*y1 = (nSizeY - h)/2;
*x2 = *x1 + w;
*y2 = *y1 + h;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -