|
楼主 |
发表于 2014-5-29 18:34:36
|
显示全部楼层
回 eric2013 的帖子
eric2013:官方那个例子,或者你把代码贴上 (2014-05-29 18:26)  /*********************************************************************
*
* Static data
*
**********************************************************************
*/
static WM_HWIN _hMenu;
static WM_HWIN _hText;
static WM_HWIN _hFrame;
static char * _paDescription[] = {
"Creates a new file",
"Opens an existing file",
"Closes the file",
"Quits the application",
" ",
"Opens file 1",
"Opens file 2",
"Opens file 3",
"Opens file 4",
"Undoes the last action",
"Redoes the previously undone action",
"Copies to clipboard",
"Inserts contents of clipboard",
"Deletes the selection",
"Displays program information"
};
/*********************************************************************
*
* Static code
*
**********************************************************************
*/
/*********************************************************************
*
* _AddMenuItem
*/
static void _AddMenuItem(MENU_Handle hMenu, MENU_Handle hSubmenu, const char* pText, U16 Id, U16 Flags) {
MENU_ITEM_DATA Item;
Item.pText = pText;
Item.hSubmenu = hSubmenu;
Item.Flags = Flags;
Item.Id = Id;
MENU_AddItem(hMenu, &Item);
}
/*********************************************************************
*
* _CreateMenu
*
* Purpose:
* Creates the menu widget
*/
static WM_HWIN _CreateMenu(WM_HWIN hParent) {
MENU_Handle hMenu, hMenuFile, hMenuEdit, hMenuHelp, hMenuRecent;
// Create main menu
hMenu = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_HORIZONTAL, ID_MENU);
// Create sub menus
hMenuFile = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
hMenuEdit = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
hMenuHelp = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
hMenuRecent = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
// Add menu items to menu 'Recent'
_AddMenuItem(hMenuRecent, 0, "File 1", ID_MENU_RECENT_0, 0);
_AddMenuItem(hMenuRecent, 0, "File 2", ID_MENU_RECENT_1, 0);
_AddMenuItem(hMenuRecent, 0, "File 3", ID_MENU_RECENT_2, 0);
_AddMenuItem(hMenuRecent, 0, "File 4", ID_MENU_RECENT_3, 0);
// Add menu items to menu 'File'
_AddMenuItem(hMenuFile, 0, "New", ID_MENU_FILE_NEW, 0);
_AddMenuItem(hMenuFile, 0, "Open", ID_MENU_FILE_OPEN, 0);
_AddMenuItem(hMenuFile, 0, "Close", ID_MENU_FILE_CLOSE, MENU_IF_DISABLED);
_AddMenuItem(hMenuFile, 0, 0, 0, MENU_IF_SEPARATOR);
_AddMenuItem(hMenuFile, hMenuRecent, "Files...", ID_MENU_FILE_RECENT, 0);
_AddMenuItem(hMenuFile, 0, 0, 0, MENU_IF_SEPARATOR);
_AddMenuItem(hMenuFile, 0, "Exit", ID_MENU_FILE_EXIT, 0);
// Add menu items to menu 'Edit'
_AddMenuItem(hMenuEdit, 0, "Undo", ID_MENU_EDIT_UNDO, 0);
_AddMenuItem(hMenuEdit, 0, "Redo", ID_MENU_EDIT_REDO, 0);
_AddMenuItem(hMenuEdit, 0, 0, 0, MENU_IF_SEPARATOR);
_AddMenuItem(hMenuEdit, 0, "Copy", ID_MENU_EDIT_COPY, 0);
_AddMenuItem(hMenuEdit, 0, "aste", ID_MENU_EDIT_PASTE, 0);
_AddMenuItem(hMenuEdit, 0, "Delete", ID_MENU_EDIT_DELETE, 0);
// Add menu items to menu 'Help'
_AddMenuItem(hMenuHelp, 0, "About", ID_MENU_HELP_ABOUT, 0);
// Add menu items to main menu
_AddMenuItem(hMenu, hMenuFile, "File", 0, 0);
_AddMenuItem(hMenu, hMenuEdit, "Edit", 0, 0);
_AddMenuItem(hMenu, hMenuHelp, "Help", 0, 0);
// Attach menu to parent window
FRAMEWIN_AddMenu(hParent, hMenu);
return hMenu;
}
/*********************************************************************
*
* _DrawGradientV
*
* Purpose:
* Draws a vertical gradient
*/
static void _DrawGradientV(int x0, int y0, int x1, int y1, GUI_COLOR Color0, GUI_COLOR Color1) {
int r0, g0, b0, r1, g1, b1;
int y, ySize;
ySize = y1 - y0 + 1;
r0 = (Color0 >> 0) & 0x000000ff;
g0 = (Color0 >> 8) & 0x000000ff;
b0 = (Color0 >> 16) & 0x000000ff;
r1 = (Color1 >> 0) & 0x000000ff;
g1 = (Color1 >> 8) & 0x000000ff;
b1 = (Color1 >> 16) & 0x000000ff;
for (y = y0; y <= y1; y++) {
GUI_COLOR Color;
int r, g, b, Diff;
Diff = y - y0;
r = r0 + (r1 - r0) * Diff / ySize;
g = g0 + (g1 - g0) * Diff / ySize;
b = b0 + (b1 - b0) * Diff / ySize;
Color = r | (g << 8) | (b << 16);
GUI_SetColor(Color);
GUI_DrawHLine(y, x0, x1);
}
}
/*********************************************************************
*
* _MessageBox
*
* Purpose:
* Creates and executes a modal message box
*/
static void _MessageBox(const char * pText, const char * pCaption) {
WM_HWIN hBox;
hBox = MESSAGEBOX_Create(pText, pCaption, GUI_MESSAGEBOX_CF_MODAL | GUI_MESSAGEBOX_CF_MOVEABLE);
WM_SetStayOnTop(hBox, 1);
WM_BringToTop(hBox);
GUI_ExecCreatedDialog(hBox);
WM_SetFocus(_hMenu);
MENU_SetSel(_hMenu, -1);
}
/*********************************************************************
*
* _cbClient
*
* Purpose:
* Callback routine of client window which 'owns' the menu widget
* and the status bar
*/
static void _cbClient(WM_MESSAGE * pMsg) {
char acBuffer[50];
int Index, xSize, ySize, xPos, yPos;
WM_HWIN hWin, hClient;
MENU_MSG_DATA * pData;
MENU_ITEM_DATA Data;
hWin = pMsg->hWin;
hClient = WM_GetClientWindow(hWin);
xSize = WM_GetWindowSizeX(hClient);
ySize = WM_GetWindowSizeY(hClient);
switch (pMsg->MsgId) {
case WM_SIZE:
//
// Adjusts text widget on changinmg the size
//
xPos = WM_GetWindowOrgX(hClient);
yPos = WM_GetWindowOrgY(hClient);
WM_SetWindowPos(_hText, xPos + 4, yPos + ySize - 10, xSize, 10);
WM_SetWindowPos(_hMenu, xPos , yPos , xSize, WM_GetWindowSizeY(_hMenu));
TEXT_SetText(_hText, "Ready");
break;
case WM_PAINT:
//
// Draws the background of the client window
//
_DrawGradientV(0, 0, xSize - 1, ySize - 12 - 1, GUI_WHITE, GUI_LIGHTBLUE);
GUI_SetColor(GUI_LIGHTGRAY);
GUI_FillRect(0, ySize - 12, xSize - 1, ySize - 1);
GUI_SetTextMode(GUI_TM_TRANS);
GUI_SetColor(GUI_BLACK);
GUI_SetFont(&GUI_Font24B_ASCII);
GUI_DispStringHCenterAt(
"Welcome"
, xSize / 2, 20);
GUI_SetFont(&GUI_Font16B_ASCII);
GUI_DispStringHCenterAt(
"sia\\n"
"2014\\n"
"5\\n"
"29\\n"
, xSize / 2, 70);
break;
case WM_MENU:
pData = (MENU_MSG_DATA*)pMsg->Data.p;
switch (pData->MsgType) {
case MENU_ON_ITEMPRESSED:
//
// This message is send regardless of the item state is disabled or not
//
MENU_GetItem(pMsg->hWinSrc, pData->ItemId, &Data);
if (Data.Flags & MENU_IF_DISABLED) {
_MessageBox("The pressed item was disabled", "Message");
}
break;
case MENU_ON_ITEMACTIVATE:
//
// This message is send on highlighting a menu item
//
Index = pData->ItemId - ID_MENU_FILE_NEW;
if (Index >= 0) {
TEXT_SetText(_hText, _paDescription[pData->ItemId - ID_MENU_FILE_NEW]);
} else {
TEXT_SetText(_hText, "Ready");
}
break;
case MENU_ON_ITEMSELECT:
//
// This message is send only if an enabled item has been selected
//
sprintf(acBuffer, "ID of the selected\\nitem is 0x%X", pData->ItemId);
_MessageBox(acBuffer, "Message");
break;
}
}
WM_DefaultProc(pMsg);
}
/*********************************************************************
*
* Public code
*
**********************************************************************
*/
/*********************************************************************
*
* MainTask
*/
void MainTask(void) {
int xSize, ySize;
WM_HWIN hClient;
/*
#if GUI_SUPPORT_MEMDEV
WM_SetCreateFlags(WM_CF_MEMDEV);
WM_EnableMemdev(WM_HBKWIN);
#endif
*/
WM_SetDesktopColor(GUI_BLACK);
MENU_SetDefaultEffect(&WIDGET_Effect_3D1L);
//
// Create frame window
//
_hFrame = FRAMEWIN_CreateEx(0, 0, 240, 320, WM_HBKWIN, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE, 0, "Main Menu", _cbClient);
FRAMEWIN_SetFont(_hFrame, &GUI_Font13_ASCII);
FRAMEWIN_AddMaxButton(_hFrame, FRAMEWIN_BUTTON_RIGHT, 0);
FRAMEWIN_AddMinButton(_hFrame, FRAMEWIN_BUTTON_RIGHT, 2);
//
// Create menu widget
//
_hMenu = _CreateMenu(_hFrame);
//
// Create text widget as simple status bar
//
hClient = WM_GetClientWindow(_hFrame);
xSize = WM_GetWindowSizeX(hClient);
ySize = WM_GetWindowSizeY(hClient);
_hText = TEXT_CreateEx(4, ySize - 10, xSize, 10, hClient, WM_CF_SHOW, 0, GUI_ID_TEXT0, "Ready");
TEXT_SetFont(_hText, &GUI_Font8_ASCII);
//
// Set keyboard focus to menu widget
//
WM_SetFocus(_hMenu);
MENU_SetSel(_hMenu, -1);
//while (1) {
// GUI_Delay(100);
//}
} |
|