源項目地址:https://github.com/Microsoft/...
如下是把樣例轉換爲簡要說明,同時給出實際運行效果及關鍵代碼:git
經過Thumb控件移動無窗口工做區的可控大小,及可拖動窗體。
github
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DragMove(); } private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e) { // Resize window width (honoring minimum width) var desiredWidth = (int) (ActualWidth + e.HorizontalChange); var minWidth = (int) (MinWidth + resizeThumb.Width + resizeThumb.Margin.Right); Width = Math.Max(desiredWidth, minWidth); // Resize window height (honoring minimum height) var desiredHeight = (int) (ActualHeight + e.VerticalChange); var minHeight = (int) (MinHeight + resizeThumb.Height + resizeThumb.Margin.Bottom); Height = Math.Max(desiredHeight, minHeight); }
查找關鍵代碼:session
private void findNextButton_Click(object sender, RoutedEventArgs e) { // Find matches if (_matches == null) { var pattern = findWhatTextBox.Text; // Match whole word? if ((bool) matchWholeWordCheckBox.IsChecked) pattern = @"(?<=\W{0,1})" + pattern + @"(?=\W)"; // Case sensitive if (!(bool) caseSensitiveCheckBox.IsChecked) pattern = "(?i)" + pattern; // Find matches _matches = Regex.Matches(_textBoxToSearch.Text, pattern); _matchIndex = 0; // Word not found? if (_matches.Count == 0) { MessageBox.Show("'" + findWhatTextBox.Text + "' not found.", "Find"); _matches = null; return; } } // Start at beginning of matches if the last find selected the last match if (_matchIndex == _matches.Count) { var result = MessageBox.Show("Nmore matches found. Start at beginning?", "Find", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) return; // Reset _matchIndex = 0; } // Return match details to client so it can select the text var match = _matches[_matchIndex]; if (TextFound != null) { // Text found Index = match.Index; Length = match.Length; OnTextFound(); } _matchIndex++; } public event TextFoundEventHandler TextFound;//定義事件 protected virtual void OnTextFound() { var textFound = TextFound; textFound?.Invoke(this, EventArgs.Empty); } //主界面調用 private void editFindMenuItem_Click(object sender, RoutedEventArgs e) { // Instantiate the dialog box var dlg = new FindDialogBox(documentTextBox) {Owner = this}; // Configure the dialog box dlg.TextFound += dlg_TextFound; // Open the dialog box modally dlg.Show(); } private void dlg_TextFound(object sender, EventArgs e) { // Get the find dialog box that raised the event var dlg = (FindDialogBox) sender; // Get find results and select found text documentTextBox.Select(dlg.Index, dlg.Length); documentTextBox.Focus(); }
關鍵代碼:app
// Show message box, passing the window owner if specified MessageBoxResult result; if (owner == null) { result = System.Windows.MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options); } else { result = System.Windows.MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options); } ps:一個小bug,當owner爲this/null時,options使用DefaultDesktopOnly與ServiceNotification參數時,顯示 報錯/移動窗口殘影,建議過濾掉這兩個參數。
代碼見圖片內
ide
private void Click(object sender, RoutedEventArgs e) { // Configure and show a notification icon in the system tray _notifyIcon = new NotifyIcon { BalloonTipText = @"Hello, NotifyIcon!", Text = @"Hello, NotifyIcon!", Icon = new Icon("NotifyIcon.ico"), Visible = true }; _notifyIcon.ShowBalloonTip(1000); }
ps:調用GetWindowPlacement/SetWindowPlacement函數
protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); try { // Load window placement details for previous application session from application settings // Note - if window was closed on a monitor that is now disconnected from the computer, // SetWindowPlacement will place the window onto a visible monitor. var wp = Settings.Default.WindowPlacement; wp.length = Marshal.SizeOf(typeof (WindowPlacement)); wp.flags = 0; wp.showCmd = (wp.showCmd == SwShowminimized ? SwShownormal : wp.showCmd); var hwnd = new WindowInteropHelper(this).Handle; SetWindowPlacement(hwnd, ref wp); } catch { // ignored } } // WARNING - Not fired when Application.SessionEnding is fired protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); // Persist window placement details to application settings WindowPlacement wp; var hwnd = new WindowInteropHelper(this).Handle; GetWindowPlacement(hwnd, out wp); Settings.Default.WindowPlacement = wp; Settings.Default.Save(); } #region Win32 API declarations to set and get window placement [DllImport("user32.dll")] private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WindowPlacement lpwndpl); [DllImport("user32.dll")] private static extern bool GetWindowPlacement(IntPtr hWnd, out WindowPlacement lpwndpl); private const int SwShownormal = 1; private const int SwShowminimized = 2; #endregion }
打開模式/非模式窗口是是否激活,是否引起其 Activated 事件this
private void ShowWindowActivateButton_Click(object sender, RoutedEventArgs e) { var tw = new ChildWindow(); // tw.ShowActivated = true; // true is the default value tw.Show(); } private void ShowWindowNoActivateButton_Click(object sender, RoutedEventArgs e) { var tw = new ChildWindow {ShowActivated = false}; tw.Show(); }
private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { // Get date/time the window is shown now (ie when it becomes visible) if ((bool) e.NewValue) { shownThisTimeTextBlock.Text = DateTime.Now.ToString(CultureInfo.InvariantCulture); } } private void Window_Closing(object sender, CancelEventArgs e) { // If Close() was called, close the window (instead of hiding it) if (_close) return; // Hide the window (instead of closing it) e.Cancel = true; Hide(); }
1 主函數判斷導航窗口返回值及接收數據顯示結果,spa
private void RunWizardButton_Click(object sender, RoutedEventArgs e) { var wizard = new WizardDialogBox(); var showDialog = wizard.ShowDialog(); var dialogResult = showDialog != null && (bool) showDialog; MessageBox.Show( dialogResult ? $"{wizard.WizardData.DataItem1}\n{wizard.WizardData.DataItem2}\n{wizard.WizardData.DataItem3}" : "Canceled."); }
2 DialogBox加載wizardLauncher嚮導啓動器導航窗口、數據、委託事件、返回函數等code
public class WizardDialogBox : NavigationWindow { public WizardDialogBox() { InitializeComponent(); // Launch the wizard var wizardLauncher = new WizardLauncher(); wizardLauncher.WizardReturn += wizardLauncher_WizardReturn; Navigate(wizardLauncher); } public WizardData WizardData { get; private set; } private void wizardLauncher_WizardReturn(object sender, WizardReturnEventArgs e) { // Handle wizard return WizardData = e.Data as WizardData; if (DialogResult == null) { DialogResult = (e.Result == WizardResult.Finished); } } }
3 啓動器代碼orm
public class WizardLauncher : PageFunction<WizardResult> { private readonly WizardData _wizardData = new WizardData(); public event WizardReturnEventHandler WizardReturn; protected override void Start() { base.Start(); // So we remember the WizardCompleted event registration KeepAlive = true; // Launch the wizard var wizardPage1 = new WizardPage1(_wizardData); wizardPage1.Return += wizardPage_Return; NavigationService?.Navigate(wizardPage1); } public void wizardPage_Return(object sender, ReturnEventArgs<WizardResult> e) { // Notify client that wizard has completed // NOTE: We need this custom event because the Return event cannot be // registered by window code - if WizardDialogBox registers an event handler with // the WizardLauncher's Return event, the event is not raised. WizardReturn?.Invoke(this, new WizardReturnEventArgs(e.Result, _wizardData)); OnReturn(null); } }
4 Page1代碼,綁定、下一頁、取消、返回事件功能
public partial class WizardPage1 : PageFunction<WizardResult> { public WizardPage1(WizardData wizardData) { InitializeComponent(); // Bind wizard state to UI DataContext = wizardData; } private void nextButton_Click(object sender, RoutedEventArgs e) { // Go to next wizard page var wizardPage2 = new WizardPage2((WizardData) DataContext); wizardPage2.Return += wizardPage_Return; NavigationService?.Navigate(wizardPage2); } private void cancelButton_Click(object sender, RoutedEventArgs e) { // Cancel the wizard and don't return any data OnReturn(new ReturnEventArgs<WizardResult>(WizardResult.Canceled)); } public void wizardPage_Return(object sender, ReturnEventArgs<WizardResult> e) { // If returning, wizard was completed (finished or canceled), // so continue returning to calling page OnReturn(e); } }