📄 msgbox.c
字号:
break;
msgDat.ptext[LineCnt+1] = pLineFeed+1;
}
LineCnt += 1;
if (dwFlag & MB_ICON_MASK)
MsgBoxW += MB_ICON_W;
MsgBoxH += LineCnt * (pFont->height+MB_ROW_SPACING) + MB_ROW_SPACING;
if (dwFlag & MB_ICON_MASK)
MsgBoxH = MsgBoxH>(MB_ICON_H+MB_ROW_SPACING)? MsgBoxH:(MB_ICON_H+MB_ROW_SPACING);
if (ptitle)
{
MsgBoxH += MB_TITLEBAR_HEIGHT;
hai_GetStringExtent(hDC, ptitle, &StrLength, S_NULL);
MsgBoxW = MsgBoxW > StrLength? MsgBoxW:StrLength;
}
else
MsgBoxH += MB_TOP_HEIGHT;
hai_ReleaseDC(hDC);
if (dwFlag & MB_BTN_MASK)
{
MsgBoxH += MB_BTNBAR_HEIGHT;
if ((dwFlag & MB_BTN_MASK) == MB_BTNOK)
StrLength = MB_BTN_W + 5;
// else if ((dwFlag & MB_BTN_MASK) == MB_BTNYESNOCANCEL)
// StrLength = (MB_BTN_W + MB_BTN_SPACEBETWEEN)*3;
else
StrLength = (MB_BTN_W + MB_BTN_SPACEBETWEEN)*2;
StrLength += 6;
MsgBoxW = MsgBoxW > StrLength? MsgBoxW:StrLength;
}
else
MsgBoxH += MB_BOTTOM_HEIGHT;
MsgBoxW += MB_SIDE_W * 2;
if (MsgBoxW+20 >= LCD_WIDTH || MsgBoxH+10 >= LCD_HEIGHT)
{
hai_MemFree(CurText);
return MB_RTN_ERROR;
}
if (dwFlag & MB_BKICON_MASK)
{
}
else
{
MsgBoxX = (LCD_WIDTH - MsgBoxW)/2;
MsgBoxY = (LCD_HEIGHT - MsgBoxH)/2;
}
msgDat.ptitle = ptitle;
msgDat.dwflag = dwFlag;
CaretShow = hai_HideCaret();
HG_SaveCurCaret(&BackCaret, sizeof(S_CARET));
_SaveCurrentScene4MsgBox(MsgboxID);
pScene = hai_GetCurSceneAddr();
hai_SetScene(pScene, MsgboxID, SID_MSGBOX, &msgDat, _MsgBoxProc);
pScene->SceneRect.x = MsgBoxX;
pScene->SceneRect.y = MsgBoxY;
pScene->SceneRect.w = MsgBoxW;
pScene->SceneRect.h = MsgBoxH;
// hai_InitEventQueue();
_hai_SetInvalidRectEx(MsgBoxX, MsgBoxY, MsgBoxW, MsgBoxH);
hai_SendEvent(EVT_CREAT, 0, 0);
hai_SendEvent(EVT_PAINT, 0, 0);
while (1)
{
event = hai_GetEvent();
if (event.EventType == EVT_QUIT)
break;
hai_EventPreprocess(&event);
pScene->pSceneProc(&event);
if (event.EventType == EVT_EXIT)
{
event.wParam = MB_RTN_NULL;
break;
}
else
hai_EventEndprocess(&event);
}
hai_MemFree(CurText);
hai_GetSceneRect(&MsgBoxX, &MsgBoxY, &MsgBoxW, &MsgBoxH);
_RestoreCurrentScene4MsgBox(MsgboxID);
_ClearSceneInfo4MsgBox(MsgboxID);
HG_RestoreCurCaret(&BackCaret);
if ((dwFlag & MB_RESTOREBG_MASK) == MB_RESTORE_BG)
{
hai_InvalidRectEx(MsgBoxX, MsgBoxY, MsgBoxW, MsgBoxH);
if (CaretShow)
hai_ShowCaret();
}
return (S_WORD)event.wParam;
}
S_VOID _OpenMsgBoxProc(SP_EVT pEvt)
{
S_BYTE b, bk;
S_WORD w, x = MB_SIDE_W, y = MB_ROW_SPACING;
S_COLOR c;
S_BYTE **pptext;
SH_DC hDC;
_SP_FONT pfont;
S_PAINTSTRUCT ps;
SP_RECT pInvalidRect = hai_GetInvalidRect();
SP_RECT pSceneRect = hai_GetSceneRect(S_NULL, S_NULL, S_NULL, S_NULL);
S_SCENE scene;
SP_SCENE pCurScene = hai_GetCurSceneAddr();
SP_MSGBOXDAT pMsgDat = hai_GetSceneData(0);
switch (pEvt->EventType)
{
case EVT_CREAT:
break;
case EVT_PAINT:
if (!hai_IsRectCovered(pSceneRect, pInvalidRect))
{
scene = *pCurScene;
*pCurScene = gMsgbox[pEvt->AppID];
b = hai_SetScreenFlush(SCREEN_FLUSH_OFF);
hai_SendEvent(EVT_PAINT, 0, 0);
hai_SetScreenFlush(b);
*pCurScene = scene;
}
hDC = hai_BeginPaint(&ps);
if (pMsgDat->dwflag & MB_BKICON_MASK)
{
}
else
{
if (pMsgDat->ptitle)
hai_DrawWndFrame(hDC, 0, 0, pSceneRect->w, pSceneRect->h, S_NULL);
else
{
hai_Draw3DBlock(hDC, 0, 0, pSceneRect->w, pSceneRect->h, S_TRUE, GRAY_BRUSH);
hai_Draw3DBlock(hDC, 2, 2, (S_SHORT)(pSceneRect->w-4), (S_SHORT)(pSceneRect->h-4), S_FALSE, LTGRAY_BRUSH);
}
}
pfont = (_SP_FONT)((SP_DC)hDC)->hFont;
c = hai_SetTextColor(hDC, COLOR_BLACK);
bk = hai_SetBkMode(hDC, BKM_TRANSPARENT);
if (pMsgDat->ptitle)
{
if ((pMsgDat->dwflag & MB_TITLE_MASK) == MB_TITLE_LEFT)
hai_TextOut(hDC, 4, 2, pMsgDat->ptitle, 0);
else
{
hai_GetStringExtent(hDC, pMsgDat->ptitle, &w, S_NULL);
hai_TextOut(hDC, (S_SHORT)((pSceneRect->w-w)/2), 2, pMsgDat->ptitle, 0);
}
y += MB_TITLEBAR_HEIGHT;
}
else
y += MB_TOP_HEIGHT;
if (pMsgDat->dwflag&MB_ICON_MASK)
{
S_DWORD IconID = GetMsgBoxIcon(pMsgDat->dwflag);
hai_DrawIcon(hDC, (S_SHORT)(x-2), (S_SHORT)(y-8), (SH_ICON)hai_LoadBitmap(MAKEINTRESOURCE(IconID)));
x += MB_ICON_W+2;
}
pptext = pMsgDat->ptext;
while(*pptext)
{
if ((pMsgDat->dwflag&MB_TEXT_MASK) == MB_TEXT_LEFT)
{
hai_TextOut(hDC, x, y, *pptext, 0);
}
else
{
hai_GetStringExtent(hDC, *pptext, &w, S_NULL);
w = ((pSceneRect->w-x-MB_SIDE_W+2)-w)/2;
hai_TextOut(hDC, (S_SHORT)(x+w), y, *pptext, 0);
}
y += (pfont->height+MB_ROW_SPACING);
pptext++;
}
hai_SetTextColor(hDC, c);
hai_SetBkMode(hDC, bk);
hai_EndPaint(&ps);
break;
case EVT_KEYUP:
break;
case EVT_COMMAND:
break;
case EVT_TIMER:
break;
case EVT_DESTROY:
break;
case EVT_CTRLCOLOR:
break;
default:
break;
}
}
typedef struct tagOpenMsgBox
{
S_BYTE MsgBoxID;
S_BYTE CaretShow;
}
S_OMB, *SP_OMB;
SH_DLG hai_OpenMsgBox(S_BYTE *ptitle, S_BYTE *ptext, S_DWORD dwFlag)
{
S_BYTE LineCnt;
S_WORD MsgBoxX, MsgBoxY, MsgBoxW, MsgBoxH, StrLength;
S_BYTE MsgboxID;
S_BYTE *pLineFeed, *CurText = S_NULL;
SH_DC hDC;
_SP_FONT pFont;
SP_SCENE pScene;
SP_OMB pOMB = S_NULL;
SP_MSGBOXDAT pmsgDat = S_NULL;
if ((MsgboxID = _FindMsgBoxID()) == APPID_NULL)
return (SH_DLG)S_NULL;
if (ptext == S_NULL)
return (SH_DLG)S_NULL;
dwFlag = dwFlag&~((S_DWORD)MB_BTN_FOCUS_MASK);
dwFlag = dwFlag&~((S_DWORD)MB_BTN_MASK);
CurText = hai_MemAlloc(strlen(ptext)+2);
if (CurText == S_NULL)
return (SH_DLG)S_NULL;
strcpy(CurText, ptext);
pmsgDat = hai_MemAlloc(sizeof(*pmsgDat));
if (pmsgDat == S_NULL)
goto ERR_OPENMSGBOX;
memset(pmsgDat, 0x00, sizeof(*pmsgDat));
if (ptitle && !ptitle[0])
ptitle = S_NULL;
if (ptitle)
{
pmsgDat->ptitle = hai_MemAlloc(strlen(ptitle)+2);
if (pmsgDat->ptitle == S_NULL)
goto ERR_OPENMSGBOX;
strcpy(pmsgDat->ptitle, ptitle);
}
hDC = hai_GetSceneDC();
if (hDC == S_NULL)
goto ERR_OPENMSGBOX;
pFont = (_SP_FONT)((SP_DC)hDC)->hFont;
MsgBoxH = MsgBoxW = StrLength = 0;
pmsgDat->ptext[0] = CurText;
for (LineCnt = 0; LineCnt < MB_TEXTLINE_MAX; LineCnt++)
{
pLineFeed = strchr(pmsgDat->ptext[LineCnt], '\n');
if (pLineFeed)
*pLineFeed = 0;
hai_GetStringExtent(hDC, pmsgDat->ptext[LineCnt], &StrLength, S_NULL);
MsgBoxW = MsgBoxW > StrLength? MsgBoxW:StrLength;
if (!pLineFeed)
break;
pmsgDat->ptext[LineCnt+1] = pLineFeed+1;
}
LineCnt += 1;
if (dwFlag & MB_ICON_MASK)
MsgBoxW += MB_ICON_W;
MsgBoxH += LineCnt * (pFont->height+MB_ROW_SPACING) + MB_ROW_SPACING;
if (dwFlag & MB_ICON_MASK)
MsgBoxH = MsgBoxH>(MB_ICON_H+MB_ROW_SPACING)? MsgBoxH:(MB_ICON_H+MB_ROW_SPACING);
if (ptitle)
{
MsgBoxH += MB_TITLEBAR_HEIGHT;
hai_GetStringExtent(hDC, ptitle, &StrLength, S_NULL);
MsgBoxW = MsgBoxW > StrLength? MsgBoxW:StrLength;
}
else
MsgBoxH += MB_TOP_HEIGHT;
hai_ReleaseDC(hDC);
if (dwFlag & MB_BTN_MASK)
{
MsgBoxH += MB_BTNBAR_HEIGHT;
if ((dwFlag & MB_BTN_MASK) == MB_BTNOK)
StrLength = MB_BTN_W + 5;
// else if ((dwFlag & MB_BTN_MASK) == MB_BTNYESNOCANCEL)
// StrLength = (MB_BTN_W + MB_BTN_SPACEBETWEEN)*3;
else
StrLength = (MB_BTN_W + MB_BTN_SPACEBETWEEN)*2;
StrLength += 6;
MsgBoxW = MsgBoxW > StrLength? MsgBoxW:StrLength;
}
else
MsgBoxH += MB_BOTTOM_HEIGHT;
MsgBoxW += MB_SIDE_W * 2;
if (MsgBoxW+20 >= LCD_WIDTH || MsgBoxH+10 >= LCD_HEIGHT)
goto ERR_OPENMSGBOX;
if (dwFlag & MB_BKICON_MASK)
{
}
else
{
MsgBoxX = (LCD_WIDTH - MsgBoxW)/2;
MsgBoxY = (LCD_HEIGHT - MsgBoxH)/2;
}
pmsgDat->ptitle = ptitle;
pmsgDat->dwflag = dwFlag;
pOMB = hai_MemAlloc(sizeof(S_OMB));
if (pOMB = S_NULL)
goto ERR_OPENMSGBOX;
HG_SetUserInputFunction(USERIP_DISABLE);
pOMB->MsgBoxID = MsgboxID;
pOMB->CaretShow = hai_HideCaret();
_SaveCurrentScene4MsgBox(MsgboxID);
pScene = hai_GetCurSceneAddr();
hai_SetScene(pScene, MsgboxID, SID_MSGBOX, pmsgDat, _OpenMsgBoxProc);
pScene->SceneRect.x = MsgBoxX;
pScene->SceneRect.y = MsgBoxY;
pScene->SceneRect.w = MsgBoxW;
pScene->SceneRect.h = MsgBoxH;
// hai_InitEventQueue();
_hai_SetInvalidRectEx(MsgBoxX, MsgBoxY, MsgBoxW, MsgBoxH);
hai_SendEvent(EVT_CREAT, 0, 0);
hai_SendEvent(EVT_PAINT, 0, 0);
return (SH_DLG)pOMB;
ERR_OPENMSGBOX:
if (CurText) hai_MemFree(CurText);
if (pOMB) hai_MemFree(pOMB);
if (pmsgDat)
{
if (pmsgDat->ptitle)
hai_MemFree(pmsgDat->ptitle);
hai_MemFree(pmsgDat);
}
return (SH_DLG)S_NULL;
}
S_WORD hai_CloseMsgBox(SH_DLG hMsgbox, S_DWORD dwFlag)
{
S_BYTE MsgboxID = ((SP_OMB)hMsgbox)->MsgBoxID;
S_WORD MsgBoxX, MsgBoxY, MsgBoxW, MsgBoxH;
SP_MSGBOXDAT pmsgDat = hai_GetSceneData(0);
if (pmsgDat->ptitle)
hai_MemFree(pmsgDat->ptitle);
hai_MemFree(pmsgDat->ptext[0]);
hai_GetSceneRect(&MsgBoxX, &MsgBoxY, &MsgBoxW, &MsgBoxH);
hai_ReleaseSceneData();
_RestoreCurrentScene4MsgBox(MsgboxID);
_ClearSceneInfo4MsgBox(MsgboxID);
if ((dwFlag & MB_RESTOREBG_MASK) == MB_RESTORE_BG)
{
hai_InvalidRectEx(MsgBoxX, MsgBoxY, MsgBoxW, MsgBoxH);
if (((SP_OMB)hMsgbox)->CaretShow)
hai_ShowCaret();
}
HG_SetUserInputFunction(USERIP_ENABLE);
return MB_RTN_YESOK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -