我認爲這裏是最乾淨的解決方案。我知道你的項目中有很多不方便重新組織的形式(例如讓他們繼承相同的表格類,並且覆蓋該類中的Show
和ShowDialog
)。此解決方案需要您只須要hook
或insert
代碼顯示了形成之前執行的形式Handle
訪問:
public class FormWndProc : NativeWindow {
protected override void WndProc(ref Message m){
if(m.Msg == 0x18&&m.WParam != IntPtr.Zero)//WM_SHOWWINDOW = 0x18
{
//your code here
//you can use m.HWnd to get the Handle of the form
}
base.WndProc(ref m);
}
}
//use the class
//suppose you want to execute the code before showing the form1, form2, form3
new FormWndProc().AssignHandle(form1.Handle);
new FormWndProc().AssignHandle(form2.Handle);
new FormWndProc().AssignHandle(form3.Handle);
//you can save the instance of FormWndProc to call the ReleaseHandle() method later if you don't want to insert code before showing the form.
如果您可以訪問您的形式,我的意思是你可以直接在覆蓋方法WndProc
,你可以這樣做:
public class Form1 : Form {
///....
protected override void WndProc(ref Message m){
if(m.Msg == 0x18&&m.WParam != IntPtr.Zero)//WM_SHOWWINDOW = 0x18
{
//your code here
//you can use m.HWnd to get the Handle of the form
}
base.WndProc(ref m);
}
}
//This way you don't need the class FormWndProc as defined above.
你想超載你的'ShowDialog'和'Show'? –
'Show'之前我想在第二個線程中顯示加載屏幕 – BramVdeventer
好的,我明白你想要什麼,請看我的答案。 –