390 |
Project 2 |
CREATING AN APPLICATION USING MANAGED C++ |
|
|
pToolBar = new Windows::Forms::ToolBar(); |
|
|
pToolBar->ImageList = pImageList; |
|
|
|
|
pToolBar->DropDownArrows = true; |
|
|
|
|
pToolBar->ShowToolTips = true; |
|
|
|
|
System::Windows::Forms::ToolBarButton* rToolbarButtons[]=new |
|
System::Windows::Forms::ToolBarButton*[6]; |
|
|
|
rToolbarButtons[0]=pNewTBButton; |
|
Y |
|
|
rToolbarButtons[1]=pOpenTBButton; |
|
|
|
L |
|
|
rToolbarButtons[2]=pSaveTBButton; |
|
|
|
|
|
|
|
|
F |
|
|
rToolbarButtons[3]=pPreviewTBButton; |
|
|
|
rToolbarButtons[4]=pPrintTBButton; |
|
|
|
|
|
|
M |
|
|
|
rToolbarButtons[5]=pHelpTBButton; |
|
|
|
|
pToolBar->Buttons->AddRange(rToolbarButtons); |
|
|
|
E |
|
|
|
|
pToolBar->ButtonClick += new |
|
|
|
|
|
T |
|
|
|
|
System::Windows::Forms:: oolBarButtonClickEventHandler(this,&CMainWindow::Toolbar- |
|
Handler); |
A |
|
|
Control *rControls[] = new Control *[2]; rControls[0] = pStatusBar;
rControls[1]= pToolBar; this->Controls->AddRange(rControls);
}
void CMainWindow::MenuItemHandler(Object* sender, EventArgs* e)
{
if(sender->Equals(pNewMenuItem))
{
New();
}
else if(sender->Equals(pOpenMenuItem))
{
Open();
}
else if(sender->Equals(pSaveMenuItem))
{
Save();
}
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 391
else if(sender->Equals(pPrintPreviewMenuItem))
{
PrintPreview();
}
else if(sender->Equals(pPrintMenuItem))
{
Print();
}
else if(sender->Equals(pHelpAboutMenuItem))
{
AboutHelp();
}
else if(sender->Equals(pExitMenuItem))
{
Exit();
}
else if(sender->Equals(pCloseMenuItem))
{
CloseView();
}
else if(sender->Equals(pTileMenuItem))
{
Tile();
}
else if(sender->Equals(pCascadeMenuItem))
{
Cascade();
}
else if(sender->Equals(pClearAllMenuItem))
{
ClearAll();
}
else if(sender->Equals(pSaveAsMenuItem))
{
Save();
}
else if(sender->Equals(pNewWindowMenuItem))
{
392 Project 2 CREATING AN APPLICATION USING MANAGED C++
NewWindow();
}
else if(sender->Equals(pPenWidthMenuItem))
{
PenWidthsDlg();
}
else if(sender->Equals(pThickLineMenuItem))
{
ThickLine();
}
else if(sender->Equals(pHelpTopicsMenuItem))
{
HelpTopics();
}
else if(sender->Equals(pToolbarMenuItem))
{
pToolBar->Visible = !pToolBar->Visible ; pToolbarMenuItem->Checked = pToolBar->Visible;
}
else if(sender->Equals(pStatusbarMenuItem))
{
pStatusBar->Visible = !pStatusBar->Visible; pStatusbarMenuItem->Checked = pStatusBar->Visible;
}
else if(sender->Equals(pDrawLine))
{
DrawLine();
}
else if(sender->Equals(pDrawEllipse))
{
DrawEllipse();
}
else if(sender->Equals(pDrawRect))
{
DrawRect();
}
else if(sender->Equals(pDrawText))
{
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 393
DrawText();
}
else if(sender->Equals(pDrawFilledEllipse))
{
DrawFilledEllipse();
}
else if(sender->Equals(pDrawFilledRect))
{
DrawFilledRect();
}
else if(sender->Equals(pDelegateHandler))
{
HandleDelegate();
}
}
void CMainWindow::ToolbarHandler(Object* sender, ToolBarButtonClickEventArgs* e)
{
// Evaluate the Button property to determine which button was clicked. switch (this->pToolBar->Buttons->IndexOf(e->Button))
{
case 0: New(); break;
case 1: Open(); break;
case 2: Save(); break;
case 3: PrintPreview(); break;
case 4: Print(); break;
case 5:
394 Project 2 CREATING AN APPLICATION USING MANAGED C++
HelpTopics();
break;
default:
break;
}
}
//About GDIPlus
void CMainWindow::AboutHelp()
{
MessageBox::Show(S”GDIPlus Version 1.0”, S”About GDIPlus”);
}
//Help Topics
void CMainWindow::HelpTopics()
{
Help::ShowHelp(this, S”help\\GDIPlus.chm”);
}
//Print
void CMainWindow::Print()
{
try
{
pPrintDoc->Print();
}
catch(Exception* e)
{
MessageBox::Show(e->ToString());
}
}
//PrintPage
void CMainWindow::PrintPage(Object* sender, PrintPageEventArgs* printEv)
{
try{
CGDIPlusView* pActiveView = dynamic_cast<CGDIPlusView*>(this- >ActiveMdiChild);
CGDIPlusDoc* pActiveDoc = pActiveView->GetDocument();
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 395
for(int i=0; i < pActiveDoc->strokeList->Count; i++)
{
CStroke* st = dynamic_cast<CStroke*>(pActiveDoc->strokeList-
>Item[i]);
st->DrawStroke(printEv->Graphics) ;
}
printEv->HasMorePages = false;
}
catch (Exception* ex)
{
MessageBox::Show(ex->ToString());
}
}
//PrintPreview
void CMainWindow::PrintPreview()
{
try {
PrintPreviewDialog* pPrevDlg = new PrintPreviewDialog(); pPrevDlg->Document = pPrintDoc;
pPrevDlg->Size = System::Drawing::Size(600, 329); pPrevDlg->ShowDialog();
}
catch(Exception* ex)
{
MessageBox::Show(ex->ToString());
}
}
//Exit
void CMainWindow::Exit()
{
Form* paChildForm[] = this->MdiChildren ;
//Make sure to ask for saving the doc before exiting the app for(int i=0; i < paChildForm->Length ; i++)
396 Project 2 CREATING AN APPLICATION USING MANAGED C++
paChildForm[i]->Close();
Application::Exit();
}
//Close
void CMainWindow::CloseView()
{
CGDIPlusView* pActiveView = dynamic_cast<CGDIPlusView*> (this- >ActiveMdiChild);
pActiveView->Close();
}
//Tile_Click
void CMainWindow::Tile()
{
this->LayoutMdi(MdiLayout::TileHorizontal);
}
//Cascade_Click
void CMainWindow::Cascade()
{
this->LayoutMdi(MdiLayout::Cascade);
}
//ClearAll
void CMainWindow::ClearAll()
{
CGDIPlusView* pActiveView = dynamic_cast<CGDIPlusView*> (this- >ActiveMdiChild);
if(pActiveView)
{
CGDIPlusDoc* pActiveDoc = pActiveView->GetDocument(); pActiveDoc->DeleteContents();
}
}
//Open
void CMainWindow::Open()
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 397
{
OpenFileDialog* pOpenDlg = new OpenFileDialog();
pOpenDlg->Filter = “GDIPlus Files (*.scb)|*.scb|All Files (*.*)|*.*”; pOpenDlg->FileName = “” ;
pOpenDlg->DefaultExt = “.scb”; pOpenDlg->CheckFileExists = true; pOpenDlg->CheckPathExists = true;
int nRes = pOpenDlg->ShowDialog ();
if(nRes == DialogResult::OK)
{
if( !(pOpenDlg->FileName)->EndsWith(“.scb”) && !(pOpenDlg->FileName)- >EndsWith(“.SCB”))
MessageBox::Show(“Unexpected file format”,”GDIPlus”,MessageBoxButtons::OK );
else
{
CGDIPlusDoc* pNewDoc = CreateDocument(); pNewDoc->OpenDocument(pOpenDlg->FileName);
}
}
}
//Save
void CMainWindow::Save()
{
try
{
CGDIPlusView* pSelectedView = dynamic_cast<CGDIPlusView*>(this- >ActiveMdiChild);
SaveFileDialog* pSaveDlg = new SaveFileDialog();
pSaveDlg->Filter = “GDIPlus Files (*.scb)|*.scb|All Files (*.*)|*.*”; pSaveDlg->DefaultExt = “.scb”;
pSaveDlg->FileName = “GDIPlus1.scb”;
int nRes = pSaveDlg->ShowDialog(); if(nRes == DialogResult::OK)
398 Project 2 CREATING AN APPLICATION USING MANAGED C++
{
if(pSelectedView)
{
CGDIPlusDoc* pDoc = pSelectedView->GetDocument(); pDoc->SaveDocument(pSaveDlg->FileName);
}
}
}
catch(Exception* ex)
{
MessageBox::Show(ex->ToString());
}
}
//New
void CMainWindow::New()
{
//If this is the first child window, enable the Menu and Toolbar items if(!this->ActiveMdiChild)
EnableItems();
CreateDocument();
}
//NewWindow
void CMainWindow::NewWindow()
{
CGDIPlusView* pActiveView = dynamic_cast<CGDIPlusView*>(this->ActiveMdiChild); CGDIPlusView* pNewView = new CGDIPlusView(pActiveView->GetDocument(),
pParentWindow); pNewView->GetDocument()->viewList->Add(pNewView); pNewView->Show();
}
//ThickLine
void CMainWindow::ThickLine()
{
CGDIPlusView* pActiveView = dynamic_cast<CGDIPlusView*> (this- >ActiveMdiChild);
CREATING A MANAGED EXTENSIONS APPLICATION Chapter 12 399
CGDIPlusDoc* pActiveDoc = pActiveView->GetDocument(); pActiveDoc->bIsThickPen = !pActiveDoc->bIsThickPen; pActiveDoc->ReplacePen();
this->pThickLineMenuItem->Checked = pActiveDoc->bIsThickPen;
}
//PenWidthDlg
void CMainWindow::PenWidthsDlg()
{
Form* f = new Form();
//Get the document of active view
CGDIPlusView* pActiveView = dynamic_cast<CGDIPlusView*> (this- >ActiveMdiChild);
CGDIPlusDoc* pActiveDoc = pActiveView->GetDocument();
f->AutoScaleBaseSize = System::Drawing::Size(5, 13);
f->Text = “Pen Width”;
f->ClientSize = System::Drawing::Size(352, 125);
Button* button1 = new Button();
button1->Location = System::Drawing::Point(264, 20); button1->Size = System::Drawing::Size(75, 23); button1->TabIndex = 1;
button1->Text = “OK”;
button1->DialogResult = System::Windows::Forms::DialogResult::OK ;//Make this “OK” button
Button* button2 = new Button();
button2->Location = System::Drawing::Point(264, 52); button2->Size = System::Drawing::Size(75, 23); button2->TabIndex = 6;
button2->Text = “Cancel”;
TextBox* textBox1 = new TextBox();
textBox1->Location = System::Drawing::Point(120, 36); textBox1->Text = pActiveDoc->nThinWidth.ToString();