- 732
- 1,094
Описание: открывает диалог выбора папки
Код:
Пример использования:
Код:
Lua:
local ffi = require('ffi');
local bit = require('bit');
ffi.cdef[[
typedef void* HANDLE;
typedef HANDLE HWND;
typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned int UINT;
typedef char* LPSTR;
typedef const char* LPCSTR;
typedef wchar_t* LPWSTR;
typedef const wchar_t* LPCWSTR;
typedef void* LPVOID;
typedef long LPARAM;
typedef struct _BROWSEINFOW {
HWND hwndOwner;
LPCWSTR pidlRoot;
LPWSTR pszDisplayName;
LPCWSTR lpszTitle;
UINT ulFlags;
int (__stdcall *lpfn)(HWND, UINT, LPARAM, LPARAM);
LPARAM lParam;
int iImage;
} BROWSEINFOW;
int SHGetPathFromIDListW(void *pidl, LPWSTR pszPath);
void CoTaskMemFree(void *pv);
void *SHBrowseForFolderW(BROWSEINFOW *lpbi);
int MultiByteToWideChar(unsigned int CodePage, DWORD dwFlags, const char* lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar);
int WideCharToMultiByte(unsigned int CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, BOOL* lpUsedDefaultChar);
]]
local shell32 = ffi.load("shell32");
local ole32 = ffi.load("ole32");
function cp1251_to_utf16(str)
local len = ffi.C.MultiByteToWideChar(1251, 0, str, -1, nil, 0)
local buf = ffi.new("wchar_t[?]", len)
ffi.C.MultiByteToWideChar(1251, 0, str, -1, buf, len)
return buf
end
function utf16_to_cp1251(wstr)
local len = ffi.C.WideCharToMultiByte(1251, 0, wstr, -1, nil, 0, nil, nil)
local buf = ffi.new("char[?]", len)
ffi.C.WideCharToMultiByte(1251, 0, wstr, -1, buf, len, nil, nil)
return ffi.string(buf)
end
function selectFolder(title)
title = title or ""
local bi = ffi.new("BROWSEINFOW")
local hwnd = ffi.new("HWND",nil);
bi.hwndOwner = hwnd
bi.pidlRoot = nil
bi.pszDisplayName = ffi.new("wchar_t[?]", 260)
bi.lpszTitle = cp1251_to_utf16(title)
bi.ulFlags = bit.bor(0x00000001, 0x00000040)
bi.lpfn = nil
bi.lParam = 0
bi.iImage = 0
local pidl = shell32.SHBrowseForFolderW(bi)
if pidl ~= nil then
local path = ffi.new("wchar_t[?]", 260)
if shell32.SHGetPathFromIDListW(pidl, path) ~= 0 then
ole32.CoTaskMemFree(pidl)
return utf16_to_cp1251(path)
end
ole32.CoTaskMemFree(pidl)
end
return nil
end
Пример использования:
Lua:
local path = selectFolder()
if path then
sampAddChatMessage("Select folder: "..path, -1)
end