Changeset 791 in dev for branches


Ignore:
Timestamp:
Jan 30, 2011, 3:28:04 AM (14 years ago)
Author:
イグトランス (egtra)
Message:

LoadAccelerators代替関数の実装

Location:
branches/egtra/ab5.0/abdev
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/egtra/ab5.0/abdev/abdev-impl/Resource/Load.cpp

    r789 r791  
    11#include "stdafx.h"
     2#include <vector>
     3#include <utility>
     4#include <boost/range.hpp>
     5#include <boost/range/adaptor/transformed.hpp>
     6#include <boost/numeric/conversion/cast.hpp>
    27#include <Resource/Load.h>
     8
     9using boost::numeric_cast;
    310
    411namespace ActiveBasic { namespace Resource {
     
    2229}
    2330
     31void* GetResource(HINSTANCE hinst, USHORT id, LPCTSTR type)
     32{
     33    if (auto hrsrc = ::FindResource(hinst, MAKEINTRESOURCE(id), type))
     34    {
     35        if (auto hSrc = ::LoadResource(hinst, hrsrc))
     36        {
     37            return ::LockResource(hSrc);
     38        }
     39    }
     40    return nullptr;
     41}
     42
     43std::pair<void*, DWORD> GetResourceWithSize(HINSTANCE hinst, USHORT id, LPCTSTR type)
     44{
     45    typedef std::pair<void*, DWORD> result_type;
     46    if (auto hrsrc = ::FindResource(hinst, MAKEINTRESOURCE(id), type))
     47    {
     48        if (auto hSrc = ::LoadResource(hinst, hrsrc))
     49        {
     50            auto p = ::LockResource(hSrc);
     51            auto size = ::SizeofResource(hinst, hrsrc);
     52            if (p != nullptr && size > 0)
     53            {
     54                return result_type(p, size);
     55            }
     56        }
     57    }
     58    return result_type(nullptr, 0);
     59}
     60
    2461HICON LoadIconCursorImpl(HINSTANCE hinst, USHORT id, int cxDesired, int cyDesired, UINT load, bool isIcon)
    2562{
    26     auto hrsrc = FindResource(hinst, MAKEINTRESOURCE(id), isIcon ? RT_GROUP_ICON : RT_GROUP_CURSOR);
    27     auto pResource = LockResource(LoadResource(hinst, hrsrc));
     63    auto pResource = GetResource(hinst, id, isIcon ? RT_GROUP_ICON : RT_GROUP_CURSOR);
    2864
    2965    auto idIcon = LookupIconIdFromDirectoryEx(reinterpret_cast<PBYTE>(pResource), isIcon, cxDesired, cyDesired, load);
    30     auto hrsrcIcon = FindResource(hinst, MAKEINTRESOURCE(idIcon), isIcon ? RT_ICON : RT_CURSOR);
    31     auto pResourceIcon = LockResource(LoadResource(hinst, hrsrcIcon));
     66    auto icon = GetResourceWithSize(hinst, numeric_cast<USHORT>(idIcon), isIcon ? RT_ICON : RT_CURSOR);
    3267
    33     return CreateIconFromResourceEx(reinterpret_cast<PBYTE>(pResourceIcon),
    34         SizeofResource(hinst, hrsrcIcon), isIcon, 0x00030000, cxDesired, cyDesired, load);
     68    return CreateIconFromResourceEx(reinterpret_cast<PBYTE>(icon.first),
     69        icon.second, isIcon, 0x00030000, cxDesired, cyDesired, load);
    3570}
    3671
     
    88123}
    89124
     125// http://msdn.microsoft.com/en-us/library/ms648010.aspx
     126struct ACCELTABLEENTRY
     127{
     128    WORD fFlags;
     129    WORD wAnsi;
     130    WORD wId;
     131    WORD padding;
     132};
     133
     134ACCEL AccelFromEntry(ACCELTABLEENTRY const& e)
     135{
     136    ACCEL t = {e.fFlags & 0x7f, e.wAnsi, e.wId};
     137    return t;
     138}
     139
     140HACCEL LoadAccelerators(HINSTANCE hinst, USHORT id)
     141{
     142    using namespace boost::adaptors;
     143
     144    auto accel = GetResourceWithSize(hinst, id, RT_ACCELERATOR);
     145    if (accel.first == nullptr)
     146    {
     147        return nullptr;
     148    }
     149
     150    auto pate = static_cast<ACCELTABLEENTRY const*>(accel.first);
     151
     152    auto a = boost::copy_range<std::vector<ACCEL>>(
     153        boost::make_iterator_range(pate, pate + accel.second / sizeof (ACCELTABLEENTRY))
     154        | transformed(AccelFromEntry));
     155
     156    if (a.size() > INT_MAX)
     157    {
     158        a.resize(INT_MAX);
     159    }
     160    return CreateAcceleratorTable(&a[0], static_cast<int>(a.size()));
     161}
     162
    90163}}
    91164
  • branches/egtra/ab5.0/abdev/abdev-impl/Resource/Load.h

    r789 r791  
    1414boost::optional<std::wstring> LoadString(HINSTANCE hinst, USHORT id);
    1515
     16HACCEL LoadAccelerators(HINSTANCE hinst, USHORT id);
     17
    1618struct IconDeleter
    1719{
  • branches/egtra/ab5.0/abdev/abdev/abdev.cpp

    r786 r791  
    15521552    //return 0;
    15531553
    1554 
    1555     hAccel=LoadAccelerators(hResInst,MAKEINTRESOURCE(IDR_ACCELERATOR1));
     1554    hAccel = ActiveBasic::Resource::LoadAccelerators(hResInst, IDR_ACCELERATOR1);
    15561555
    15571556    //メインウィンドウ
Note: See TracChangeset for help on using the changeset viewer.