Test Use CListControl and Events and colordlg,and odbc.

// TestItemListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testPint2.h"
#include "TestItemListDlg.h"
#include <SQL.H>
#include <SQLEXT.H>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// TestItemListDlg dialog


TestItemListDlg::TestItemListDlg(CWnd* pParent /*=NULL*/)
 : CDialog(TestItemListDlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(TestItemListDlg)
  // NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
}


void TestItemListDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(TestItemListDlg)
 DDX_Control(pDX, IDC_LISTContrl, m_listCon);
 DDX_Control(pDX, IDC_LIST1, m_list);
 //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(TestItemListDlg, CDialog)
 //{{AFX_MSG_MAP(TestItemListDlg)
 ON_BN_CLICKED(IDC_TestColor, OnTestColor)
 ON_NOTIFY(NM_DBLCLK, IDC_LISTContrl, OnDblclkLISTContrl)
 ON_BN_CLICKED(IDC_TestODBC, OnTestODBC)
 ON_BN_CLICKED(IDC_TestCreateDB, OnTestCreateDB)
 ON_BN_CLICKED(IDC_TestGetObjId, OnTestGetObjId)
 ON_BN_CLICKED(IDC_testAtoI, OntestAtoI)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// TestItemListDlg message handlers

void TestItemListDlg::OnOK()
{
 // TODO: Add extra validation here
 //add heder.
 for(int i=0;i<6;i++){
  CString temp;
  temp.Format("header%d",i);
 m_listCon.InsertColumn(i,temp);
 //BOOL SetColumnWidth(int nCol, int cx);
 m_listCon.SetColumnWidth(i,100);
 }
 //now set contents.
 //BOOL SetItemText(int nItem, int nSubItem, LPCTSTR lpszText);
 m_listCon.SetExtendedStyle(LVS_EX_FULLROWSELECT);
 //for(int j=0;j<12;j++)
 //m_listCon.InsertItem(j,"item1");//every item meanse one row.
 //m_listCon.SetItemText(1,2,"ss");]

 //try color
 //BOOL SetTextColor(COLORREF cr);
 //COLORREF GetTextBkColor() const;
 //BOOL SetTextBkColor(COLORREF cr);
 //=RGB(0, 255, 0);
 
 //COLORREF c=RGB(34,233,45);
    COLORREF c=color;
 //m_listCon.SetBkColor(c);
    //m_listCon.SetTextBkColor(c);

 CString temp;
 
 for(int row=0;row<12;row++){
    
  temp.Format("item"+row);
     m_listCon.InsertItem(row,temp);//every item meanse one row.
  //give every item set value.
  for(int col=0;col<6;col++){
   //if do not set col0,then col:0 use item.
  temp.Format("row%dCol%d",row,col);
  m_listCon.SetItemText(row,col,temp);
  }
 }
 
 
   //now test delete column
    //BOOL DeleteColumn(int nCol);
 //m_listCon.DeleteColumn(4);
 //ok!
 //delete all item.
 //m_listCon.DeleteAllItems();
 //CDialog::OnOK();
}

void TestItemListDlg::OnTestColor()
{
CColorDialog colorDialog;
COLORREF color1;
 
if( colorDialog.DoModal() == IDOK )
{
color1 = colorDialog.GetColor();
color=color1;
}
//this->Set 
}

void TestItemListDlg::OnDblclkLISTContrl(NMHDR* pNMHDR, LRESULT* pResult)
{
 // TODO: Add your control notification handler code here
 //get the now line and get some info.
// m_listCon.GetSelectedCount();
 int num= m_listCon.GetSelectionMark();
 //CString GetItemText(int nItem, int nSubItem) const;
    CString value;
 value=m_listCon.GetItemText(num, 1) ;
 CString temp;
 temp.Format("%d",num);
 AfxMessageBox(temp);
    AfxMessageBox(value);
 *pResult = 0;
}

