4 11建立和管理有模式對話方塊

2021-08-03 20:31:41 字數 2321 閱讀 6671

visual studio 2015

其他版本

若要了解有關 visual studio 2017 rc 的最新文件,請參閱 visual studio 2017 rc 文件。

在建立模式對話方塊在 visual studio 時,您必須確保顯示對話方塊中,同時禁用對話方塊中的父視窗,然後對話方塊中關閉後重新啟用父視窗。 如果不這樣做,可能會收到錯誤:"microsoft visual studio 不能關閉,因為模式對話方塊處於活動狀態。 關閉活動對話方塊,然後重試。"

有兩種方法執行此操作。 建議的方法,如果您有 wpf 對話方塊中,是派生它從dialogwindow,然後呼叫showmodal以顯示對話方塊。 如果這樣做,您不需要管理模式的父視窗的狀態。

如果您的對話方塊不是 wpf 中,或者對於某些其他原因不能派生您的對話方塊類派生dialogwindow,則必須通過呼叫獲取對話方塊中的父getdialogownerhwnd自行和管理模式狀態,通過呼叫enablemodeless引數為 0 (false) 在顯示對話方塊中,並關閉該對話方塊之後呼叫該方法再次使用的引數 1 (true) 之前的方法。

建立乙個名為的 vsix 專案opendialogtest並新增乙個名為的選單命令opendialog。 有關如何執行此操作的詳細資訊,請參閱使用選單命令建立擴充套件。

若要使用dialogwindow類,必須新增對下列程式集引用 (在框架選項卡的新增引用對話方塊中):

在 opendialog.cs,新增以下using語句︰c#

using microsoft.visualstudio.platformui;
宣告乙個名為類testdialogwindow派生的dialogwindow:c#

class testdialogwindow : dialogwindow  

若要能夠降至最低並最大化對話方塊中,將設定hasmaximizebutton和hasminimizebutton為 true:c#

internal testdialogwindow()  

在opendialog.showmessagebox方法中,將現有**替換為以下︰c#

testdialogwindow testdialog = new testdialogwindow();  

testdialog.showmodal();

生成並執行應用程式。 visual studio 的實驗例項應顯示。 在工具的實驗例項中的選單上,您應該看到名為命令呼叫 opendialog。 當您單擊此命令時,你應看到對話方塊視窗。 您應能夠降至最低並最大化視窗。

對於此過程中,您可以使用opendialogtest在具有相同的程式集引用的上乙個過程中建立的解決方案。

將以下**新增using宣告︰c#

using system.windows;  

using microsoft.internal.visualstudio.platformui;

建立乙個名為類testdialogwindow2派生的視窗:c#

class testdialogwindow2 : window  

新增對私有引用ivsuishell:

private ivsuishell shell;
新增將該引用設定為乙個建構函式ivsuishell:c#

public testdialogwindow2(ivsuishell uishell)  

在opendialog.showmessagebox方法中,將現有**替換為以下︰c#

ivsuishell uishell = (ivsuishell)serviceprovider.getservice(typeof(svsuishell));  

testdialogwindow2 testdialog2 = new testdialogwindow2(uishell);

//get the owner of this dialog

intptr hwnd;

uishell.getdialogownerhwnd(out hwnd);

testdialog2.windowstartuplocation = system.windows.windowstartuplocation.centerowner;

uishell.enablemodeless(0);

try

finally

生成並執行應用程式。 在工具選單上,您應該看到名為命令呼叫 opendialog。 當您單擊此命令時,你應看到對話方塊視窗。

建立有模式對話方塊

使用有模式對話方塊時在對話方塊彈出後呼叫函式不會立即返回,而是等到對話方塊銷毀後才會返回 請注意在對話方塊彈出後其他視窗的訊息依然會被傳遞 所以在使用對話方塊時其他視窗都不能接收使用者輸入。建立有模式對話方塊的方法是呼叫cdialog domodal 下面的 演示了這種用法 cyourview on...

MFC 建立模式對話方塊與非模式對話方塊

模式對話方塊 my about cb cb.domodal 非模式對話方塊 my about cb new my about cb create idd myabout,this cb showwindow sw show vc2008 mfc 預設新增到對話方塊會有 確定跟取消,但這倆框在資源檢視...

模式對話方塊與無模式對話方塊

顯示子窗體 1 showdialog 無模式對話方塊 babywindow.showdialog 顯示子窗體 showdialog 無模式對話方塊 無模式 窗體讓您在此窗體與另一窗體之間變換焦點,而不必關閉初始窗體。使用者在該窗體顯示的同時可繼續在任何應用程式的其他位置工作。例如 文字編輯軟體裡面的...