ab.com コミュニティ https://www.activebasic.com/forum/ |
|
String型を返すWindows APIのラッパ https://www.activebasic.com/forum/viewtopic.php?t=1148 |
ページ 1 / 1 |
作成者: | イグトランス [ 2006年6月13日(火) 15:26 ] |
記事の件名: | String型を返すWindows APIのラッパ |
GetWindowTextStrやGetDlgItemStrの要領で文字列を結果として返すラッパを投稿していきます。 コード: TypeDef GetStr_GetLenProc = *Function(pv As VoidPtr) As DWord TypeDef GetStr_GetStrProc = *Function(pv As VoidPtr, buf As *Byte, len As DWord) As DWord Function GetString(pv As VoidPtr, pfnGetLen As GetStr_GetLenProc, pfnGetStr As GetStr_GetStrProc) As String Dim len As DWord len = pfnGetLen(pv) If len = 0 Then Exit Function GetString = ZeroString(len) len = pfnGetStr(pv, StrPtr(GetString), len) #ifdef _AB_VER5 GetString.Length = len #else SetDWord(StrPtr(GetString) - SizeOf (DWord), len) #endif End Function本来ならクラスを使ってやりたいところですが,抽象メンバ関数が無いので,通常の関数と関数ポインタを使っていますが, それさえ目をつぶればテンプレートメソッドパターンになっているはずです。 とにかくこれ単独で使うのではなく,実際のラッパは次以降です。 2006/09/29 AB5に対応させました。 |
作成者: | イグトランス [ 2006年6月13日(火) 15:32 ] |
記事の件名: | |
というわけで,まずはDragQueryFileのString版, DragQueryFileStrです。 [ここをクリックすると内容が表示されます]
ようするにGetStr_GetLenProcは文字列の長さを返す関数で,コード: Class DragQueryInfo Public Sub DragQueryInfo(hDrop As HDROP, iFile As Long) This.hDrop = hDrop This.iFile = iFile End Sub hDrop As HDROP iFile As Long End Class Function DragQueryFile_ForGetLen(ByRef dqi As DragQueryInfo) As DWord With dqi DragQueryFile_ForGetLen = DragQueryFile(.hDrop, .iFile, 0, 0) End With End Function Function DragQueryFile_ForGetStr(ByRef dqi As DragQueryInfo, buf As *Byte, len As DWord) As DWord With dqi DragQueryFile_ForGetStr = DragQueryFile(.hDrop, .iFile, buf, len + 1) End With End Function Function DragQueryFileStr(hDrop As HDROP, iFile As Long) As String If iFile >= 0 Then Dim dqi As DragQueryInfo(hDrop, iFile) DragQueryFileStr = GetString(VarPtr(dqi), AddressOf (DragQueryFile_ForGetLen), AddressOf (DragQueryFile_ForGetStr)) End If End Function GetStr_GetStrProcは実際に文字列を得てさらに新たな長さを返すような関数であればよいのです。 そうすればGetStringがZeroStringを呼んだり文字列の長さを調整したりなどといった雑用をこなしてくれます。 |
作成者: | イグトランス [ 2006年6月13日(火) 17:06 ] |
記事の件名: | IDListとSHBrowseForFolder |
次はIDListからパス文字列へ変換するSHGetPathFromIDListのラッパ、IDListToPathStrと それを使ってフォルダ選択ダイアログの結果をStringで返すBrowseForFolder_Strです。 [ここをクリックすると内容が表示されます]
なんの面白みも無いですが,使用例です。
コード: '------------------------------------------------------------------------------- ' IDListToPathStr, BrowseForFolder_Str TypeDef UINT = DWord Type SHITEMID cb As WORD abID[ELM(1)] As Byte End Type Type ITEMIDLIST mkid As SHITEMID End Type TypeDef PITEMIDLIST = *ITEMIDLIST Function GetMaxPath(pv As VoidPtr) As DWord GetMaxPath = MAX_PATH End Function Function GetPathFromIDList(pv As VoidPtr, buf As *Byte, len As DWord) As DWord If len < MAX_PATH Then buf[0] = 0 Exit Function End If SHGetPathFromIDList(pidlBrowse, buf) GetPathFromIDList = lstrlen(buf) End Function Function IDListToPathStr(pidl As *ITEMIDLIST) As String IDListToPathStr = GetString( VarPtr(dqi), AddressOf (GetMaxPath) As GetStr_GetLenProc, AddressOf (GetPathFromIDList) As GetStr_GetStrProc) End Function Function BrowseForFolder_Str(hwnd As HWND, pszTitle As BytePtr, flags As UINT) As String Dim bi As BROWSEINFO 'BROWSEINFO構造体の初期化 'ZeroMemory(VarPtr(bi), Len(bi)) With bi .hwndOwner = hwnd .lpszTitle = pszTitle .ulFlags = flags End With '「フォルダの参照」ダイアログボックスを表示 Dim pidlBrowse As *ITEMIDLIST pidlBrowse = SHBrowseForFolder(bi) If pidlBrowse Then BrowseForFolder_Str = IDListToPathStr(pidlBrowse) CoTaskMemFree(pidlBrowse) End If End Function コード: Dim folderPath As String folderPath = BrowseForFolder_Str(hMainWnd, "フォルダの選択", BIF_RETURNONLYFSDIRS) MessageBox(hMainWnd, StrPtr(folderPath), "選択したフォルダのパス", MB_OK)BrowseForFolder_Strは最初の引数がウィンドウハンドル,次にダイアログのタイトル,最後にフラグ指定の引数としました。 |
作成者: | イグトランス [ 2006年9月29日(金) 09:55 ] |
記事の件名: | |
GetWindowTextStrの焼き直しです。GetWindowTextLengthはちょうど引数が一致するので直接渡しています。 GetWindowTextは微妙に引数が異なるので,そうは行きませんでした。 コード: Function GetWindowTextStr(hwnd As HWND) As String GetWindowTextStr = GetString(hwnd, GetProcAddress(GetModuleHandle("user32"), "GetWindowTextLengthA"), AddressOf(GetWindowTextBufLen)) End Function Function GetDlgItemTextStr(hDlg As HWND, idDlgItem As Long) As String GetDlgItemTextStr = GetWindowTextStr(GetDlgItem(hDlg, idDlgItem)) End Function Function GetWindowTextBufLen(hwnd As HWND, p As *Byte, len As Long) As Long GetWindowTextBufLen = GetWindowText(hwnd, p, len + 1) End Function平成18年11月04日修正しました。 |
ページ 1 / 1 | 全ての表示時間は UTC+09:00 です |
Powered by phpBB® Forum Software © phpBB Limited https://www.phpbb.com/ |