-
1 Attachment(s)
[RESOLVED] Disable Textbox in General Tab of File Property Dialog ?
Hi guys,
this one is clearly over my head. Does anybody know how I could disable Textbox in file Property Dialog box (where you can change file/folder name) ??
I need this in my app (my Listview File Manager) because particular folders shouldn't be renamed at any cost - but I still want to keep Dialog box.
Image of this Dialox box is shown below (Textbox in question is coloured red).
ANY HELP MUCH APPRECIATED !! - other suggestions welcomed too...
Attachment 144525
-
Re: Disable Textbox in General Tab of File Property Dialog ?
I think this will require getting the handle of the specific window (i.e. the text box in the properties form) then use the EnableWindow api function.
https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
http://www.pinvoke.net/default.aspx/user32.enablewindow
I suspect getting the handle is going to be the difficult part.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Well I was hoping that I wouldn't get an answer like this, but I suspected It. Do you have any simmilar example of code that does something like this ? What about FileStream.Lock (to lock only currently file to read only)?
-
Re: Disable Textbox in General Tab of File Property Dialog ?
I don't. I've never needed to disable controls on another app. As far as I know there is no way to make a file or folder that cannot be removed if someone is persistent. You could bury your folder in some obscure directory, but no matter what you do, someone could remove them if they wanted to.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Originally Posted by
LuckyLuke82
Well I was hoping that I wouldn't get an answer like this, but I suspected It. Do you have any simmilar example of code that does something like this ? What about FileStream.Lock (to lock only currently file to read only)?
Why not make your own properties dialog in your app?
Just about everything you'd really need to show the user is available in the FileInfo object and getting the other info that's on the "details" tab probably would be far easier to figure out than simply how to disable the textbox on the existing windows dialog.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Originally Posted by
kebo
As far as I know there is no way to make a file or folder that cannot be removed if someone is persistent.
I know that and I'm not trying to do that actually. It's a security precaution in my app which uses specifically construsted directories and creates some folders with names from DB values - that enables my app to link filesystem directories with certain data about it from DB. So when user takes a look at property of those folders, I would just like to disable renaming in that window.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Originally Posted by
JuggaloBrotha
Why not make your own properties dialog in your app?
Indeed. I actually have done that allready, but changed It to this dialog, mostly because of visual taste. But my app actually communicates with Windows all the way so my goal is to make same looks as possible. Beside that, FileInfo object lacks of some stuff - e.g. I couldn't figure out how to show multiple selected files/folder property.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Originally Posted by
LuckyLuke82
Indeed. I actually have done that allready, but changed It to this dialog, mostly because of visual taste. But my app actually communicates with Windows all the way so my goal is to make same looks as possible. Beside that, FileInfo object lacks of some stuff - e.g. I couldn't figure out how to show multiple selected files/folder property.
To get the summed info on multiple files just do what Windows does, loop the selection creating a FileInfo and add up the info and then display the file count and the summed info.
For a folder, you'll want to use recursion and as you change folders just keep a running folder count along with the file count.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Thanks for sharing that, but I'll rather stick with dialog box. Can somebody rather tell me how I could get Dialog box name ? I don't have Spy++, I'm using Visual Studio Express edition.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Because you know the title of the properties window, you can find the window using the FindWindow API call
http://pinvoke.net/default.aspx/user32/FindWindow.html
Code:
Dim fileName as string = String.format("{0} Properties","myFile.ext" )
dim hndl as IntPtr = FindWindow(Nothing, fileName)
Once you have that, you need to get the handle of the textbox in the properties window. That I'm afraid, I don't know how to do. Maybe this will help you...
http://stackoverflow.com/questions/1...sual-basic-net
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Can somebody rather tell me how I could get Dialog box name ?
OK I figured that one out, you can do It with FindWindow API. Now I'll try to write API for this :)
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Because you know the title of the properties window, you can find the window using the FindWindow API call
Lol I didn't notice your reply, I allready did It myself. Thanks man !!
-
Re: Disable Textbox in General Tab of File Property Dialog ?
So far I tried this:
Code:
Public Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Declare Function EnableWindow Lib "user32" Alias "EnableWindow" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
Sub Test()
Dim Hwnd As Integer = Module1.FindWindow(Nothing, "New document Properties")
If Not (Hwnd = 0) Then
EnableWindow(Hwnd, 0)
End If
End Sub
It locks whole File Property window, but I get a very long error with It: "A call to PInvoke function '[…]' has unbalanced the stack.This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."
Looking at error I tried with this too:
Code:
<DllImport("user32.dll", CallingConvention = CallingConvention.Cdecl, CharSet:=CharSet.Auto, SetLastError:=True)>
But then this produces me error in Visual Studio. Any suggestions guys ?
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Your EnableWindow declaration signature is for VB6. Change the Long's to Int32.
The handle that you are passing to the EnableWindow API needs to be the specific handler of the textbox you are trying to disable (which changes each time the window opens). By passing the handle of the properties window, you are trying to disable the entire window. Take a look at the link in post 10, it may help
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Your EnableWindow declaration signature is for VB6. Change the Long's to Int32.
Thanks kebo, I'll try that tommorow, It's bed time now. I know that this is far from desired textbox, but pieces by pieces will get me to It. Im not quite good with API. For post #10 - your link to stack doesn't work for me.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
The forum doesn't like me to embed links, so I have a bit of trouble with them. Try this
http://stackoverflow.com/questions/1...sual-basic-net
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Eureka, got It working :)
Here is my solution (a little bit modified code from post#10):
Code:
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpWindowText As String, ByVal cch As Integer) As Integer
Public Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Declare Function EnableWindow Lib "user32" Alias "EnableWindow" (ByVal hwnd As Int32, ByVal fEnable As Int32) As Long
Public Declare Function EnumChildWindows Lib "user32" (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, ByVal lParam As IntPtr) As Boolean
Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
Public Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
Dim ChildrenList As New List(Of IntPtr)
Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
Try
EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
Finally
If ListHandle.IsAllocated Then ListHandle.Free()
End Try
Return ChildrenList.ToArray
End Function
Public Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
ChildrenList.Add(Handle)
Return True
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Hwnd As Integer = FindWindow(Nothing, Listview1.SelectedItems(0).Text & " Properties")
Dim text As String = Space(Int16.MaxValue)
If Not (Hwnd = 0) Then
Dim hndls() As IntPtr = GetChildWindows(Hwnd)
For Each hnd In hndls
Dim window = GetWindowText(hnd, text, Int16.MaxValue)
'This is the textbox in File Property dialog that I wanted
If window = 3 Then
EnableWindow(hnd, 0)
End If
Next
End If
End Sub
My only concern left is that how I find the window "FindWindow(Nothing, Listview1.SelectedItems(0).Text & " Properties")". What about If user has Windows settings something diffrent than English ? Is there any better way to determine window, like I used GetWindowText function which returned me only integer which never changes ? ...I'm asking this because FindWindow returns Integers, but they constantly change, so I reckon that you cannot determine window by that. I would also be glad If someone would show me a shortcut for this, I think API code is pretty long.
Thanks for help kebo, that post really helped me a lot :)
-
Re: Disable Textbox in General Tab of File Property Dialog ?
You might try enumerating the running processes using one of the overloads of GetProcesses method in the Process class...
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
Problem is that if there are multiple property windows open, you need to disgusting which one you want either by title handle, or some other means.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Problem is that if there are multiple property windows open, you need to disgusting which one you want either by title handle, or some other means.
Thanks, but first I'm going to take a look at different approaches in API - There must be another way to get Parent window same as you get child windows. I've seen some different varieties.
Another thing worth mentioning is this - I noticed that API I posted doesn't work in same Sub - to me It looks like Windows doesn't allow to lock anything or doesn't fully paint those child controls after certain time. What happens is that nothing gets locked - but If you try code like in my answer (Button__Click) everything is fine. I solved It by Timer though - after 100 interval Textbox gets locked. What am I missing here ?
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Well, testing a bit more I figured that Textbox doesn't get locked allways - Actually It locks only If folder is named like Integer (e.g. "1.0"). If folder is named like a String (e.g. "New Folder") then code doesn't do anything. What could be wrong - I don't receive no errors, code even gets to end with no problem. Is a string in Textbox problem ?
-
Re: Disable Textbox in General Tab of File Property Dialog ?
It sure looks to me like you did that explicitly. GetWindowText returns the length of the string copied. You are checking whether the length of the string is 3 characters, so "1.0" is going to work, as would "cat", but "New Folder" has a length of 10, so it won't work.
What's the point of checking the length? Especially, what's the significance of checking whether the length is 3?
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
What's the point of checking the length? Especially, what's the significance of checking whether the length is 3?
Hi Shaggy. Actually that's the only part I don't quite understand in my API- I thought that GetWindowText gets the window that I need to lock, not getting text.length. How can I change that to check If window has same string as I need (In this case folders name in Listview)?
-
Re: Disable Textbox in General Tab of File Property Dialog ?
I've never used that method, but a quick look at the documentation suggests that the string you pass as the second argument (you call it text) will contain the contents of the textbox.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Solved. I had to delete GetWindowText function and use WM_GETTEXT instead :)
Code:
Public Const WM_GETTEXT As Integer = &HD
Public Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Public Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Declare Function EnableWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal fEnable As Boolean) As Boolean
Public Declare Function EnumChildWindows Lib "user32" (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, ByVal lParam As IntPtr) As Boolean
Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
Public Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
Dim ChildrenList As New List(Of IntPtr)
Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
Try
EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
Finally
If ListHandle.IsAllocated Then ListHandle.Free()
End Try
Return ChildrenList.ToArray
End Function
Public Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
ChildrenList.Add(Handle)
Return True
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Hwnd As Integer = FindWindow(Nothing, Listview1.SelectedItems(0).Text & " Properties")
If Not (Hwnd = 0) Then
Dim hndls() As IntPtr = GetChildWindows(Hwnd)
For Each hnd In hndls
'Alloc memory for the buffer that recieves the text
Dim Handle As IntPtr = Marshal.AllocHGlobal(100)
'send WM_GWTTEXT message to the notepad window
Dim NumText As Integer = SendMessage(hnd, WM_GETTEXT, 50, Handle)
'copy the characters from the unmanaged memory to a managed string
Dim DesiredText As String = Marshal.PtrToStringUni(Handle)
'If text from child window matches selected item text, then disable this window
If DesiredText = Listview1.SelectedItems(0).Text Then
EnableWindow(hnd, False)
Timer1.Stop()
Timer1.Enabled = False
Exit Sub
End If
Next
End If
End Sub
However, why code doesn't get done from same Sub is still a mystery for me. But It works fine with Timer, so I'll close this thread.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Originally Posted by
Shaggy Hiker
I've never used that method, but a quick look at the documentation suggests that the string you pass as the second argument (you call it text) will contain the contents of the textbox.
Yes, but I need to compare that Textbox string (from post #1) to my ListviewItem.Text, here is the problem I had. I'm selecting ListviewItem which represents folder and then show It's property. If folder has certain name then I should disable Textbox in File Property Dialog, that was the whole point. I'm not sure If I could get that with GetWindowText function, so I changed It.
-
Re: [RESOLVED] Disable Textbox in General Tab of File Property Dialog ?
This is from the documentation for GetWindowText:
Quote:
If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control.
So, you solved the problem by doing what GetWindowText was doing anyways. However, it works, and that's the bottom line.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Originally Posted by
LuckyLuke82
Solved. I had to delete GetWindowText function and use WM_GETTEXT instead :)
However, why code doesn't get done from same Sub is still a mystery for me. But It works fine with Timer, so I'll close this thread.
GetWindowText is really for getting a window's title bar text, not control text.
I don't understand what you mean by needing a timer, Timer for what?
I used SPY++ to look at the properties window and that textbox you want is the first "Edit" class window on the general tab in Win7 and Win10, so being on English Windows I tried like this and works as expected.
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function EnableWindow(hWnd As IntPtr, bEnable As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ListView1.SelectedItems.Count = 0 Then
MessageBox.Show("Please select an item!", "Title", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
Dim hWnd As IntPtr = FindWindow("#32770", ListView1.SelectedItems(0).Text & " Properties")
If Not hWnd.Equals(IntPtr.Zero) Then
Dim genTabContainer = FindWindowEx(hWnd, IntPtr.Zero, "#32770", "General")
If Not genTabContainer.Equals(IntPtr.Zero) Then
Dim firstEditBox = FindWindowEx(genTabContainer, IntPtr.Zero, "Edit", Nothing)
If Not firstEditBox.Equals(IntPtr.Zero) Then
EnableWindow(firstEditBox, False)
End If
End If
End If
End Sub
End Class
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Thanks for sharing EdgeMeal. Your code beats mine when It comes to length :D:D
Quote:
used SPY++ to look at the properties window
Unfortunally I have Visual Studio Community (free edition) and SPY++ isn't available in It.
Quote:
textbox you want is the first "Edit" class window on the general tab in Win7 and Win10, so being on English Windows I tried like this and works as expected.
As If you knew - I actually need this code for 2 Languages so my question to you or anybody that has SPY++ - Did you get "#32770" (I reckon that is Window ID ?) in SPY++ and does It get changed in other OS Language ?!?
Quote:
I don't understand what you mean by needing a timer, Timer for what?
I'm glad somebody noticed what I wrote. Mine (neither yours) code doesn't work from same Sub where I open Property Dialog Box. So this isn't working (full code, If you're willing to test It just copy):
Code:
Public Structure SHELLEXECUTEINFO
Public cbSize As Integer
Public fMask As UInteger
Public hwnd As IntPtr
<MarshalAs(UnmanagedType.LPTStr)>
Public lpVerb As String
<MarshalAs(UnmanagedType.LPTStr)>
Public lpFile As String
<MarshalAs(UnmanagedType.LPTStr)>
Public lpParameters As String
<MarshalAs(UnmanagedType.LPTStr)>
Public lpDirectory As String
Public nShow As Integer
Public hInstApp As IntPtr
Public lpIDList As IntPtr
<MarshalAs(UnmanagedType.LPTStr)>
Public lpClass As String
Public hkeyClass As IntPtr
Public dwHotKey As UInteger
Public hIcon As IntPtr
Public hProcess As IntPtr
End Structure
Public Function ShowProperties(Filename As String) As Boolean
Dim info As New SHELLEXECUTEINFO()
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info)
info.lpVerb = "properties"
info.lpFile = Filename
info.nShow = SW_SHOW
info.fMask = SEE_MASK_INVOKEIDLIST
Return ShellExecuteEx(info)
End Function
Private Sub PropertiesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PropertiesToolStripMenuItem.Click
'Show Properties window
ShowProperties(Listview1.SelectedItems(0).Tag.ToString)
'Here I'm determining for which folders Textbox has to be locked
If Listview1.SelectedItems(0).Text="Test" And Listview1.SelectedItems(0).ImageKey="folder" then
Dim genTabContainer = FindWindowEx(hWnd, IntPtr.Zero, "#32770", "General")
If Not genTabContainer.Equals(IntPtr.Zero) Then
Dim firstEditBox = FindWindowEx(genTabContainer, IntPtr.Zero, "Edit", Nothing)
If Not firstEditBox.Equals(IntPtr.Zero) Then
EnableWindow(firstEditBox, False)
End If
End If
End If
End Sub
I think (but am not sure) that this happens because Property Dialog Box needs some time to fully paint all windows or doesn't allow to lock anything after certain time. Or maybe something to do with a ShowProperties function. Calling code from Timer solved this issue. If you know answer to this too, please let me know.
-
Re: Disable Textbox in General Tab of File Property Dialog ?
Quote:
Originally Posted by
LuckyLuke82
I think (but am not sure) that this happens because Property Dialog Box needs some time to fully paint all windows or doesn't allow to lock anything after certain time. Or maybe something to do with a ShowProperties function. Calling code from Timer solved this issue. If you know answer to this too, please let me know.
"#32770" is classname for dialogs, "Edit" is classname for edit controls, there are a few others, AFAIK those Windows classnames don't change with language. Lots of info about classes here, About Window Classes.
Ya, you may need to give Windows a little time to open and draw the properties window, other then that I don't have any experience with that API and it has a ton of flags/options. Best of luck!
-
Re: [RESOLVED] Disable Textbox in General Tab of File Property Dialog ?
Thanks for sharing link, very useful to know.
Quote:
Ya, you may need to give Windows a little time to open and draw the properties window
I tested with timer too, strange thing that code executes even if only interval of 1 is set. So drawing window probably isn't the cause, but something else. Anyway, thanks for help !!
-
Re: [RESOLVED] Disable Textbox in General Tab of File Property Dialog ?
Just out of curiosity I tested fully - looks like Dialog Box Window draws after 59-80ms, so you need to delay code for that time. I removed Timer and solved issues:
Doing
Quote:
Threading.Thread.Sleep(100)
before code for locking window works fine now.