首页 > 代码库 > Wince动态加载字体资源

Wince动态加载字体资源

#define     SUB_KEY   L"SOFTWARE\\Microsoft\\FontLink\\SystemLink" //子键
#define     KEY_VALUE L"\\NANDFLASH\\YeedonMedia\\Font\\msyh.ttf,微软雅黑" //键值
#define     FONT_PATH L"\\NANDFLASH\\YeedonMedia\\Font\\msyh.ttf"//字体路径

void ResetFontRegedit()
{
     DWORD dwIndex = 0, NameSize, NameCnt, NameMaxLen, Type;
     DWORD KeySize, KeyCnt, KeyMaxLen, DateSize, MaxDateLen;
     WCHAR *szKeyName;
     WCHAR *szValueName;
     LPBYTE szValueDate;
     HKEY hKey = NULL;

     LONG nResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SUB_KEY, 0, KEY_ALL_ACCESS, &hKey);
     if(nResult != ERROR_SUCCESS)
     {
        printf("打开注册表失败! RegOpenKeyEx return %d, GetLastError() return %d", nResult, GetLastError());
     }

     //获取子键信息
     if(RegQueryInfoKey(hKey, NULL, NULL, NULL, &KeyCnt, &KeyMaxLen, NULL, &NameCnt, &NameMaxLen, &MaxDateLen, NULL, NULL) != ERROR_SUCCESS)
     {
        printf("(RegQueryInfoKey错误! GetLastError() return %d", GetLastError());
        ::RegCloseKey(hKey);
     }

     //枚举键值信息
     for(dwIndex = 0; dwIndex < NameCnt; dwIndex++)//枚举键值
     {
        DateSize = MaxDateLen + 1;
        NameSize = NameMaxLen + 1;
        szValueName = (WCHAR*)malloc(NameSize);
        szValueDate = (LPBYTE)malloc(DateSize);
        RegEnumValue(hKey, dwIndex, szValueName, &NameSize, NULL, &Type, szValueDate, &DateSize);//读取键值
        
        if(Type == REG_SZ)
        {
           //判断键值项类型并做其他修改
           printf("%s\n", szValueName);

           RegSetValueEx(hKey, szValueName, 0, REG_SZ, (byte*)KEY_VALUE, sizeof(KEY_VALUE));
        }
     }
     ::RegCloseKey(hKey);
}

void AddFontResource()
{
     RemoveFontResource();
     if(::AddFontResource(FONT_PATH) == 0) //加载字体失败
     {
          printf("加载字体资源失败!\n");
     }
     ::SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
}

void RemoveFontResource()
{
     RemoveFontResource(FONT_PATH);
     ::SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
}

Wince动态加载字体资源