forked from yiywain/SouiChartDemo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SouiChartDemo.cpp
209 lines (196 loc) · 10.4 KB
/
SouiChartDemo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// dui-demo.cpp : main source file
//
#include "stdafx.h"
#include "MainDlg.h"
//从PE文件加载,注意从文件加载路径位置
#define RES_TYPE 0
//#define SYSRES_TYPE 0
// #define RES_TYPE 0 //PE
// #define RES_TYPE 1 //ZIP
// #define RES_TYPE 2 //7z
// #define RES_TYPE 2 //文件
//去掉多项支持,以免代码显得混乱
#if (RES_TYPE==1)
#include "resprovider-zip\zipresprovider-param.h"
#else
#if (RES_TYPE==2)
#include "resprovider-7zip\zip7resprovider-param.h"
#endif
#endif
#ifdef _DEBUG
#define SYS_NAMED_RESOURCE _T("soui-sys-resourced.dll")
#else
#define SYS_NAMED_RESOURCE _T("soui-sys-resource.dll")
#endif
//定义唯一的一个R,UIRES对象,ROBJ_IN_CPP是resource.h中定义的宏。
ROBJ_IN_CPP
class SOUIEngine
{
private:
SComMgr m_ComMgr;
SApplication *m_theApp;
bool m_bInitSucessed;
public:
SOUIEngine(HINSTANCE hInstance):m_theApp(NULL), m_bInitSucessed(false){
CAutoRefPtr<SOUI::IRenderFactory> pRenderFactory;
BOOL bLoaded = FALSE;
//使用GDI渲染界面
bLoaded = m_ComMgr.CreateRender_GDI((IObjRef * *)& pRenderFactory);
SASSERT_FMT(bLoaded, _T("load interface [render] failed!"));
//设置图像解码引擎。默认为GDIP。基本主流图片都能解码。系统自带,无需其它库
CAutoRefPtr<SOUI::IImgDecoderFactory> pImgDecoderFactory;
bLoaded = m_ComMgr.CreateImgDecoder((IObjRef * *)& pImgDecoderFactory);
SASSERT_FMT(bLoaded, _T("load interface [%s] failed!"), _T("imgdecoder"));
pRenderFactory->SetImgDecoderFactory(pImgDecoderFactory);
m_theApp = new SApplication(pRenderFactory, hInstance);
m_bInitSucessed = (TRUE==bLoaded);
};
operator bool()const
{
return m_bInitSucessed;
}
//加载系统资源
bool LoadSystemRes()
{
BOOL bLoaded = FALSE;
//从DLL加载系统资源
{
HMODULE hModSysResource = LoadLibrary(SYS_NAMED_RESOURCE);
if (hModSysResource)
{
CAutoRefPtr<IResProvider> sysResProvider;
CreateResProvider(RES_PE, (IObjRef * *)& sysResProvider);
sysResProvider->Init((WPARAM)hModSysResource, 0);
m_theApp->LoadSystemNamedResource(sysResProvider);
FreeLibrary(hModSysResource);
}
else
{
SASSERT(0);
}
}
return TRUE==bLoaded;
}
//加载用户资源
bool LoadUserRes()
{
CAutoRefPtr<IResProvider> pResProvider;
BOOL bLoaded = FALSE;
#ifdef _DEBUG
//选择了仅在Release版本打包资源则在DEBUG下始终使用文件加载
{
CreateResProvider(RES_FILE, (IObjRef * *)& pResProvider);
bLoaded = pResProvider->Init((LPARAM)_T("uires"), 0);
SASSERT(bLoaded);
}
#else
{
CreateResProvider(RES_PE, (IObjRef * *)& pResProvider);
bLoaded = pResProvider->Init((WPARAM)m_theApp->GetInstance(), 0);
SASSERT(bLoaded);
}
#endif
m_theApp->InitXmlNamedID(namedXmlID, ARRAYSIZE(namedXmlID), TRUE);
m_theApp->AddResProvider(pResProvider);
return TRUE==bLoaded;
}
//加载LUA支持
bool LoadLUAModule()
{
BOOL bLoaded =FALSE;
CAutoRefPtr<SOUI::IScriptFactory> pScriptLuaFactory;
bLoaded = m_ComMgr.CreateScrpit_Lua((IObjRef * *)& pScriptLuaFactory);
SASSERT_FMT(bLoaded, _T("load interface [%s] failed!"), _T("scirpt_lua"));
m_theApp->SetScriptFactory(pScriptLuaFactory);
return TRUE == bLoaded;
}
//加载多语言支持
bool LoadMultiLangsModule()
{
BOOL bLoaded = FALSE;
CAutoRefPtr<ITranslatorMgr> trans;
bLoaded = m_ComMgr.CreateTranslator((IObjRef * *)& trans);
SASSERT_FMT(bLoaded, _T("load interface [%s] failed!"), _T("translator"));
if (trans)
{//加载语言翻译包
m_theApp->SetTranslator(trans);
pugi::xml_document xmlLang;
if (m_theApp->LoadXmlDocment(xmlLang, _T("translator:lang_cn")))
{
CAutoRefPtr<ITranslator> langCN;
trans->CreateTranslator(&langCN);
langCN->Load(&xmlLang.child(L"language"), 1);//1=LD_XML
trans->InstallTranslator(langCN);
}
}
return TRUE == bLoaded;
}
//注册用户自定义皮肤和控件
void Regitercustom()
{
m_theApp->RegisterWindowClass<SLineChart>();
m_theApp->RegisterWindowClass<SBarChart>();
m_theApp->RegisterWindowClass<SPieChart>();
}
~SOUIEngine()
{
if (m_theApp)
{
delete m_theApp;
m_theApp = NULL;
}
}
template<class MainWnd>
int Run()
{
MainWnd dlgMain;
dlgMain.Create(GetActiveWindow());
dlgMain.SendMessage(WM_INITDIALOG);
dlgMain.CenterWindow(dlgMain.m_hWnd);
dlgMain.ShowWindow(SW_SHOWNORMAL);
return m_theApp->Run(dlgMain.m_hWnd);
}
SApplication* GetApp()
{
return m_theApp;
}
};
//debug时方便调试设置当前目录以便从文件加载资源
void SetDefaultDir()
{
TCHAR szCurrentDir[MAX_PATH] = { 0 };
GetModuleFileName(NULL, szCurrentDir, sizeof(szCurrentDir));
LPTSTR lpInsertPos = _tcsrchr(szCurrentDir, _T('\\'));
#ifdef _DEBUG
_tcscpy(lpInsertPos + 1, _T("..\\SouiChartDemo"));
#else
_tcscpy(lpInsertPos + 1, _T("\0"));
#endif
SetCurrentDirectory(szCurrentDir);
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int /*nCmdShow*/)
{
HRESULT hRes = OleInitialize(NULL);
SASSERT(SUCCEEDED(hRes));
SetDefaultDir();
int nRet = 0;
{
SOUIEngine souiEngine(hInstance);
if (souiEngine)
{
//加载系统资源
souiEngine.LoadSystemRes();
//加载用户资源
souiEngine.LoadUserRes();
//注册用户自定义皮肤/控件/布局/其它自定义
souiEngine.Regitercustom();
nRet = souiEngine.Run<CMainDlg>();
}
else
{
MessageBox(NULL, _T("无法正常初使化SOUI"), _T("错误"), MB_OK | MB_ICONERROR);
}
}
OleUninitialize();
return nRet;
}