void TestItemListDlg::OnTestODBC()
{
 //here try use odbc directly to get objid.
 //#include <SQL.H>
 //#include <SQLEXT.H>
 //ODBC32.LIBneed!!!!!!!1
  RETCODE rcode;

  HENV henv1;
  HDBC hdbc1;
  HSTMT hstmt1;

 //char szFirstName[50];
 char szLastName[50];
 //char szPhoneNum[20];
   SDWORD sdODataLength;
    unsigned char conStringOut[256];
    //1:
    rcode = ::SQLAllocEnv(&henv1);
    if (rcode != SQL_SUCCESS) {
  AfxMessageBox("allocEnvError.");
     return;
 }
    if (rcode == SQL_SUCCESS)
    { //2:
      rcode = ::SQLAllocConnect(henv1, & hdbc1);
      if (rcode != SQL_SUCCESS) {
  AfxMessageBox("AllocConnectError.");
     return;
   }
   if (rcode == SQL_SUCCESS)
      {   //3:
          rcode = ::SQLDriverConnect(hdbc1, 0,
          (unsigned char *)"DSN=JiSheng",
          SQL_NTS, conStringOut, 256, NULL,
          SQL_DRIVER_NOPROMPT);
    if (rcode != SQL_SUCCESS) {
    AfxMessageBox("DroverCpmmectEror.");
       return;
    }
         if (rcode == SQL_SUCCESS)
   {//4:
          rcode = ::SQLAllocStmt(hdbc1, &hstmt1);
    if (rcode != SQL_SUCCESS) {
    AfxMessageBox("SQLAllocStmtEror.");
       return;
    }
          if (rcode == SQL_SUCCESS)
    {   //5:execute!
              rcode = ::SQLExecDirect(hstmt1,
             (unsigned char *)
             "SELECT * FROM test",
              SQL_NTS);
             //6:get result by return code.
            for (rcode = ::SQLFetch(hstmt1);
              rcode == SQL_SUCCESS;
              rcode = SQLFetch(hstmt1))
            {
              ::SQLGetData(hstmt1, 1, SQL_C_CHAR,
                szLastName, 50,  & sdODataLength);
              ::MessageBox(NULL, szLastName,
                " from AddressBookDb ", MB_OK);
            }
            ::SQLFreeStmt(hstmt1, SQL_DROP);
          }
          ::SQLDisconnect(hdbc1);
        }
        ::SQLFreeConnect(hdbc1);
      }
      ::SQLFreeEnv(henv1);
    }
}
 

void TestItemListDlg::OnTestCreateDB()
{
//CDatabase db;
/*
try
{
db.Open("LocalServer");
MessageBox("´ò¿ª³É¹¦","Êý¾Ý¿â³õʼ»¯",MB_ICONINFORMATION);
db.ExecuteSQL("CREATE DATABASE yourdatebase");
MessageBox("´´½¨Êý¾Ý¿â³É¹¦","Êý¾Ý¿â³õʼ»¯",MB_ICONINFORMATION);

db.ExecuteSQL("CREATE TABLE yourdatebase..command(c_user CHAR(128),c_address CHAR(50),c_action CHAR(50),c_object CHAR(50),c_level CHAR(10),c_return CHAR(10),c_remark CHAR(200),c_date CHAR(50),c_time CHAR(50),c_index INT IDENTITY(1,1))");
MessageBox("´´½¨ÃüÁîÈÕÖ¾³É¹¦","Êý¾Ý¿â³õʼ»¯",MB_ICONINFORMATION);

db.Close();
}  
*/
}

