| MauroGv wrote: |
|
Hello Rodolfo,
but still remains the problem that button to click to do the operation shows Open and not Delete.
Thank,
Mauro
| |
You can override the WndProc method and use SetWindowText API to do the trick, take the following step: 1). Find the handle of the OpenFileDialog via m.LParam
2). Find the handle of the Open button via FindWindowEx function
3). Change the text of Open button via SetWindowText function.
I wirte the following sample for your information, put attention to the code in bold, hope it helps you and others.
Code Snippet
partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
static extern bool SetWindowText(IntPtr hWnd, string lpString);
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog delete = new OpenFileDialog();
delete.Title = "Delete File";
if (delete.ShowDialog() == DialogResult.OK)
{
if (MessageBox.Show("do you want to delete this file?",
"comfirm delete", MessageBoxButtons.YesNoCancel)
== DialogResult.Yes)
{
File.Delete(delete.FileName);
}
}
}
protected override void WndProc(ref Message m)
{
// Leting the Form take care off all messages.
base.WndProc(ref m);
if (m.Msg == 289) // Notify of message loop
{
IntPtr dialogHandle = (IntPtr)m.LParam; // Handle of the file dialog
// Getting the handle of the open button in the OpenFileDialog dialog.
IntPtr openButtonHandle = FindWindowEx(dialogHandle, IntPtr.Zero,
"Button", "&Open");
SetWindowText(openButtonHandle, "Delete");
}
}
}
For VB.NET code, I translate it as following and have a test, no problem.
Code Snippet
System.Runtime.InteropServices
System.IO
Class Form3
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter _
As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function SetWindowText(ByVal hWnd As IntPtr, ByVal lpString As String) _
As Boolean
End Function
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim delete As New OpenFileDialog()
delete.Title = "Delete File"
If delete.ShowDialog() = Windows.Forms.DialogResult.OK Then
If MessageBox.Show("do you want to delete this file?", _
"comfirm delete", MessageBoxButtons.YesNoCancel) _
= Windows.Forms.DialogResult.Yes Then
File.Delete(delete.FileName)
End If
End If
End Sub
Protected Overloads Overrides Sub WndProc(ByRef m As Message)
' Leting the Form take care off all messages.
MyBase.WndProc(m)
If m.Msg = 289 Then
' Notify of message loop
Dim dialogHandle As IntPtr = DirectCast(m.LParam, IntPtr)
' Handle of the file dialog
' Getting the handle of the open button in the OpenFileDialog dialog.
Dim openButtonHandle As IntPtr = _
FindWindowEx(dialogHandle, IntPtr.Zero, "Button", "&Open")
SetWindowText(openButtonHandle, "Delete")
End If
End Sub
Class
|