做过界面开发的人想必都清楚如果要用VC++来开发漂亮的程序界面其难度真是苦不堪言, 当然VC++可以作出漂亮的界面但要投入大量的人力和时间其代码量可以用海量来形容。BUG自然也就不计其数。本文主要介绍如何采用VC++结合FLASH在短时间内以及很少的人力投入的情况开发出精美漂亮的软件界面。在本例中VC++主要进行相关数据逻辑及业务处理,FLASH则进行相关的界面表现。FLASH和VC++之间采用XML流通信。VC++的XML解析库为开源的TinyXml解析库。 本实例的开发环境为visual studio 2003 + FLASH8.0。其效果图如下:

下面将进行详细介绍:
1、首先建一个基于MFC的对话框应用程序。
2、在对话框中插入名为Shockwave Flash Object的ActiveX控件.
3、为该FLASH控件添加相应的变量即FSCommand消息处理函数.
4、在OnInitDialog()函数添加如下代码将相关数据传给FLASH界面
1
// TODO: 在此添加额外的初始化代码
2
//加载flash界面
3
char szBuf[256];
4
string szXml;
5
6
CString szFlashPath(m_FlashCtrl.GetModulePath());
7
szFlashPath += "演示界面.swf";
8
9
m_FlashCtrl.LoadMovie(0, szFlashPath);
10
MoveWindow(0, 0, 490, 345);
11
m_FlashCtrl.MoveWindow(0, 0, 490, 345);
12
13
//设置标题栏
14
TiXmlElement xRoot("win_app");
15
sprintf(szBuf, "%u", 0xf0000006);
16
xRoot.SetAttribute("style", szBuf);
17
xRoot.SetAttribute("event", 13);
18
19
TiXmlElement xItem("item");
20
xItem.SetAttribute("title", "我的测试程序");
21
xRoot.InsertEndChild(xItem);
22
szXml << xRoot;
23
m_FlashCtrl.SetVariable("_root.g_Protocol.win_data", szXml.c_str());
24
25
//设置组合框的数据
26
xRoot.Clear();
27
szXml.clear();
28
xRoot.SetAttribute("event", 76);
29
30
TiXmlElement xQuery("query");
31
xQuery.SetAttribute("sel_item", 1);
32
//xRoot.InsertEndChild(xQuery);
33
34
xItem.RemoveAttribute("title");
35
xItem.SetAttribute("info", "武林外传");
36
xQuery.InsertEndChild(xItem);
37
38
xItem.SetAttribute("info", "西游记");
39
xQuery.InsertEndChild(xItem);
40
41
xItem.SetAttribute("info", "三国演义");
42
xQuery.InsertEndChild(xItem);
43
44
xItem.SetAttribute("info", "红楼梦");
45
xQuery.InsertEndChild(xItem);
46
47
xItem.SetAttribute("info", "魔法英雄");
48
xQuery.InsertEndChild(xItem);
49
xRoot.InsertEndChild(xQuery);
50
51
szXml << xRoot;
52
m_FlashCtrl.SetVariable("_root.g_Protocol.win_data", szXml.c_str());
53
54
//设置列表框数据
55
xRoot.Clear();
56
xQuery.Clear();
57
szXml.clear();
58
xRoot.SetAttribute("event", 77);
59
xQuery.SetAttribute("sel_item", 1);
60
61
xItem.SetAttribute("info", "汉皇重色思倾国");
62
xQuery.InsertEndChild(xItem);
63
64
xItem.SetAttribute("info", "御宇多年求不得");
65
xQuery.InsertEndChild(xItem);
66
67
xItem.SetAttribute("info", "杨家有女初长成");
68
xQuery.InsertEndChild(xItem);
69
70
xItem.SetAttribute("info", "养在深闺人未识");
71
xQuery.InsertEndChild(xItem);
72
73
xItem.SetAttribute("info", "天生丽质难自弃");
74
xQuery.InsertEndChild(xItem);
75
76
xRoot.InsertEndChild(xQuery);
77
78
szXml << xRoot;
79
m_FlashCtrl.SetVariable("_root.g_Protocol.win_data", szXml.c_str());
5、在OnFsCommand()中处理FLASH发来的消息.
1
void CUIShowDlg::OnFsCommand(LPCTSTR command, LPCTSTR args)
2

{
3
try
4
{
5
int nEvent = 0;
6
TiXmlDocument xmlData;
7
xmlData.Parse(command);
8
if (xmlData.Error())
9
{
10
throw (__LINE__);
11
}
12
13
TiXmlElement *pRoot = xmlData.FirstChildElement();
14
if (NULL == pRoot)
15
{
16
throw (__LINE__);
17
}
18
pRoot->Attribute("event", &nEvent);
19
20
switch(nEvent)
21
{
22
case 0xff000000: //移动窗口消息
23
SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
24
break;
25
case 0xff000001: //关闭窗口
26
EndDialog(0);
27
break;
28
case 0xff000003: //最小化窗口
29
ShowWindow(SW_MINIMIZE);
30
break;
31
default:
32
break;
33
}
34
}
35
catch (
)
36
{
37
TRACE("\r\n收到无效的命令 : %s", command);
38
}
39
}
40
到此一款精美的软件界面已经开发完成。有兴趣的朋友可以下载我的源代码看看。
/Files/beifangying/UIShow.rar