void TestItemListDlg::OnTestGetObjId()
{
 //get one table's ID field's all value,and get the bigest one
 //and then add one.
 //to get bigger.
 //unsigned char sql[]="select id from test\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";// test";
 
 /* 1:
 unsigned char sql[]="select id from Tab_ÄÐÐÔ±£½¡·þÎñ¼Ç¼\0\0";
 int size=strlen((char*)sql);
 //unsigned char a='a';
 //char b='b';
 //a=(unsigned char)b;
 CString table="Tab_ÄÐÐÔ±£½¡·þÎñ¼Ç¼";//"Info_×Öµä";
 
    if(table.Compare("Tab_ÄÐÐÔ±£½¡·þÎñ¼Ç¼")==0){
 unsigned char sql1[]="select id from Tab_ÄÐÐÔ±£½¡·þÎñ¼Ç¼;\0\0";
 int size2=strlen(( char*)sql1);
 //sql=(unsigned char )sql1; 
 for(int j=0,i=0;i<size,j<size2;i++,j++){
  sql[i]=sql1[j];
  }//Öйý.
 }
    table="test";
 //now second table.
 if(table.Compare("test")==0){
 unsigned char sql1[]="select id from test;";
 int size2=strlen(( char*)sql1);
 //sql=(unsigned char )sql1; 
 for(int j=0,i=0;i<size,j<size2;i++,j++){
  sql[i]=sql1[j];
  }//Öйý.
 }

   */
   //now test get sql from table name,no use if.
   //to get any table's sql;
 CString tableN="Tab_ÄÐÐÔ±£½¡·þÎñ¼Ç¼";//"test";
 tableN+=";";
    unsigned char sql[100];
 char sqlH[100]="select ID from ";
 
    char *tableNC=tableN.GetBuffer(tableN.GetLength());
    strcat(sqlH,tableNC);
 for(int i=0;i<100;i++){
    sql[i]=(unsigned char)sqlH[i];
    }

 

 //turn ; into \0 as str's end;
 unsigned char *p=sql;
 while(*p){
 if(*p==';')
  *p='\0';
 p++;
 }
   //now should get right table.
MessageBox((char*)sql,"",MB_OK);
 //unsigned char *pT=table.GetBuffer(12);
  
 unsigned char *pSql=sql;

  RETCODE rcode;

  HENV henv1;
  HDBC hdbc1;
  HSTMT hstmt1;

 //char szFirstName[50];
 char nowValue[10];
 //char szPhoneNum[20];
   SDWORD sdODataLength;
    unsigned char conStringOut[256];
    //1:
    rcode = ::SQLAllocEnv(&henv1);
    if (rcode != SQL_SUCCESS) {
  AfxMessageBox("allocEnvError.");
     return;
 }
    if (rcode == SQL_SUCCESS)
    { //2:
      rcode = ::SQLAllocConnect(henv1, & hdbc1);
      if (rcode != SQL_SUCCESS) {
  AfxMessageBox("AllocConnectError.");
     return;
   }
   if (rcode == SQL_SUCCESS)
      {   //3:
          rcode = ::SQLDriverConnect(hdbc1, 0,
          (unsigned char *)"DSN=JiSheng",
          SQL_NTS, conStringOut, 256, NULL,
          SQL_DRIVER_NOPROMPT);
    if (rcode != SQL_SUCCESS) {
    AfxMessageBox("DroverCpmmectEror.");
       return;
    }
         if (rcode == SQL_SUCCESS)
   {//4:
          rcode = ::SQLAllocStmt(hdbc1, &hstmt1);
    if (rcode != SQL_SUCCESS) {
    AfxMessageBox("SQLAllocStmtEror.");
       return;
    }
          if (rcode == SQL_SUCCESS)
    {   //5:execute!
              rcode = ::SQLExecDirect(hstmt1,
             //(unsigned char *)
             // "SELECT * FROM test",
    //sql,
    //(unsigned char*)"select id from test",
    pSql,
              SQL_NTS);
             //6:get result by return code.
     long nowId=0;
            for (rcode = ::SQLFetch(hstmt1);
              rcode == SQL_SUCCESS;
              rcode = SQLFetch(hstmt1))
            {
              ::SQLGetData(hstmt1, 1, SQL_C_CHAR,
                nowValue, 50,  & sdODataLength);
     long tempL=atol(nowValue);
     if(tempL>nowId)
      nowId=tempL;
     //end set the now get bigger id.
              //::MessageBox(NULL, szLastName,
              //  " from AddressBookDb ", MB_OK);
            }//end for and get get biggest.
             CString temp;
    temp.Format("now id:%d,ret id:%d",nowId,nowId+1);
    AfxMessageBox(temp);
            ::SQLFreeStmt(hstmt1, SQL_DROP);
          }
          ::SQLDisconnect(hdbc1);
        }
        ::SQLFreeConnect(hdbc1);
      }
      ::SQLFreeEnv(henv1);
    }
 
}

void TestItemListDlg::OntestAtoI()
{   
 HCURSOR hlCursor;
    hlCursor = AfxGetApp()->LoadStandardCursor(IDC_WAIT);
    SetCursor(hlCursor);
 //::SetCursor(HCURSOR);
char num[]="23";
int numInt=atoi(num); 
numInt++;
CString temp;
temp.Format("%d",numInt);
AfxMessageBox(temp);
//char *nn;
//itoa(2,nn,0);
//AfxMessageBox(nn);
}

转载于:https://www.cnblogs.com/abraham/archive/2004/03/21/3812.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值