记念开始,今天一定要写什么东东
作者:shj 发布于:2014-1-22 16:56
随便写个。。。eve market.先随便上张截图。完整程序下载地址market.zip
主要用了几个控件,截图上看看就明白了。
在程序中用了sqllite的数据库,用于所有物品的基础信息。
下面贴点代码。
#pragma comment(lib,"sqlite3.lib") //连接了sqlite3的数据库。
open_sql("new_eve.db");//初始化时打开了new_eve.db这个数据库。
//打开和关闭数据库
int Cmarket_V01App::open_sql(char *dbname)
{
int rc = 0;
rc = sqlite3_open(dbname,&db);
if (rc != SQLITE_OK)
{
CString debug_out = sqlite3_errmsg(db);
sqlite3_close(db);
return -1;
}
return 0;
}
void Cmarket_V01App::close_sql()
{
if (db != NULL)
{
sqlite3_close(db);
}
}
初始化时build了整个树结构代码如下:
void Cmarket_V01App::build_tree(void *p)
{
Cmarket_V01Dlg *p_wnd = (Cmarket_V01Dlg *)p;
char * errmsg = NULL;
char **dbResult[5]; //是 char ** 类型,两个*号
int nRow[5], nColumn[5];
char sql[5][1024];
int result,i,j,x,y,z;
CString out;
p_wnd->m_tree.GetCapture();
HTREEITEM hTreeItemParent=NULL;
HTREEITEM hTreeItemRoot=NULL;
HTREEITEM hTreeChild[5] = {NULL};
if (db == NULL)
{
return ;
}
hTreeItemRoot = p_wnd->m_tree.GetRootItem();
hTreeItemParent=p_wnd->m_tree.InsertItem("吉他行情");
result = sqlite3_get_table( db, "select distinct classify1 from eve_data", &dbResult[0], &nRow[0], &nColumn[0], &errmsg );
if (result != SQLITE_OK)
{
return ;
}
out.Format("查到%d条记录\n", nRow[0] );
OutputDebugString(out);
//for( i = 0; i < nRow[0] ; i++ )
//{
// out.Format(" “第 %d 条记录\n", i );
// OutputDebugString(out);
// out.Format( "字段名:%s 字段值:%s\n", dbResult[0][0], dbResult[0][i+1] );
// OutputDebugString(out);
// out.Format( "-------\n" );
// OutputDebugString(out);
//}
SetWindowPos(AfxGetMainWnd()->m_hWnd, HWND_TOPMOST, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOSIZE);
for(i = 1;i < nRow[0];i++)
{
if (dbResult[0][i] == NULL)
{
continue;
}
out.Format("%s",dbResult[0][i]);
hTreeChild[0] = p_wnd->m_tree.InsertItem(out,hTreeItemParent);
sprintf(sql[0],"select distinct classify2 from eve_data where classify1 = '%s'",dbResult[0][i]);
result = sqlite3_get_table( db, sql[0], &dbResult[1], &nRow[1], &nColumn[1], &errmsg );
for( j = 1; j < nRow[1]+1 ; j++ )
{
if (dbResult[1][j] == NULL)
{
continue;
}
out.Format("查到%d条记录\n", nRow[1] );
OutputDebugString(out);
//out.Format(" “第 %d 条记录\n", j );
//OutputDebugString(out);
//out.Format( "字段名:%s 字段值:%s\n", dbResult[1][0], dbResult[1][j] );
//OutputDebugString(out);
//out.Format( "-------\n" );
//OutputDebugString(out);
out.Format("%s",dbResult[1][j]);
hTreeChild[1] = p_wnd->m_tree.InsertItem(out,hTreeChild[0]);
}
sqlite3_free_table(dbResult[1]);
}
sqlite3_free_table(dbResult[0]);
return ;
}
因为数据库里面条数太多,整个EVE的数据库有5W多条。我们用的又是轻量级的数据库,在生成时查询所有数据并显示会导致程序启动需要初始化的时间过长。 这里我们启动时只加载前几个大项的数据,并没有加载所有数据。当选择具体的条目时在根据条目来进行添加。选择时的代码如下:
void Cmarket_V01Dlg::OnNMClickTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
CPoint pt;
int i = 0;
GetCursorPos(&pt);
m_tree.ScreenToClient(&pt);
CString classify_text[8]= {""};
int classify_count = 0;
UINT nFlag = 0;
HTREEITEM hFather[8] = {NULL};
HTREEITEM hChild = NULL;
HTREEITEM hItem = m_tree.HitTest(pt, &nFlag);
if( NULL != hItem )
{
m_tree.SelectItem(hItem);
// CString strTemp=m_tree.GetItemText(hItem);
DWORD nID=m_tree.GetItemData(hItem);
if (nID != 0)
{
theApp.show_item((void *)this,nID);
UpdateData(FALSE);
*pResult = 0;
return ;
}
hFather[6] = m_tree.GetParentItem(hItem);
classify_text[0] = m_tree.GetItemText(hItem);
// classify_text.Format("%d,%s",nID,strTemp);
if (hFather[6] != NULL)
{
for (i = 5;i >= 0;i--)
{
hFather[i] = m_tree.GetParentItem(hFather[i+1]);
if (hFather[i] == NULL)
{
break;
}
}
classify_count = i;
for (i;i <= 6 ;i++)
{
classify_text[6 - i + 1] = m_tree.GetItemText(hFather[i]);
}
}
for (classify_count = 6 ;hFather[classify_count] != NULL;classify_count--)
{
;
}
classify_count = 6 - classify_count;
if (!m_tree.ItemHasChildren(hItem) )
{
printf("get");
theApp.add_item((void *)this,classify_text,classify_count,hItem);
}
UpdateData(FALSE);
}
*pResult = 0;
}
先写这些,数据库查询这些东东有需要的朋友留言我在贴吧。
« 不得不吐下炉石传说,更新太慢了。
|
开篇»
评论:


2014-08-07 13:21