以 Rob Sherrit 在 2014 年 1 月 22 日的回复为灵感,我创建了一个新模块并将其命名为 CKRFileDialog(随便你怎么称呼它),其中包含以下代码:
Public Function Show(fd As Object, CoveredForm As Form, Optional bShowHelp As Boolean = False) As DialogResult
Dim oDR As DialogResult
'The .Net FileDialogs open in the last Form that was created.
'To ensure they appear in the area of the current Form, we create a new HIDDEN PositionForm and then
'delete it afterwards.
Dim PositionForm As New Form With {
.StartPosition = FormStartPosition.Manual,
.Left = CoveredForm.Left + CInt(CoveredForm.Width / 8), 'adjust as required
.Top = CoveredForm.Top + CInt(CoveredForm.Height / 8), 'adjust as required
.Width = 0,
.Height = 0,
.FormBorderStyle = Windows.Forms.FormBorderStyle.None,
.Visible = False
}
PositionForm.Show()
'If you use SkyDrive you need to ensure that "bShowHelp" is set to True in the passed parameters.
'This is a workaround for a problem on W8.1 machines with SkyDrive installed.
'Setting it to "true" causes the "old" W7 control to be used which avoids a pointing to SkyDrive error.
'If you do not use SkyDrive then simply do not pass the last parameter (defaults to "False")
fd.ShowHelp = bShowHelp
'store whether the form calling this routine is set as "topmost"
Dim oldTopMost As Integer = CoveredForm.TopMost
'set the calling form's topmost setting to "False" (else the dialogue will be "buried"
CoveredForm.TopMost = False
oDR = fd.ShowDialog(PositionForm)
'set the "topmost" setting of the calling form back to what it was.
CoveredForm.TopMost = oldTopMost
PositionForm.Close()
PositionForm.Dispose()
Return oDR
End Function
然后我在我的各个模块中调用此代码,如下所示:
如果执行“FileOpen”,请确保将 FileOpenDialog 组件添加到您的表单或代码中,并根据需要调整组件的属性(例如 InitDirectory、Multiselect 等)
在使用 FileSaveDialog 组件时执行相同的操作(可能会应用与 FileOpenDialog 组件不同的属性)。
要“显示”对话框组件,请使用如下代码行,传递两个参数,第一个参数是您正在使用的 FileDialog(“打开”或“保存”),第二个参数是您希望覆盖对话的表单。
CKRFileDialog.Show(saveFileDialog1, CoveredForm) 或 CKRFileDialog.Show(openFileDialog1, CoveredForm)
请记住,如果您使用 SkyDrive,则必须将“True”作为第三个参数传递:
CKRFileDialog.Show(saveFileDialog1, CoveredForm, True) 或 CKRFileDialog.Show(openFileDialog1, CoveredForm, True)
我将对话的“偏移量”设置为“CoveredForm”表格上上下的 1/8,但您可以将其设置回 1/2(如 Rob Sherret 的代码中)或您希望的任何值。
这似乎是最简单的方法
谢谢罗伯!:-)