外字の登録と解除 (CEudcHelper, CEudcHolder) [ATL]

(2007.05.16)
#pragma once
 
// WAC.com Class Liblary for Visual C++
// Copyright (C) WAC.com Inc. All rights reserved.
//
// This file is a part of the WAC.com Class Liblary.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
 
// '06.08.14 : UNICODEビルド対応 (sha)
// '07.04.11 : 全面的に改定 (sha)
 
#include <atlbase.h>                            // CRegKey
 
#include "Module.h"
 
 
/////////////////////////////////////////////////////////////////////////////
// CEudcHelper
//
// SDK32:外字ファイルを更新する方法
// http://support.microsoft.com/kb/413278/ja
 
class CEudcHelper
{
protected:
    typedef CEudcHelper                            thisClass;
 
protected:
    CEudcHelper();                                // 実装しない。。
    virtual ~CEudcHelper();
 
public:
    static BOOL Probe_EnableEUDC(BOOL fEnableEUDC)
    {
        CModule module;
 
        HINSTANCE hInst = module.LoadLibrary(_T("gdi32.dll"));
        if (hInst == NULL)
        {
            return FALSE;
        }
 
        typedef BOOL WINAPI FUNC_GDI32_ENABLEEUDC(BOOL fEnableEUDC);
        FUNC_GDI32_ENABLEEUDC* func = (FUNC_GDI32_ENABLEEUDC*)module.GetProcAddress(_T("EnableEUDC"));
        if (func == NULL)
        {
            return FALSE;
        }
 
        return (*func)(fEnableEUDC);
    }
 
public:
    static HRESULT RegsterEUDC(LPCTSTR szFontFace, LPCTSTR szFileName, int nCodePage = 932)
    {
        // 一時的に、無効化&有効化
        CApplyEUDC apply;
 
        CRegKey regKey;
 
        CString strRegSubKey = thisClass::GetRegSubKey(nCodePage);
        LONG lResult = regKey.Create(HKEY_CURRENT_USER, strRegSubKey);
        if (lResult != ERROR_SUCCESS)
        {
            DWORD dwError = ::GetLastError();
            return HRESULT_FROM_WIN32(dwError);
        }
 
        lResult = regKey.SetStringValue(szFontFace, szFileName);
        if (lResult != ERROR_SUCCESS)
        {
            DWORD dwError = ::GetLastError();
            return HRESULT_FROM_WIN32(dwError);
        }
 
        return S_OK;
    }
    static HRESULT UnregsterEUDC(LPCTSTR szFontFace, int nCodePage = 932)
    {
        // 一時的に、無効化&有効化
        CApplyEUDC apply;
 
        CRegKey regKey;
 
        CString strRegSubKey = thisClass::GetRegSubKey(nCodePage);
        LONG lResult = regKey.Create(HKEY_CURRENT_USER, strRegSubKey);
        if (lResult != ERROR_SUCCESS)
        {
            DWORD dwError = ::GetLastError();
            return HRESULT_FROM_WIN32(dwError);
        }
 
        lResult = regKey.DeleteValue(szFontFace);
        if (lResult != ERROR_SUCCESS)
        {
            DWORD dwError = ::GetLastError();
            return HRESULT_FROM_WIN32(dwError);
        }
 
        return S_OK;
    }
 
protected:
    static CString GetRegSubKey(int nCodePage)
    {
        CString strResult;
        strResult.Format(_T("EUDC\\%d"), nCodePage);
        return strResult;
    }
    class CApplyEUDC
    {
    public:
        CApplyEUDC()
        {
            BOOL br = CEudcHelper::Probe_EnableEUDC(FALSE);
            if (!br)
            {
                // 無視する。。
            }
        }
        virtual ~CApplyEUDC()
        {
            BOOL br = CEudcHelper::Probe_EnableEUDC(TRUE);
            if (!br)
            {
                // 無視する。。
            }
        }
    };
};
 
 
/////////////////////////////////////////////////////////////////////////////
// CEudcHolder
 
class CEudcHolder  
{
protected:
    typedef CEudcHolder                            thisClass;
 
public:
    CEudcHolder()
    {
        m_nCodePage = -1;
    }
    CEudcHolder(LPCTSTR szFontFace, LPCTSTR szFileName, UINT nCodePage = 932)
    {
        HRESULT hr = S_OK;
 
        m_nCodePage = -1;
 
        hr = this->Register(szFontFace, szFileName, nCodePage);
        if (FAILED(hr))
        {
            // 無視する。。
        }
    }
    virtual ~CEudcHolder()
    {
        HRESULT hr = S_OK;
 
        if (m_nCodePage != -1)
        {
            hr = this->Unregister();
            if (FAILED(hr))
            {
                // 無視する。。
            }
        }
    }
 
public:
    HRESULT Register(LPCTSTR szFontFace, LPCTSTR szFileName, UINT nCodePage = 932)
    {
        ATLASSERT(m_nCodePage == -1);
        ATLASSERT(nCodePage != -1);
 
        HRESULT hr = S_OK;
 
        hr = CEudcHelper::RegsterEUDC(szFontFace, szFileName, nCodePage);
        if (FAILED(hr))
        {
            // 無視する。。
        }
 
        m_strFontFace = szFontFace;
        m_strFileName = szFileName;
        m_nCodePage = nCodePage;
 
        return hr;
    }
    HRESULT Unregister()
    {
        ATLASSERT(m_nCodePage != -1);
 
        HRESULT hr = S_OK;
 
        hr = CEudcHelper::UnregsterEUDC(m_strFontFace, m_nCodePage);
        if (FAILED(hr))
        {
            // 無視する。。
        }
 
        m_strFontFace = _T("");
        m_strFileName = _T("");
        m_nCodePage = -1;
 
        return hr;
    }
 
protected:
    CString m_strFontFace;
    CString m_strFileName;
    UINT m_nCodePage;
};
一覧に戻る
© 2003 WAC.com All Right Reserved.