-
دوشنبه 02 شهریور 1388, 22:36 عصر
#3
کاربر تازه وارد
نقل قول: Windows Forms 2.0 Programming, 2nd Edition: Chapter 14: Applications
Collapse
// MDIParentForm.cs
partial class MDIParentForm : Form {
...
// This is necessary to bring the MDI parent window to the front,
// because Activate and BringToFront don't seem to have any effect.
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
public void CreateMDIChildWindow(string fileName) {
SetForegroundWindow(this.Handle);
// Detect whether file is already open
if( !string.IsNullOrEmpty(fileName) ) {
foreach( MDIChildForm openForm in this.MdiChildren ) {
if( string.Compare(openForm.FileName, fileName, true) == 0 ) {
openForm.Activate();
return;
}
}
}
// If file not open, open it
MDIChildForm form = new MDIChildForm();
form.OpenFile(fileName);
form.MdiParent = this;
form.Show();
}
void newToolStripMenuItem_Click(object sender, EventArgs e) {
this.CreateMDIChildWindow(null);
}
void openToolStripMenuItem_Click(object sender, EventArgs e) {
if( this.openFileDialog.ShowDialog() == DialogResult.OK ) {
this.CreateMDIChildWindow(this.openFileDialog.File Name);
}
}
...
} This code allows users to open a file using a menu strip item, and it lays the foundation for opening a file from the command line, including preventing the reopening of an already open file. We continue using WindowsFormsApplicationBase to achieve this, updating the earlier sample to acquire the command line arguments and pass them to the application main form's CreateMDIChildWindow method to open a file:
Collapse
// SingleMDIApplication.cs
class SingleMDIApplication : WindowsFormsApplicationBase {
static SingleMDIApplication application;
internal static SingleMDIApplication Application {
get {
if( application == null ) {
application = new SingleMDIApplication();
}
return application;
}
}
public SingleMDIApplication() {
// This ensures the underlying single-SDI framework is employed,
// and OnStartupNextInstance is fired
this.IsSingleInstance = true;
}
// Load MDI parent form and first MDI child form
protected override void OnCreateMainForm() {
this.MainForm = new MDIParentForm();
this.CreateMDIChildWindow(this.CommandLineArgs);
}
void CreateMDIChildWindow(ReadOnlyCollection<string> args) {
// Get file name, if provided
string fileName = (args.Count > 0 ? args[0] : null);
// Ask MDI parent to create a new MDI child
// and open file
((MDIParentForm)this.MainForm).CreateMDIChildWindo w(fileName);
}
} During construction, we specify that this application is a single-instance application. Unlike with multiple-SDI applications, however, we don't need to set the ShutdownStyle property because its value defaults to AfterMainFormCloses�exactly what is needed for an MDI application.
OnCreateMainForm creates the MDI parent form and sets it as the application's main form and the one responsible for creating MDI child windows. Then, the command line arguments are passed to the helper CreateMDIChildWindow method, which parses them for a file name. Either a file name or null is passed to the MDI parent form's version of CreateMDIChildWindow, which creates the new MDI child window, into which it loads a file; then CreateMDIChildWindow establishes the MDI parent-child relationship and shows the requested file. CreateMDIChildWindow also activates the MDI parent form to bring the application to the foreground.
In the second scenario, the desired processing is for the command line arguments to be passed from the second instance to the first, to which the first instance responds by processing the command line arguments and, if required, creating a new MDI child form. WindowsFormsApplicationBase handles the underlying mechanics of passing arguments from the second instance to the first, but it is up to you to process the command line arguments accordingly. You can achieve this by overriding WindowsFormsApplicationBase.OnStartupNextInstance, which passes the command line arguments via the CommandLine property of a StartupNextInstanceEventArgs object. The following code shows the OnStartupNextInstance override implementation:
Collapse
// SingleMDIApplication.cs
class SingleMDIApplication : WindowsFormsApplicationBase {
...
// Must call base constructor to ensure correct initial
// WindowsFormsApplicationBase configuration
public SingleMDIApplication() {...}
// Load MDI parent form and first MDI child form
protected override void OnCreateMainForm() {...}
// Load subsequent MDI child form
protected override void OnStartupNextInstance(
StartupNextInstanceEventArgs e) {
this.CreateMDIChildWindow (e.CommandLine);
}
void CreateMDIChildWindow(ReadOnlyCollection<string> args) {...}
} As you can see, centralizing CreateMDIChildWindow into a single helper method greatly simplifies the implementation of OnStartupNextInstance.
That's the complete solution, so let's look at how it operates. Suppose we start the application for the first time by executing the following statement from the command line:
Collapse
C:\SingleInstanceSample.exe C:\file1.txt The result is to load the application, configure the single-instance command line argument (passing support from our derivation of WindowsFormsApplicationBase), load the main MDI parent form, and, finally, open an MDI child form, displaying the file specified from the command line arguments. Figure 14.8 illustrates the result.
قوانین ایجاد تاپیک در تالار
- شما نمی توانید تاپیک جدید ایجاد کنید
- شما نمی توانید به تاپیک ها پاسخ دهید
- شما نمی توانید ضمیمه ارسال کنید
- شما نمی توانید پاسخ هایتان را ویرایش کنید
-
قوانین سایت