GacUI学习(一)之高仿系统记事本(二)

转载请注明来源:http://www.cnblogs.com/lyfh/p/6107614.html

上篇《GacUI学习(一)之高仿系统记事本(一)》已经将全局排版布局搞定了,本篇将创建菜单,并进行处理菜单事件。

记事本菜单主要有文件、编辑、格式、查看、帮助这五个,故可以在TextBoxEditorWindow类中添加一个GuiToolstripMenuBar类型的主菜单(MainMenu)用来放置五个GuiToolstripButton类型的菜单,然后每个GuiToolstripButton类的菜单里面再次添加相应的GuiToolstripButton类的子菜单。具体代码如下

 private:

     //GuiControls
GuiToolstripMenuBar* MainMenu; //主菜单
GuiToolstripButton* FileMenu; //"文件"菜单
GuiToolstripButton* NewMenu; //"新建"菜单
GuiToolstripButton* OpenMenu; //"打开"菜单
GuiToolstripButton* SaveMenu; //"保存"菜单
GuiToolstripButton* SaveAsMenu; //"另存为"菜单
GuiToolstripButton* SetPageMenu; //"页面设置"菜单
GuiToolstripButton* PrintMenu; //"打印"菜单
GuiToolstripButton* ExitMenu; //"退出"菜单 GuiToolstripButton* EditMenu; //"编辑"菜单
GuiToolstripButton* UndoMenu; //"撤销"菜单
GuiToolstripButton* CutMenu; //"剪切"菜单
GuiToolstripButton* CopyMenu; //"复制"菜单
GuiToolstripButton* PasteMenu; //"粘贴"菜单
GuiToolstripButton* DeleteMenu; //"删除"菜单
GuiToolstripButton* FindMenu; //"查找"菜单
GuiToolstripButton* FindNextMenu; //"查找下一个"菜单
GuiToolstripButton* ReplaceMenu; //"替换"菜单
GuiToolstripButton* GotoMenu; //"转到"菜单
GuiToolstripButton* SelectAllMenu; //"全选"菜单
GuiToolstripButton* DateMenu; //"时间/日期"菜单 GuiToolstripButton* FormatMenu; //"格式"菜单
GuiToolstripButton* AutoWrapMenu; //"自动换行"
GuiToolstripButton* FontTypeMenu; //"字体" GuiToolstripButton* ViewMenu; //"查看"菜单
GuiToolstripButton* StatusBarMenu; //"状态栏" GuiToolstripButton* HelpMenu; //"帮助"菜单
GuiToolstripButton* AboutMenu; //"关于" GuiMultilineTextBox* MainTextBox; //最主要的一个多行编辑框

具体意义可以参见注释,因为菜单事件最后会设计中间的编辑框(MainTextBox)的操作,所以我们本篇也将创建出中间的编辑框。按照从上往下的顺序,先创建菜单,同样在构造函数中

 //下面自创一个菜单栏
{
GuiCellComposition* MainMenuCell = new GuiCellComposition;
table->AddChild(MainMenuCell);
MainMenuCell->SetSite(, , , ); //创建主菜单
GuiToolstripMenuBar* MainMenu = g::NewMenuBar();
MainMenu->GetBoundsComposition()->SetMinSizeLimitation(GuiGraphicsComposition::LimitToElementAndChildren);
MainMenu->GetBoundsComposition()->SetAlignmentToParent(Margin(, , , )); MainMenuCell->AddChild(MainMenu->GetBoundsComposition());
//文件菜单
{
FileMenu = g::NewMenuBarButton();
FileMenu->SetText(L"文件(F)");
FileMenu->SetAlt(L"F");
MainMenu->GetToolstripItems().Add(FileMenu); //文件子菜单
{
//"新建"菜单项
NewMenu = g::NewMenuItemButton();
NewMenu->SetText(L"新建(M)");
NewMenu->SetAlt(L"N");
NewMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::NewMenu_OnClicked);
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(NewMenu);
//"打开"菜单项
OpenMenu = g::NewMenuItemButton();
OpenMenu->SetText(L"打开(O)...");
OpenMenu->SetAlt(L"O");
OpenMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::OpenMenu_OnClicked);
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(OpenMenu);
//"保存"菜单项
SaveMenu = g::NewMenuItemButton();
SaveMenu->SetText(L"保存(S)");
SaveMenu->SetAlt(L"S");
SaveMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::SaveMenu_OnClicked);
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(SaveMenu);
//"另存为"菜单
SaveAsMenu = g::NewMenuItemButton();
SaveAsMenu->SetText(L"另存为(A)...");
SaveAsMenu->SetAlt(L"A");
SaveAsMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::SaveAsMenu_OnClicked);
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(SaveAsMenu);
//分割条
GuiControl* FileSplitter1 = g::NewMenuSplitter();
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(FileSplitter1);
//"页面设置"菜单
SetPageMenu = g::NewMenuItemButton();
SetPageMenu->SetText(L"页面设置(U)...");
SetPageMenu->SetAlt(L"U");
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(SetPageMenu);
//"打印"菜单
PrintMenu = g::NewMenuItemButton();
PrintMenu->SetText(L"打印(P)...");
PrintMenu->SetAlt(L"P");
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(PrintMenu);
//分割条
GuiControl* FileSplitter2 = g::NewMenuSplitter();
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(FileSplitter2);
//"退出"菜单项
ExitMenu = g::NewMenuItemButton();
ExitMenu->SetText(L"退出(X)");
ExitMenu->SetAlt(L"X");
ExitMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::ExitMenu_OnClicked);
FileMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(ExitMenu);
}
} //编辑菜单
{
EditMenu = g::NewMenuBarButton();
EditMenu->SetText(L"编辑(E)");
EditMenu->SetAlt(L"E");
MainMenu->GetToolstripItems().Add(EditMenu); //编辑子菜单
{
//"撤销"菜单
UndoMenu = g::NewMenuItemButton();
UndoMenu->SetText(L"撤销(U)");
UndoMenu->SetAlt(L"U");
UndoMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::UndoMenu_OnClicked);
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(UndoMenu); GuiControl* EditSplitter1 = g::NewMenuSplitter();
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(EditSplitter1); //"剪切"菜单
CutMenu = g::NewMenuItemButton();
CutMenu->SetText(L"剪切(T)");
CutMenu->SetAlt(L"T");
CutMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::CutMenu_OnClicked);
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(CutMenu); //"复制"菜单
CopyMenu = g::NewMenuItemButton();
CopyMenu->SetText(L"复制(C)");
CopyMenu->SetAlt(L"C");
CopyMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::CopyMenu_OnClicked);
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(CopyMenu); //"粘贴"菜单
PasteMenu = g::NewMenuItemButton();
PasteMenu->SetText(L"粘贴(P)");
PasteMenu->SetAlt(L"P");
PasteMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::PasteMenu_OnClicked);
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(PasteMenu); //"删除"菜单
DeleteMenu = g::NewMenuItemButton();
DeleteMenu->SetText(L"删除(L)");
DeleteMenu->SetAlt(L"L");
DeleteMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::DeleteMenu_OnClicked);
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(DeleteMenu); GuiControl* EditSplitter2 = g::NewMenuSplitter();
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(EditSplitter2); //查找
FindMenu = g::NewMenuItemButton();
FindMenu->SetText(L"查找(F)...");
FindMenu->SetAlt(L"F");
FindMenu->SetShortcutText(L"Ctrl+F"); FindMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::FindMenu_OnClicked);
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(FindMenu); //查找下一个
FindNextMenu = g::NewMenuItemButton();
FindNextMenu->SetText(L"查找下一个(N)");
FindNextMenu->SetAlt(L"N");
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(FindNextMenu); //替换
ReplaceMenu = g::NewMenuItemButton();
ReplaceMenu->SetText(L"替换(R)...");
ReplaceMenu->SetAlt(L"R");
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(ReplaceMenu); //转到
GotoMenu = g::NewMenuItemButton();
GotoMenu->SetText(L"转到(G)...");
GotoMenu->SetAlt(L"G");
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(GotoMenu); GuiControl* EditSplitter3 = g::NewMenuSplitter();
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(EditSplitter3); //全选
SelectAllMenu = g::NewMenuItemButton();
SelectAllMenu->SetText(L"全选(A)");
SelectAllMenu->SetAlt(L"A");
SelectAllMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::SelectAllMenu_OnClicked);
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(SelectAllMenu); //时间/日期
DateMenu = g::NewMenuItemButton();
DateMenu->SetText(L"时间/日期(D)");
DateMenu->SetAlt(L"D");
DateMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::DateMenu_OnClicked);
EditMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(DateMenu);
}
} //"格式"菜单
{
FormatMenu = g::NewMenuBarButton();
FormatMenu->SetText(L"格式(O)");
FormatMenu->SetAlt(L"O");
MainMenu->GetToolstripItems().Add(FormatMenu); //格式子菜单
{
//自动换行
AutoWrapMenu = g::NewMenuItemButton();
AutoWrapMenu->SetText(L"自动换行(W)");
AutoWrapMenu->SetAlt(L"W");
FormatMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(AutoWrapMenu); //字体
FontTypeMenu = g::NewMenuItemButton();
FontTypeMenu->SetText(L"字体(F)...");
FontTypeMenu->SetAlt(L"F");
FormatMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(FontTypeMenu);
}
} //"查看"菜单
{
ViewMenu = g::NewMenuBarButton();
ViewMenu->SetText(L"查看(V)");
ViewMenu->SetAlt(L"V");
MainMenu->GetToolstripItems().Add(ViewMenu); //查看子菜单
{
StatusBarMenu = g::NewMenuItemButton();
StatusBarMenu->SetText(L"状态栏(S)");
StatusBarMenu->SetAlt(L"S"); ViewMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(StatusBarMenu);
}
} //"帮助"菜单
{
HelpMenu = g::NewMenuBarButton();
HelpMenu->SetText(L"帮助(H)");
HelpMenu->SetAlt(L"H");
MainMenu->GetToolstripItems().Add(HelpMenu); //帮助子菜单
{
AboutMenu = g::NewMenuItemButton();
AboutMenu->SetText(L"关于GacTextBox");
AboutMenu->SetAlt(L"A");
HelpMenu->EnsureToolstripSubMenu()->GetToolstripItems().Add(AboutMenu);
}
}
}

