⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tdelwin.tex

📁 很牛的GUI源码wxWidgets-2.8.0.zip 可在多种平台下运行.
💻 TEX
字号:
\section{Window deletion overview}\label{windowdeletionoverview}Classes: \helpref{wxCloseEvent}{wxcloseevent}, \helpref{wxWindow}{wxwindow}Window deletion can be a confusing subject, so this overview is providedto help make it clear when and how you delete windows, or respond to user requeststo close windows.\wxheading{What is the sequence of events in a window deletion?}When the user clicks on the system close button or system close command,in a frame or a dialog, wxWidgets calls \helpref{wxWindow::Close}{wxwindowclose}. Thisin turn generates an EVT\_CLOSE event: see \helpref{wxCloseEvent}{wxcloseevent}.It is the duty of the application to define a suitable event handler, anddecide whether or not to destroy the window.If the application is for some reason forcing the application to close(\helpref{wxCloseEvent::CanVeto}{wxcloseeventcanveto} returns false), the window should always be destroyed, otherwise there is the option toignore the request, or maybe wait until the user has answered a questionbefore deciding whether it is safe to close. The handler for EVT\_CLOSE shouldsignal to the calling code if it does not destroy the window, by calling \helpref{wxCloseEvent::Veto}{wxcloseeventveto}. Calling this provides useful informationto the calling code.The wxCloseEvent handler should only call \helpref{wxWindow::Destroy}{wxwindowdestroy} todelete the window, and not use the {\bf delete} operator. This is becausefor some window classes, wxWidgets delays actual deletion of the window until all events have been processed,since otherwise there is the danger that events will be sent to a non-existent window.As reinforced in the next section, calling Close does not guarantee that the windowwill be destroyed. Call \helpref{wxWindow::Destroy}{wxwindowdestroy} if you want to becertain that the window is destroyed.\wxheading{How can the application close a window itself?}Your application can either use \helpref{wxWindow::Close}{wxwindowclose} event just asthe framework does, or it can call \helpref{wxWindow::Destroy}{wxwindowdestroy} directly.If using Close(), you can pass a true argument to this function to tell the event handlerthat we definitely want to delete the frame and it cannot be vetoed.The advantage of using Close instead of Destroy is that it will call any clean-up codedefined by the EVT\_CLOSE handler; for example it may close a document contained ina window after first asking the user whether the work should be saved. Close can be vetoedby this process (return false), whereas Destroy definitely destroys the window.\wxheading{What is the default behaviour?}The default close event handler for wxDialog simulates a Cancel command,generating a wxID\_CANCEL event. Since the handler for this cancel event mightitself call {\bf Close}, there is a check for infinite looping. The default handlerfor wxID\_CANCEL hides the dialog (if modeless) or calls EndModal(wxID\_CANCEL) (if modal).In other words, by default, the dialog {\it is not destroyed} (it might have been createdon the stack, so the assumption of dynamic creation cannot be made).The default close event handler for wxFrame destroys the frame using Destroy().\wxheading{What should I do when the user calls up Exit from a menu?}You can simply call \helpref{wxWindow::Close}{wxwindowclose} on the frame. Thiswill invoke your own close event handler which may destroy the frame.You can do checking to see if your application can be safely exited at this point,either from within your close event handler, or from within your exit menu commandhandler. For example, you may wish to check that all files have been saved.Give the user a chance to save and quit, to not save but quit anyway, or to cancelthe exit command altogether.\wxheading{What should I do to upgrade my 1.xx OnClose to 2.0?}In wxWidgets 1.xx, the {\bf OnClose} function did not actually delete 'this', but signaledto the calling function (either {\bf Close}, or the wxWidgets framework) to deleteor not delete the window.To update your code, you should provide an event table entry in your frame ordialog, using the EVT\_CLOSE macro. The event handler function might look like this:{\small%\begin{verbatim}  void MyFrame::OnCloseWindow(wxCloseEvent& event)  {    if (MyDataHasBeenModified())    {      wxMessageDialog* dialog = new wxMessageDialog(this,        "Save changed data?", "My app", wxYES_NO|wxCANCEL);      int ans = dialog->ShowModal();      dialog->Destroy();      switch (ans)      {        case wxID_YES:      // Save, then destroy, quitting app          SaveMyData();          this->Destroy();          break;        case wxID_NO:       // Don't save; just destroy, quitting app          this->Destroy();          break;        case wxID_CANCEL:   // Do nothing - so don't quit app.        default:          if (!event.CanVeto()) // Test if we can veto this deletion            this->Destroy();    // If not, destroy the window anyway.          else            event.Veto();     // Notify the calling code that we didn't delete the frame.          break;      }    }  }\end{verbatim}}%\wxheading{How do I exit the application gracefully?}A wxWidgets application automatically exits when the last top level window(\helpref{wxFrame}{wxframe} or \helpref{wxDialog}{wxdialog}), is destroyed. Putany application-wide cleanup code in \helpref{wxApp::OnExit}{wxapponexit} (thisis a virtual function, not an event handler).\wxheading{Do child windows get deleted automatically?}Yes, child windows are deleted from within the parent destructor. This includes any childrenthat are themselves frames or dialogs, so you may wish to close these child frame or dialog windowsexplicitly from within the parent close handler.\wxheading{What about other kinds of window?}So far we've been talking about `managed' windows, i.e. frames and dialogs. Windowswith parents, such as controls, don't have delayed destruction and don't usually haveclose event handlers, though you can implement them if you wish. For consistency,continue to use the \helpref{wxWindow::Destroy}{wxwindowdestroy} function insteadof the {\bf delete} operator when deleting these kinds of windows explicitly.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -