2

SHBrowseForFolderAPI を p/invoke して、ユーザーにフォルダーを選択するように求めようとしていますが、タイトルの静的/ラベル フィールドがツリー コントロールの上に浮かんでいるのが見えます。

レイアウトが悪いフォルダ参照

コードは、ネット上の複数の場所で参照されている記事からダウンロードされ、C# から VB.NET に変換されました (既存のアプリにドロップされていました)。

これを正しくレイアウトするためのヒントがあれば、教えてください。

呼び出しコードは単純です。

  Using getLocDialog As New FolderBrowserDialog()

     getLocDialog.Description = "This is a test"
     getLocDialog.ShowDialog()

  End Using

次に、コードの VB.NET バージョンを次に示します。

Public Class FolderBrowserDialog
   Inherits CommonDialog

   '---------------------------------------------------------------------------
   ' instance
   '---------------------------------------------------------------------------

   Private info As BrowseInfo
   Private folder As String = String.Empty

   '---------------------------------------------------------------------------
   ' ctor
   '---------------------------------------------------------------------------

   Public Sub New()
      info = New BrowseInfo()
      info.Title = String.Empty
      InitCommonControls()
   End Sub

   '---------------------------------------------------------------------------
   ' public
   '---------------------------------------------------------------------------

   Public Shadows Function ShowDialog() As DialogResult

      Dim pitemidlist As IntPtr

      Try
         pitemidlist = SHBrowseForFolder(info.ToByteArray())
      Catch mme As MissingMethodException
         Throw New PlatformNotSupportedException("Your platform doesn't support the SHBrowseForFolder API", mme)
      End Try

      If pitemidlist = IntPtr.Zero Then
         Return DialogResult.Cancel
      End If

      '//maxpath unicode chars
      Dim buffer As Byte() = New Byte(519) {}
      Dim success As Boolean = SHGetPathFromIDList(pitemidlist, buffer)
      '//get string from buffer
      If success Then

         folder = System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length)

         Dim nullindex As Integer = folder.IndexOf(Convert.ToChar(0))
         If nullindex <> -1 Then
            folder = folder.Substring(0, nullindex)
         End If
      End If
      LocalFree(pitemidlist)

      Return DialogResult.OK

   End Function

   Public ReadOnly Property SelectedPath() As String
      Get
         Return folder
      End Get
   End Property

   Public Property Description() As String
      Get
         Return info.Title
      End Get
      Set(ByVal value As String)
         info.Title = value
      End Set
   End Property

   '---------------------------------------------------------------------------
   ' private 
   '---------------------------------------------------------------------------

   <DllImport("commctrl", SetLastError:=True)> _
   Private Shared Sub InitCommonControls()
   End Sub

   <DllImport("ceshell", SetLastError:=True)> _
   Private Shared Function SHBrowseForFolder(ByVal lpbi As Byte()) As IntPtr
   End Function

   <DllImport("ceshell", SetLastError:=True)> _
   Private Shared Function SHGetPathFromIDList(ByVal pidl As IntPtr, ByVal pszPath As Byte()) As Boolean
   End Function

   <DllImport("coredll", SetLastError:=True)> _
   Private Shared Function LocalFree(ByVal ptr As IntPtr) As IntPtr
   End Function

   '---------------------------------------------------------------------------
   ' inner classes
   '---------------------------------------------------------------------------


   Private Class BrowseInfo

      Private m_data As Byte()
      Private m_displayname As Byte()
      Private m_title As Byte()
      Private namehandle As GCHandle
      Private titlehandle As GCHandle

      Public Sub New()

         m_data = New Byte(31) {}
         m_displayname = New Byte(511) {}
         m_title = New Byte(127) {}

         namehandle = GCHandle.Alloc(m_displayname, GCHandleType.Pinned)
         titlehandle = GCHandle.Alloc(m_title, GCHandleType.Pinned)

         BitConverter.GetBytes(namehandle.AddrOfPinnedObject().ToInt32 + 4).CopyTo(m_data, 8)
         BitConverter.GetBytes(titlehandle.AddrOfPinnedObject().ToInt32 + 4).CopyTo(m_data, 12)

      End Sub

      Public Function ToByteArray() As Byte()
         Return m_data
      End Function

      Protected Overrides Sub Finalize()
         namehandle.Free()
         titlehandle.Free()
         MyBase.Finalize()
      End Sub

      Public Property Title() As String
         Get
            Dim ttl As String = System.Text.Encoding.Unicode.GetString(m_title, 0, m_title.Length)
            Dim nullindex As Integer = ttl.IndexOf(Convert.ToChar(0))
            If nullindex = -1 Then

               Return ttl
            End If
            Return ttl.Substring(0, ttl.IndexOf(Convert.ToChar(0)))
         End Get
         Set(ByVal value As String)
            Dim titlebytes As Byte() = System.Text.Encoding.Unicode.GetBytes(value & Convert.ToChar(0))
            If titlebytes.Length > m_title.Length Then
               Throw New ArgumentException("Description must be no longer than 64 characters")
            End If
            Try
               Buffer.BlockCopy(titlebytes, 0, m_title, 0, titlebytes.Length)
            Catch ex As Exception
            End Try
         End Set
      End Property

      Public Property FileName() As String
         Get
            Dim fn As String = System.Text.Encoding.Unicode.GetString(m_displayname, 0, m_displayname.Length)
            Dim nullindex As Integer = fn.IndexOf(Convert.ToChar(0))
            If nullindex = -1 Then
               Return fn
            End If
            Return fn.Substring(0, fn.IndexOf(Convert.ToChar(0)))
         End Get
         Set(ByVal value As String)
            Dim filenamebytes As Byte() = System.Text.Encoding.Unicode.GetBytes(value & Convert.ToChar(0))
            If filenamebytes.Length > m_title.Length Then
               Throw New ArgumentException("SelectedFolder must be no longer than 256 characters")
            End If
            Buffer.BlockCopy(filenamebytes, 0, m_displayname, 0, filenamebytes.Length)
         End Set
      End Property

      '/*HWND hwndOwner;
      'LPCITEMIDLIST pidlRoot;
      'LPTSTR pszDisplayName;
      'LPCTSTR lpszTitle;
      'UINT ulFlags;
      'BFFCALLBACK lpfn;
      'LPARAM lParam;
      'int iImage;*/

   End Class