按照上篇所述,创建菜单项,首先要创建一个GuiCellComposition,然后将此GuiCellComposition添加到前面创建的table里面,并SetSite()到第一行第一列,因为table本身就只有一列,故设置此GuiCellComposition占据一列。值得一提的是当我们把一个控件或者一个排版布局添加到它的父时,GacUI为自行处理控件,并管理对应的内存,所以此处我们直接new出类,然后添加,后面不需要delete,否则会造成二次delete。GacUI已经处理了常见的快捷键,所以创建菜单时,可以直接在对应的菜单上面SetAlt()即可设置Alt+快捷键。

上面的代码其实没有写完,只实现了对部分菜单的事件处理。对于菜单以及没有用到的按钮类,基本事件就是鼠标点击(Click),此处正是用了Clicked事件,然后将事件与对应的 MenuName_OnClick()“链接”起来,eg:

UndoMenu->Clicked.AttachMethod(this, &TextBoxEditorWindow::UndoMenu_OnClicked);

此处的UndoMenu_OnClicked即为我们自定义的一个事件处理函数,此函数有固定格式,用到了模板,本人对C++模板几乎完全不懂,故不作说明,此处可以固定为

void Func(GuiGraphicsComposition* sender, GuiEventArgs& arguments)

以此类推,对应的代码如下:

 void TextBoxEditorWindow::UpdateEditMenuStatus()
{
UndoMenu->SetEnabled(MainTextBox->CanUndo());
CutMenu->SetEnabled(MainTextBox->CanCut());
CopyMenu->SetEnabled(MainTextBox->CanCopy());
PasteMenu->SetEnabled(MainTextBox->CanPaste());
DeleteMenu->SetEnabled(MainTextBox->CanCut()); } void TextBoxEditorWindow::window_OnClipboardUpdated(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
UpdateEditMenuStatus();
} void TextBoxEditorWindow::NewMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
GetCurrentController()->DialogService()->ShowMessageBox(this->GetNativeWindow(),
L"功能暂未实现!",
L"GacTextBox",
INativeDialogService::DisplayOK,
INativeDialogService::DefaultFirst,
INativeDialogService::IconInformation,
INativeDialogService::ModalWindow);
} void TextBoxEditorWindow::OpenMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
GuiOpenFileDialog* OpenFile = new GuiOpenFileDialog;
OpenFile->SetFilter(L"文本文档(*.txt)|*.txt|所有文件(*.*)|*.*");
OpenFile->SetDefaultExtension(L"txt");
this->AddComponent(OpenFile); if (OpenFile->ShowDialog())
{
CurrentFilePath = OpenFile->GetFileName();
this->SelectOpenModeComboBox->SetSelectedIndex();
//默认以Ansi方式读取文件
WString OpenFileData = ReadFileByAnsi(CurrentFilePath);
//WString OpenFileData = ReadFileByUTF8(CurrentFilePath);
//WString OpenFileData = ReadFileByUTF16LE(CurrentFilePath); MainTextBox->SetText(OpenFileData);
WString WindowsTitle = CurrentFilePath + L" -GacTextBox";
this->SetText(WindowsTitle);
} this->RemoveComponent(OpenFile);
delete OpenFile;
} void TextBoxEditorWindow::SaveMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
//如果MainTextBox内容发生改变,点击保存按钮才有反应
if (bIsTextChanged == true)
{
//如果CurrentPath为空,则表明没有打开文件
if (CurrentFilePath.Length() == )
{
GuiSaveFileDialog* SaveFile = new GuiSaveFileDialog;
SaveFile->SetFilter(L"文本文档(*.txt)|*.txt|所有文件(*.*)|*.*");
SaveFile->SetDefaultExtension(L"txt");
SaveFile->SetTitle(L"保存");
this->AddComponent(SaveFile); if (SaveFile->ShowDialog())
{
CurrentFilePath = SaveFile->GetFileName();
}
else
return;
this->RemoveComponent(SaveFile);
delete SaveFile;
} bool bResult;
if (CurrentEncoding == L"UTF8")
{
bResult = WriteFileByUTF8(CurrentFilePath, MainTextBox->GetText());
}
else if (CurrentEncoding == L"UTF16LE")
{
bResult = WriteFileByUTF16LE(CurrentFilePath, MainTextBox->GetText());
}
else
{
bResult = WriteFileByAnsi(CurrentFilePath, MainTextBox->GetText());
} this->SetText(CurrentFilePath + L" -GacTextBox");
}
} void TextBoxEditorWindow::SaveAsMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
GuiSaveFileDialog* SaveFile = new GuiSaveFileDialog;
SaveFile->SetFilter(L"文本文档(*.txt)|*.txt|所有文件(*.*)|*.*");
SaveFile->SetDefaultExtension(L"txt");
SaveFile->SetTitle(L"保存");
SaveFile->SetOptions(INativeDialogService::FileDialogPromptOverwriteFile);
this->AddComponent(SaveFile); if (SaveFile->ShowDialog())
{
CurrentFilePath = SaveFile->GetFileName();
}
else
return;
this->RemoveComponent(SaveFile);
delete SaveFile; bool bResult;
if (CurrentEncoding == L"UTF8")
{
bResult = WriteFileByUTF8(CurrentFilePath, MainTextBox->GetText());
}
else if (CurrentEncoding == L"UTF16LE")
{
bResult = WriteFileByUTF16LE(CurrentFilePath, MainTextBox->GetText());
}
else
{
bResult = WriteFileByAnsi(CurrentFilePath, MainTextBox->GetText());
} /*
stream::FileStream* SaveFileStream = new stream::FileStream(CurrentFilePath, stream::FileStream::WriteOnly);
if (!SaveFileStream->IsAvailable())
{
GetCurrentController()->DialogService()->ShowMessageBox(this->GetNativeWindow(),
L"保存文件失败!",
L"GacTextBox",
INativeDialogService::DisplayOK,
INativeDialogService::DefaultFirst,
INativeDialogService::IconInformation,
INativeDialogService::ModalWindow);
return;
} SaveFileStream->Write((void*)(MainTextBox->GetText().Buffer()),
MainTextBox->GetText().Length());
delete SaveFileStream;
*/
this->SetText(CurrentFilePath + L" -GacTextBox"); } void TextBoxEditorWindow::ExitMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
PostQuitMessage();
} void TextBoxEditorWindow::UndoMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
MainTextBox->Undo();
} void TextBoxEditorWindow::PasteMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
MainTextBox->Paste();
MainTextBox->SetFocus();
} void TextBoxEditorWindow::CopyMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
MainTextBox->Copy();
MainTextBox->SetFocus();
} void TextBoxEditorWindow::CutMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
MainTextBox->Cut();
MainTextBox->SetFocus();
} void TextBoxEditorWindow::DeleteMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
MainTextBox->SetSelectionText(L"");
MainTextBox->SetFocus();
} void TextBoxEditorWindow::SelectAllMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
MainTextBox->SelectAll();
MainTextBox->SetFocus();
} void TextBoxEditorWindow::FindMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
FindTextWindow* findtextwindow = new FindTextWindow;
findtextwindow->GetNativeWindow()->SetParent(this->GetNativeWindow());
findtextwindow->MoveToScreenCenter();
findtextwindow->Show(); } void TextBoxEditorWindow::DateMenu_OnClicked(GuiGraphicsComposition * sender, GuiEventArgs & arguments)
{
SYSTEMTIME systime;
GetLocalTime(&systime);
wchar_t *szTimeW = new wchar_t[];
memset(szTimeW, NULL, * sizeof(wchar_t));
swprintf_s(szTimeW, , L"%4d/%02d/%02d %02d:%02d", systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute); WString TimeStrW = szTimeW;
MainTextBox->SetSelectionText(TimeStrW);
delete[] szTimeW;
}

由于此程序没有写完,所以此代码也不是完整的,待我完成整个程序后再更新

05-04 10:54