UG二次开发完整示例程序,MFC连接数据库,UIStyler调用
花键轴设计的主程序 Database_main.cpp : implementation file#include "stdafx.h"
#include "app_data.h"
#include "Database_main.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CDatabase_main::CDatabase_main(CWnd* pParent /*=NULL*/)
: CDialog(CDatabase_main::IDD, pParent)
{
//{{AFX_DATA_INIT(CDatabase_main)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CDatabase_main::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDatabase_main)
DDX_Control(pDX, IDC_LIST_PARA, m_list_control);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDatabase_main, CDialog)
//{{AFX_MSG_MAP(CDatabase_main)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CDatabase_main::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
try{
m_pdatabase=new CDatabase();
m_pset=new CRecordset();
m_pdatabase->Open(_T("data"),FALSE,FALSE,_T("ODBC;DSN=data"),FALSE);
m_pset->m_pDatabase=m_pdatabase;
}
catch(CDBException* e){
e->ReportError();
e->Delete();
delete m_pset;
delete m_pdatabase;
return TRUE;}
DataShow("data");
return TRUE;// return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDatabase_main::ClearList()
{
//m_list_control.SetExtendedStyle(LVS_EX_FULLROWSELECT);
//m_list_control.SetExtendedStyle(LVS_EX_GRIDLINES);
m_list_control.DeleteAllItems();
while(m_list_control.DeleteColumn(0));
UpdateWindow();
}
void CDatabase_main::DataShow(CString table_name)
{
ClearList();
CString strSQL;
strSQL="select * from "+table_name;
ShowInformation(strSQL);
}
BOOL CDatabase_main::ShowInformation(CString strSQL)
{
if(!m_pdatabase->IsOpen()) return FALSE;
if(!m_pset) return FALSE;
try{
BeginWaitCursor();
if(m_pset->IsOpen()) m_pset->Close();
m_pset->Open(CRecordset::dynaset,strSQL);
if(!m_pset->IsEOF())
{m_pset->MoveLast();
m_pset->MoveFirst();}
nFieldCount=m_pset->GetODBCFieldCount();
CODBCFieldInfo fieldinfo;
for(int n=0;n<nFieldCount;n++){
m_pset->GetODBCFieldInfo(n,fieldinfo);
int nWidth=m_list_control.GetStringWidth(fieldinfo.m_strName)+35;
m_list_control.InsertColumn(n,fieldinfo.m_strName,LVCFMT_RIGHT,nWidth);
}
CString strValue;
m_pset->MoveFirst();
int nCount=0;
while(!m_pset->IsEOF()){
m_list_control.InsertItem(nCount,strValue);
for(int j=0;j<nFieldCount;j++){
m_pset->GetFieldValue(j,strValue);
m_list_control.SetItemText(nCount,j,strValue);
}
m_pset->MoveNext();
nCount++;
}
EndWaitCursor();
}
catch(CDBException *e){
e->Delete();
EndWaitCursor();
return FALSE;
}
return TRUE;
}
void CDatabase_main::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
delete m_pset;
delete m_pdatabase;
}
extern CString str;
void CDatabase_main::OnOK()
{
// TODO: Add extra validation here
UINT uSelectedCount=m_list_control.GetSelectedCount();
if(uSelectedCount>1||uSelectedCount==0)
{
if(uSelectedCount==0)
{
MessageBox("要选择一条参数!","提示",MB_ICONWARNING+MB_OK);
return;
}
if(uSelectedCount>1)
{
MessageBox("只能选择一条参数!","提示",MB_ICONWARNING+MB_OK);
return;
}
}
else
{
int nItemSel=0;
nItemSel=m_list_control.GetNextItem(nItemSel-1,LVNI_SELECTED);
for(int j=0;j<nFieldCount;j++)
str=m_list_control.GetItemText(nItemSel,j);
}
CDialog::OnOK();
}
圆头直杆圆凸模的主程序:
Database_main.cpp : implementation file
#include "stdafx.h"
#include "tumo_data.h"
#include "Database_main.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CDatabase_main dialog
CDatabase_main::CDatabase_main(CWnd* pParent /*=NULL*/)
: CDialog(CDatabase_main::IDD, pParent)
{
//{{AFX_DATA_INIT(CDatabase_main)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CDatabase_main::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDatabase_main)
DDX_Control(pDX, IDC_LIST_PARA, m_list_control);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDatabase_main, CDialog)
//{{AFX_MSG_MAP(CDatabase_main)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CDatabase_main message handlers
BOOL CDatabase_main::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
try{
m_pdatabase=new CDatabase();
m_pset=new CRecordset();
m_pdatabase->Open(_T("data"),FALSE,FALSE,_T("ODBC;DSN=data"),FALSE);
m_pset->m_pDatabase=m_pdatabase;
}
catch(CDBException* e){
e->ReportError();
e->Delete();
delete m_pset;
delete m_pdatabase;
return TRUE;}
DataShow("data1");
return TRUE;// return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDatabase_main::ClearList()
{
//m_list_control.SetExtendedStyle(LVS_EX_FULLROWSELECT);
//m_list_control.SetExtendedStyle(LVS_EX_GRIDLINES);
m_list_control.DeleteAllItems();
while(m_list_control.DeleteColumn(0));
UpdateWindow();
}
void CDatabase_main::DataShow(CString table_name)
{
ClearList();
CString strSQL;
strSQL="select * from "+table_name;
ShowInformation(strSQL);
}
BOOL CDatabase_main::ShowInformation(CString strSQL)
{
if(!m_pdatabase->IsOpen()) return FALSE;
if(!m_pset) return FALSE;
try{
BeginWaitCursor();
if(m_pset->IsOpen()) m_pset->Close();
m_pset->Open(CRecordset::dynaset,strSQL);
if(!m_pset->IsEOF())
{m_pset->MoveLast();
m_pset->MoveFirst();}
nFieldCount=m_pset->GetODBCFieldCount();
CODBCFieldInfo fieldinfo;
for(int n=0;n<nFieldCount;n++){
m_pset->GetODBCFieldInfo(n,fieldinfo);
int nWidth=m_list_control.GetStringWidth(fieldinfo.m_strName)+35;
m_list_control.InsertColumn(n,fieldinfo.m_strName,LVCFMT_RIGHT,nWidth);
}
CString strValue;
m_pset->MoveFirst();
int nCount=0;
while(!m_pset->IsEOF()){
m_list_control.InsertItem(nCount,strValue);
for(int j=0;j<nFieldCount;j++){
m_pset->GetFieldValue(j,strValue);
m_list_control.SetItemText(nCount,j,strValue);
}
m_pset->MoveNext();
nCount++;
}
EndWaitCursor();
}
catch(CDBException *e){
e->Delete();
EndWaitCursor();
return FALSE;
}
return TRUE;
}
void CDatabase_main::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
delete m_pset;
delete m_pdatabase;
}
extern CString str;
void CDatabase_main::OnOK()
{
// TODO: Add extra validation here
UINT uSelectedCount=m_list_control.GetSelectedCount();
if(uSelectedCount>1||uSelectedCount==0)
{
if(uSelectedCount==0)
{
MessageBox("要选择一条参数!","提示",MB_ICONWARNING+MB_OK);
return;
}
if(uSelectedCount>1)
{
MessageBox("只能选择一条参数!","提示",MB_ICONWARNING+MB_OK);
return;
}
}
else
{
int nItemSel=0;
nItemSel=m_list_control.GetNextItem(nItemSel-1,LVNI_SELECTED);
for(int j=0;j<nFieldCount;j++)
str=m_list_control.GetItemText(nItemSel,j);
}
CDialog::OnOK();
}
花键轴数据库的连接程序app.cpp
#include <stdio.h>
#include <uf.h>
#include <uf_defs.h>
#include <uf_exit.h>
#include <uf_ui.h>
#include <uf_styler.h>
#include <uf_mb.h>
#include <stdlib.h>
#include <uf_modl_expressions.h>
#include <uf_part.h>
#include <uf_modl.h>
#include <uf_assem.h>
#include <math.h>
#include "app.h"
/* The following definition defines the number of callback entries */
/* in the callback structure: */
/* UF_STYLER_callback_info_t APP_cbs */
#define APP_CB_COUNT ( 6 + 1 ) /* Add 1 for the terminator */
extern "C" DllExport bool para_database_func(void *str_mid);
UF_STYLER_item_value_type_t data;
static UF_STYLER_callback_info_t APP_cbs =
{
{UF_STYLER_DIALOG_INDEX, UF_STYLER_CONSTRUCTOR_CB, 0, APP_constructor_cb},
{UF_STYLER_DIALOG_INDEX, UF_STYLER_DESTRUCTOR_CB , 0, APP_destructor_cb},
{UF_STYLER_DIALOG_INDEX, UF_STYLER_OK_CB , 0, APP_ok_cb},
{UF_STYLER_DIALOG_INDEX, UF_STYLER_APPLY_CB , 0, APP_apply_cb},
{UF_STYLER_DIALOG_INDEX, UF_STYLER_CANCEL_CB , 0, APP_cancel_cb},
{APP_ACTION_DATA , UF_STYLER_ACTIVATE_CB , 0, APP_action_database},
{UF_STYLER_NULL_OBJECT, UF_STYLER_NO_CB, 0, 0 }
};
static UF_MB_styler_actions_t actions[] = {
{ "app.dlg",NULL, APP_cbs,UF_MB_STYLER_IS_NOT_TOP },
{ NULL,NULL,NULL,0 } /* This is a NULL terminated list */
};
//#ifdef MENUBAR_COMMENTED_OUT
extern void ufsta (char *param, int *retcode, int rlen)
{
interror_code;
if ( (UF_initialize()) != 0)
return;
if ( (error_code = UF_MB_add_styler_actions ( actions ) ) != 0 )
{
char fail_message;
UF_get_fail_message(error_code, fail_message);
printf ( "%s\n", fail_message );
}
UF_terminate();
return;
}
//#endif /*MENUBAR_COMMENTED_OUT*/
#ifdef DISPLAY_FROM_CALLBACK
extern int <enter the name of your function> ( int *response )
{
interror_code = 0;
if ( ( error_code = UF_initialize() ) != 0 )
return (0) ;
if ( ( error_code = UF_STYLER_create_dialog ( "app.dlg",
APP_cbs, /* Callbacks from dialog */
APP_CB_COUNT, /* number of callbacks*/
NULL, /* This is your client data */
response ) ) != 0 )
{
char fail_message;
/* Get the user function fail message based on the fail code.*/
UF_get_fail_message(error_code, fail_message);
UF_UI_set_status (fail_message);
printf ( "%s\n", fail_message );
}
UF_terminate();
return (error_code);
}
#endif /* DISPLAY_FROM_CALLBACK */
#ifdef DISPLAY_FROM_USER_EXIT
extern void <enter a valid user exit here> (char *param, int *retcode, int rlen)
{
intresponse = 0;
interror_code = 0;
if ( ( UF_initialize() ) != 0 )
return;
if ( ( error_code = UF_STYLER_create_dialog ( "app.dlg",
APP_cbs, /* Callbacks from dialog */
APP_CB_COUNT, /* number of callbacks*/
NULL, /* This is your client data */
&response ) ) != 0 )
{
char fail_message;
/* Get the user function fail message based on the fail code.*/
UF_get_fail_message(error_code, fail_message);
UF_UI_set_status (fail_message);
printf ( "%s\n", fail_message );
}
UF_terminate();
return;
}
extern int ufusr_ask_unload (void)
{
/* unload immediately after application exits*/
return ( UF_UNLOAD_IMMEDIATELY );
/*via the unload selection dialog... */
/*return ( UF_UNLOAD_SEL_DIALOG ); */
/*when UG terminates... */
/*return ( UF_UNLOAD_UG_TERMINATE ); */
}
extern void ufusr_cleanup (void)
{
return;
}
#endif /* DISPLAY_FROM_USER_EXIT */
int APP_constructor_cb ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
UF_terminate ();
/* Callback acknowledged, do not terminate dialog */
return (UF_UI_CB_CONTINUE_DIALOG);
/* A return value of UF_UI_CB_EXIT_DIALOG will not be accepted */
/* for this callback type.You must continue dialog construction.*/
}
/*
int APP_destructor_cb ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
UF_terminate ();
/* Callback acknowledged, do not terminate dialog. */
/* A return value of UF_UI_CB_EXIT_DIALOG will not be accepted*/
/* for this callback type.You must continue dialog destruction*/
return (UF_UI_CB_CONTINUE_DIALOG);
}
int APP_ok_cb ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
int i;
write_para(dialog_id);
const char env="UGII_USER_DIR";
char *basedir=NULL;
basedir=getenv(env);
char dir;
strcpy(dir,basedir);
strcat(dir,"\\grip\\huajianzhou.grx");
char *grip_exe=dir;
int status;
int grip_arg_count=7; //参数个数
UF_args_t grip_arg_list;
grip_arg_list.type=UF_TYPE_DOUBLE;
grip_arg_list.length=0;
double fdat=data.value.integer;
grip_arg_list.address=&fdat;
for(i=1;i<7;i++)
{
grip_arg_list.type=UF_TYPE_DOUBLE;
grip_arg_list.length=0;
grip_arg_list.address=&data.value.real;
}
status=UF_call_grip(grip_exe,grip_arg_count,grip_arg_list);
UF_terminate ();
/* Callback acknowledged, terminate dialog */
/* It is STRONGLY recommended that you exit your */
/* callback with UF_UI_CB_EXIT_DIALOG in a ok callback.*/
/* return ( UF_UI_CB_EXIT_DIALOG ); */
return (UF_UI_CB_EXIT_DIALOG);
}
int APP_apply_cb ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
int i;
write_para(dialog_id);
const char env="UGII_USER_DIR";
char *basedir=NULL;
basedir=getenv(env);
char dir;
strcpy(dir,basedir);
strcat(dir,"\\grip\\huajianzhou.grx");
char *grip_exe=dir;
int status;
int grip_arg_count=7; //参数个数
UF_args_t grip_arg_list;
grip_arg_list.type=UF_TYPE_DOUBLE;
grip_arg_list.length=0;
double fdat=data.value.integer;
grip_arg_list.address=&fdat;
for(i=1;i<7;i++)
{
grip_arg_list.type=UF_TYPE_DOUBLE;
grip_arg_list.length=0;
grip_arg_list.address=&data.value.real;
}
status=UF_call_grip(grip_exe,grip_arg_count,grip_arg_list);
UF_terminate ();
/* Callback acknowledged, do not terminate dialog */
/* A return value of UF_UI_CB_EXIT_DIALOG will not be accepted */
/* for this callback type.You must respond to your apply button.*/
return (UF_UI_CB_CONTINUE_DIALOG);
}
void write_para(int dialog_id)
{
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_INT_N;
UF_STYLER_ask_value(dialog_id,&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_D1;
UF_STYLER_ask_value(dialog_id,&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_D2;
UF_STYLER_ask_value(dialog_id,&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_W;
UF_STYLER_ask_value(dialog_id,&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_K;
UF_STYLER_ask_value(dialog_id,&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_L;
UF_STYLER_ask_value(dialog_id,&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_LL;
UF_STYLER_ask_value(dialog_id,&data);
}
/*
int APP_cancel_cb ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
UF_terminate ();
/* Callback acknowledged, terminate dialog */
/* It is STRONGLY recommended that you exit your */
/* callback with UF_UI_CB_EXIT_DIALOG in a cancel call */
/* back rather than UF_UI_CB_CONTINUE_DIALOG. */
return ( UF_UI_CB_EXIT_DIALOG );
}
/*
void read_para_from_database(int dialog_id,void *str_mid)
{
/* Make sure User Function is available. */
/* ---- Enter your callback code here ----- */
UF_STYLER_item_value_type_t data;
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_D1;
data.value.real=atof(((char **)str_mid));
UF_STYLER_set_value(dialog_id,&data);
UF_STYLER_free_value(&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_D2;
data.value.real=atof(((char **)str_mid));
UF_STYLER_set_value(dialog_id,&data);
UF_STYLER_free_value(&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_W;
data.value.real=atof(((char **)str_mid));
UF_STYLER_set_value(dialog_id,&data);
UF_STYLER_free_value(&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_INT_N;
data.value.integer=atof(((char **)str_mid));
UF_STYLER_set_value(dialog_id,&data);
UF_STYLER_free_value(&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_L;
data.value.real=atof(((char **)str_mid));
UF_STYLER_set_value(dialog_id,&data);
UF_STYLER_free_value(&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_K;
data.value.real=atof(((char **)str_mid));
UF_STYLER_set_value(dialog_id,&data);
UF_STYLER_free_value(&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=APP_REAL_LL;
data.value.real=atof(((char **)str_mid));
UF_STYLER_set_value(dialog_id,&data);
UF_STYLER_free_value(&data);
}
int APP_action_database ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
char *str_mid;
if(para_database_func(str_mid)==true)
read_para_from_database(dialog_id,str_mid);
for(int i=0;i<8;i++)
{
delete [] str_mid;
}
UF_terminate ();
/* Callback acknowledged, do not terminate dialog */
return (UF_UI_CB_CONTINUE_DIALOG);
/* or Callback acknowledged, terminate dialog. */
/* return ( UF_UI_CB_EXIT_DIALOG ); */
}
根据自己的需要,修改这样的例子时候,一定要注意修改指针的个数,数据类型,要不然总会出错,调试了一天才弄出来
页:
[1]