End Class

元の C# は次のとおりです。

public class FolderBrowserDialog : CommonDialog
    {
        private BrowseInfo info;
        private string folder = string.Empty;

        /// <summary>
        /// Initializes a new instance of the FolderBrowserDialog class.
        /// </summary>
        public FolderBrowserDialog()
        {
            info = new BrowseInfo();
            info.Title = string.Empty;
            InitCommonControls();
        }

        /// <summary>
        /// Runs a common dialog box with a default owner.
        /// </summary>
        /// <returns></returns>
        public new DialogResult ShowDialog()
        {
            IntPtr pitemidlist;

            try
            {
                pitemidlist = SHBrowseForFolder(info.ToByteArray());
            }
            catch(MissingMethodException mme)
            {
                throw new PlatformNotSupportedException("Your platform doesn't support the SHBrowseForFolder API",mme);
            }

            if(pitemidlist==IntPtr.Zero)
            {
                return DialogResult.Cancel;
            }

            //maxpath unicode chars
            byte[] buffer = new byte[520];
            bool success = SHGetPathFromIDList(pitemidlist, buffer);
            //get string from buffer
            if(success)
            {
                folder = System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length);

                int nullindex = folder.IndexOf('\0');
                if(nullindex!=-1)
                {
                    folder = folder.Substring(0, nullindex);
                }
            }
            LocalFree(pitemidlist);

            return DialogResult.OK;
        }

        /// <summary>
        /// Gets the path selected by the user.
        /// </summary>
        public string SelectedPath
        {
            get
            {
                return folder;
            }
        }

        /// <summary>
        /// Gets or sets the descriptive text displayed above the tree view control in the dialog box.
        /// </summary>
        public string Description
        {
            get
            {
                return info.Title;
            }
            set
            {
                info.Title = value;
            }
        }


        #region P/Invokes

        [DllImport("commctrl", SetLastError=true)]
        private static extern void InitCommonControls();

        [DllImport("ceshell", SetLastError=true)]
        private static extern IntPtr SHBrowseForFolder(byte[] lpbi);

        [DllImport("ceshell", SetLastError=true)]
        private static extern bool SHGetPathFromIDList(IntPtr pidl, byte[] pszPath); 

        [DllImport("coredll", SetLastError=true)]
        private static extern IntPtr LocalFree(IntPtr ptr);

        #endregion

        #region helper class for BROWSEINFO struct
        private class BrowseInfo
        {
            private byte[] m_data;
            private byte[] m_displayname;
            private byte[] m_title;
            private GCHandle namehandle;
            private GCHandle titlehandle;

            public BrowseInfo()
            {
                m_data = new byte[32];
                m_displayname = new byte[512];
                m_title = new byte[128];

                namehandle = GCHandle.Alloc(m_displayname, GCHandleType.Pinned);
                titlehandle = GCHandle.Alloc(m_title, GCHandleType.Pinned);

                BitConverter.GetBytes((int)namehandle.AddrOfPinnedObject() + 4).CopyTo(m_data, 8);
                BitConverter.GetBytes((int)titlehandle.AddrOfPinnedObject() + 4).CopyTo(m_data, 12);
            }

            public byte[] ToByteArray()
            {
                return m_data;
            }

            ~BrowseInfo()
            {
                namehandle.Free();
                titlehandle.Free();
            }

            public string Title
            {
                get
                {
                    string title = System.Text.Encoding.Unicode.GetString(m_title, 0, m_title.Length);
                    int nullindex = title.IndexOf('\0');
                    if(nullindex==-1)
                    {
                        return title;
                    }
                    return title.Substring(0, title.IndexOf('\0'));
                }
                set
                {
                    byte[] titlebytes = System.Text.Encoding.Unicode.GetBytes(value + '\0');
                    if(titlebytes.Length > m_title.Length)
                    {
                        throw new ArgumentException("Description must be no longer than 64 characters");
                    }
                    try
                    {
                        Buffer.BlockCopy(titlebytes, 0, m_title,0, titlebytes.Length); 
                    }
                    catch
                    {
                    }
                }
            }

            public string FileName
            {
                get
                {
                    string filename = System.Text.Encoding.Unicode.GetString(m_displayname, 0, m_displayname.Length);
                    int nullindex = filename.IndexOf('\0');
                    if(nullindex==-1)
                    {
                        return filename;
                    }
                    return filename.Substring(0, filename.IndexOf('\0'));
                }
                set
                {
                    byte[] filenamebytes = System.Text.Encoding.Unicode.GetBytes(value + '\0');
                    if(filenamebytes.Length > m_title.Length)
                    {
                        throw new ArgumentException("SelectedFolder must be no longer than 256 characters");
                    }
                    Buffer.BlockCopy(filenamebytes, 0, m_displayname,0, filenamebytes.Length); 
                }
            }

                        /*HWND hwndOwner;
                        LPCITEMIDLIST pidlRoot;
                        LPTSTR pszDisplayName;
                        LPCTSTR lpszTitle;
                        UINT ulFlags;
                        BFFCALLBACK lpfn;
                        LPARAM lParam;
                        int iImage;*/

        }
        #endregion
    }

その他の詳細: 開発環境は Visual Studio 2008、Compact Framework 3.5 です。デバイスは Windows CE 4.2 を実行します。

4

1 に答える 1

1

、それとWindows CE 5.0SHBrowseForFolderで同じ経験をしました。FolderBrowserDialog

BIF_STATUSTEXT回避策は、フラグを設定することです。このようにして、レイアウトが移動し、タイトルが TreeView の上に表示されます。

これを行うuint m_flagsには、BrowseInfoクラスにデータ メンバーを追加します。そのコンストラクターに次を追加します。

const uint BIF_STATUSTEXT = 0x00000004;
m_flags |= BIF_STATUSTEXT;

メソッドにToByteArray()以下を追加します。

BitConverter.GetBytes(m_flags).CopyTo(m_data, 16);
于 2013-10-31T15:17:39.203 に答える