?? dlgstoreadjust3.cpp
字號:
// DlgStoreAdjust3.cpp : implementation file
//
#include "stdafx.h"
#include "aaa.h"
#include "DlgStoreAdjust3.h"
#include "CustomGrid.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern _ConnectionPtr m_pCon;
extern _RecordsetPtr m_pRs;
extern CAaaApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CDlgStoreAdjust3 dialog
CDlgStoreAdjust3::CDlgStoreAdjust3(CWnd* pParent /*=NULL*/)
: CDialog(CDlgStoreAdjust3::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgStoreAdjust3)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CDlgStoreAdjust3::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgStoreAdjust3)
DDX_Control(pDX, IDC_LIST1, m_listinfo);
DDX_Control(pDX, IDC_DETAILLIST, m_list);
DDX_Control(pDX, IDC_COMTOSTORAGE, m_tostorage);
DDX_Control(pDX, IDC_COMFROMSTORAGE, m_fromstorage);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgStoreAdjust3, CDialog)
//{{AFX_MSG_MAP(CDlgStoreAdjust3)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgStoreAdjust3 message handlers
BOOL CDlgStoreAdjust3::OnInitDialog()
{
CDialog::OnInitDialog();
//添加列
m_list.InsertColumn(0,"商品編號",LVCFMT_LEFT,200);
m_list.InsertColumn(1,"商品名稱",LVCFMT_LEFT,180);
m_list.InsertColumn(2,"商品數量",LVCFMT_LEFT,80);
m_listinfo.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_FLATSB);
m_listinfo.InsertColumn(0,"商品編號",LVCFMT_LEFT,80);
m_listinfo.InsertColumn(1,"商品名稱",LVCFMT_LEFT,80);
m_listinfo.InsertColumn(2,"規格",LVCFMT_LEFT,80);
m_listinfo.InsertColumn(4,"助記碼",LVCFMT_LEFT,80);
//添加空行
m_list.InsertItem(0,"");
m_listinfo.SetParent(&m_list);
AddStorage();
return TRUE;
}
BOOL CDlgStoreAdjust3::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message ==WM_KEYDOWN)
{
if ((pMsg->hwnd ==m_listinfo.m_hWnd)&&(pMsg->wParam == 13))
{
OnDblclkList1(NULL,NULL);
return true;
}
/****************************************************************
說明: 表格中編輯框獲得焦點時,按Enter鍵控制單元格焦點移動
****************************************************************/
if ((pMsg->wParam == 13)&&(pMsg->hwnd==m_list.edit.m_hWnd))
{
MoveFocus();
return true;
}
/****************************************************************
說明: 表格中獲得焦點時,按Enter鍵添加新行
****************************************************************/
if ((pMsg->wParam == 13)&&(pMsg->hwnd == m_list.m_hWnd))
{
AddNewRow();
return true;
}
}
/****************************************************************
說明: 表格中編輯框按鍵時的事件,用于判斷是否限制字符輸入,如果用戶按
PageDown鍵,將使輔助錄入表格獲得焦點
****************************************************************/
if ((pMsg->message == WM_KEYDOWN)&&(pMsg->hwnd == m_list.edit.m_hWnd))
{
LimitEdit();
if (pMsg->wParam == 34)
{
ShowListinfo();
}
else if (pMsg->wParam ==VK_DELETE)//按delete鍵刪除表格當前行
DeleteCurRow();
}
/****************************************************************
說明: 表格中編輯框改變時的事件
****************************************************************/
if ((pMsg->message == WM_KEYUP)&&(pMsg->hwnd == m_list.edit.m_hWnd))
{
OnEditChange();
}
/****************************************************************
說明: 在窗體中控件獲得焦點時,使提示列表不可見
****************************************************************/
if (pMsg->message==WM_LBUTTONDOWN)
{
if (pMsg->hwnd!= m_listinfo.m_hWnd)
m_listinfo.ShowWindow(SW_HIDE);
}
/****************************************************************
說明: 在單擊表格時,根據當前列判斷是否顯示編輯框
****************************************************************/
if ((pMsg->message ==WM_LBUTTONDOWN)&&(pMsg->hwnd ==m_list.m_hWnd))
{
//當前列為商品編號,商品名稱為空,顯示編輯框,在當前列為商品名稱,商品編號為空,顯示編輯框,
CString tempID,tempname;
LVHITTESTINFO pos;
pos.pt.x = LOWORD(pMsg->lParam);
pos.pt.y = HIWORD(pMsg->lParam);
pos.flags = LVHT_ABOVE;
int row,col;
row = -1;
col = -1;
if (m_list.SubItemHitTest(&pos)>=0)
{
m_list.SetFocus();
row = pos.iItem;
col = pos.iSubItem;
tempID = m_list.GetItemText(row,0);
tempname = m_list.GetItemText(row,1);
m_list.showedit =true;
if ((tempname.IsEmpty()==false)&&(col == 0))
m_list.showedit =false;
else if((tempID.IsEmpty()==false)&&(col == 1))
m_list.showedit =false;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void CDlgStoreAdjust3::OnEditChange()
{
CString str;
m_list.edit.GetWindowText(str);
CString sql = "";
switch (m_list.col)
{
case 0:
{
if (!str.IsEmpty())
sql.Format("select * from tb_merchandiseinfo where id like '%s%%' or shortname like '%s%%' ",str,str);
break;
}
case 1:
{
if (!str.IsEmpty())
sql.Format("select * from tb_merchandiseinfo where name like '%s%%' or shortname like '%s%%'",str,str);
break;
}
break;
}
if (! sql.IsEmpty())
{
m_pRs->raw_Close();
m_pRs->Open((_variant_t)sql,m_pCon.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
if (m_pRs->RecordCount>0)
{
m_listinfo.DeleteAllItems();
int i=0;
while(! m_pRs->adoEOF)
{
m_listinfo.InsertItem(100,"");
for (int m = 0;m<4;m++)
m_listinfo.SetItemText(i,m,(TCHAR*)(_bstr_t)m_pRs->GetFields()->GetItem((long)m)->Value);
m_pRs->MoveNext();
i +=1;
}
ShowListInfo();
}
else
m_listinfo.ShowWindow(SW_HIDE);
}
else
m_listinfo.ShowWindow(SW_HIDE);
}
void CDlgStoreAdjust3::ShowListInfo()
{
CRect rect;
m_list.GetSubItemRect(m_list.row,m_list.col,LVIR_BOUNDS,rect);
m_listinfo.MoveWindow(rect.left,rect.bottom+1,300,150);
m_listinfo.ShowWindow(SW_SHOW);
}
void CDlgStoreAdjust3::LimitEdit()
{
m_list.edit.IsNumber = false;
//在修改商品數量時只允許輸入數字
if (m_list.col ==2)
{
m_list.edit.IsNumber = true;
}
}
void CDlgStoreAdjust3::ShowListinfo()
{
if (m_listinfo.IsWindowVisible())
{
m_listinfo.SetFocus();
m_listinfo.PostMessage(WM_KEYDOWN,VK_HOME,0);
//listinfo.SetSelectionMark(1);
}
}
void CDlgStoreAdjust3::DeleteCurRow()
{
int currow;
currow = m_list.row;
if (currow!=-1)
{
if (MessageBox("確實要刪除當前行嗎?","提示",MB_YESNO)==IDYES)
{
int m = m_list.GetItemCount();
if (m >1)
{
for (int i = currow+1;i<m_list.GetItemCount();i++)
{
for(int c = 0; c<3 ;c++)
{
m_list.SetItemText(i-1,c,m_list.GetItemText(i,c));
}
}
m_list.DeleteItem(m_list.GetItemCount()-1);
}
else
{
for (int i = 0;i< 3;i++)
{
m_list.SetItemText(0,i,"");
}
}
}
}
}
void CDlgStoreAdjust3::OnOK()
{
//prevent from pressing enter key to close form
}
void CDlgStoreAdjust3::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
int row = m_listinfo.GetSelectionMark();
if (row!=-1)
{
CString temp;
temp = m_listinfo.GetItemText(row,0);
//設置商品編號
m_list.SetItemText(m_list.row,0,temp);
//設置商品名稱
m_list.SetItemText(m_list.row,1,m_listinfo.GetItemText(row,1));
m_listinfo.ShowWindow(SW_HIDE);
}
m_list.col =2;
m_list.ShowEdit();
}
void CDlgStoreAdjust3::AddNewRow()
{
int counts = m_list.GetItemCount();
if (m_list.row == counts-1)//當前行為最后一行
{
m_list.InsertItem(100,"");
m_list.row+=1;
m_list.col = 0;
m_list.ShowEdit();
}
else
{
m_list.row+=1;
m_list.col = 0;
m_list.ShowEdit();
}
}
void CDlgStoreAdjust3::MoveFocus()
{
m_list.DisposeEdit(true);
if (m_list.col <3)
{
m_list.col = m_list.col+1;
CString tempID,tempname;
tempID = m_list.GetItemText(m_list.row,0);
tempname = m_list.GetItemText(m_list.row,1);
m_list.showedit = true;
if ((tempname.IsEmpty()==false)&&(m_list.col == 0))
m_list.showedit =false;
else if((tempID.IsEmpty()==false)&&(m_list.col == 1))
m_list.showedit =false;
m_list.ShowEdit();
}
else
{
AddNewRow();
}
}
void CDlgStoreAdjust3::OnCancel()
{
//prevent from pressing esc key to close form
}
void CDlgStoreAdjust3::AddStorage()
{
m_fromstorage.ResetContent();//刪除所有的數據
m_tostorage.ResetContent();
CString sql;
sql = "select distinct storagename from tb_storageinfo";
m_pRs->Close();
m_pRs->Open((_bstr_t)sql,m_pCon.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
while (!m_pRs->adoEOF)
{
m_tostorage.AddString((TCHAR *)(_bstr_t)m_pRs->GetFields()->GetItem("storagename")->Value);
m_fromstorage.AddString((TCHAR *)(_bstr_t)m_pRs->GetFields()->GetItem("storagename")->Value);
m_pRs->MoveNext();
}
}
//取消操作
void CDlgStoreAdjust3::CancelOperate()
{
m_fromstorage.SetCurSel(0);
m_tostorage.SetCurSel(0);
m_list.DeleteAllItems();
m_list.InsertItem(100,"");
}
//保存操作
void CDlgStoreAdjust3::AddOperator()
{
if (CheckInfo()) //通過檢查
{
CString sql,SQL;
CString c_sum,fromstorage,tostorage,merchandiseid;
float f_sum;//mercandise number
m_fromstorage.GetWindowText(fromstorage);
m_tostorage.GetWindowText(tostorage);
int row = m_list.GetItemCount();
for (int i = 0;i<row;i++)
{
c_sum = m_list.GetItemText(i,2);
f_sum = atof(c_sum);
merchandiseid=m_list.GetItemText(i,0);
sql.Format("update tb_merchandisestorage set Storagenum = Storagenum- %f where merchandiseID = '%s' and storagename = '%s'",f_sum,merchandiseid,fromstorage);
SQL.Format("select * from tb_merchandisestorage where merchandiseID = '%s' and storagename = '%s'",merchandiseid,tostorage);
try
{
m_pRs->raw_Close();
m_pRs->Open((_variant_t)sql,m_pCon.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
m_pRs->raw_Close();
m_pRs->Open((_variant_t)SQL,m_pCon.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
if (m_pRs->RecordCount>0)
{
m_pRs->raw_Close();
SQL.Format("update tb_merchandisestorage set Storagenum = Storagenum+ %f where merchandiseID = '%s' and storagename = '%s' ",f_sum,merchandiseid,tostorage);
m_pRs->Open((_variant_t)SQL,m_pCon.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
}
else
{
m_pRs->raw_Close();
SQL.Format("insert tb_merchandisestorage values('%s','%s',%f,default,default)",merchandiseid,tostorage,f_sum);
m_pRs->Open((_variant_t)SQL,m_pCon.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
}
CancelOperate();
MessageBox("操作成功.","提示",64);
}
catch(_com_error &e)
{
MessageBox(e.ErrorMessage(),"提示",64);
}
}
}
}
//檢查信息
bool CDlgStoreAdjust3::CheckInfo()
{
CString str,fromstorage,tostorage;
m_fromstorage.GetWindowText(fromstorage);
m_tostorage.GetWindowText(tostorage);
if ((fromstorage.IsEmpty())||(tostorage.IsEmpty()))
{
MessageBox("請設置倉庫信息.","提示",64);
return false;
}
if (fromstorage == tostorage)
{
MessageBox("調撥的倉庫信息不能相同.","提示",64);
return false;
}
int row,col;
for (row = 0;row< m_list.GetItemCount();row++)
for(col = 0;col <3;col++)
{
str = m_list.GetItemText(row,col);
if (str.IsEmpty())
{
MessageBox("商品信息列表不能為空.","提示",64);
return false;
}
}
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -