编译环境:VC++2010学习版
这是coding.net上的两张图。我准备用它山寨一个Winform程序,顺便学习C++/CLI。
这是coding.net上的两张图。我准备用它山寨一个Winform程序,顺便学习C++/CLI。
第一步:将需要的控件拖到Form1上并设置属性以搭好框架。
第二步:
1、添加读写数据的初始化代码(由于VC++2010学习版不支持创建强类型的DataSet,所以我创建弱类型的DataSet并添加4张空表)
#pragma once
using namespace System::Data::OleDb;
using namespace System::Text::RegularExpressions;
using namespace System::Collections::Generic;
using namespace System::Linq;
using namespace System::Threading;
#pragma warning(disable:4244)
//using namespace System::Collections;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
ReadData();
}
private:
static OleDbConnection^ conn = gcnew OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=1.xls;Extended Properties='Excel 8.0;HDR=YES;IMEX=2'");
void ReadData(){
array<String^>^ SQLString0={
"select * from [Sheet1$]",
"select 工号,姓名,时间,RIGHT(房床位,6) as 房床位 from [Sheet1$] where 状态 like '%待起钟%'",
"select 名称 as 服务项目,技师,价格,支付额,支付方式 from [结账A03$] where 列表='项目'",
"select 名称 as 产品,数量,价格,支付额,支付方式 from [结账A03$] where 列表='产品'"
};
dataSet1->Clear();
for(int i=0;i<SQLString0->Length;i++){
OleDbDataAdapter^ da=gcnew OleDbDataAdapter(SQLString0[i],conn);
da->Fill(dataSet1->Tables[i]);
}
conn->Close();
}
void WriteData(){
array<String^>^ SQLString1={
"select * from [结账A03$]",
"update [结账A03$] set 套票='"+textBox8->Text+"' where ID=1",
"update [结账A03$] set 选择套票='"+comboBox1->SelectedItem+"' where ID=1",
"update [结账A03$] set 挂账='"+textBox9->Text+"' where ID=1",
"update [结账A03$] set 选择挂账='"+comboBox2->SelectedItem+"' where ID=1",
"update [结账A03$] set 备注='"+richTextBox1->Text+"' where ID=1"
};
conn->Open();
OleDbCommand^ cmd=gcnew OleDbCommand(SQLString1[0],conn);
cmd->ExecuteNonQuery();
for each(KeyValuePair<String^,int> o in Dic){
cmd->CommandText = "update [结账A03$] set "+o.Key+"='"+o.Value+"' where ID=1";
cmd->ExecuteNonQuery();
}
for(int i=1;i<SQLString1->Length;i++){
cmd->CommandText = SQLString1[i];
cmd->ExecuteNonQuery();
}
conn->Close();
}
2、编辑第一张图的事件
static Dictionary<String^, int>^ Dic;
static Bitmap^ memoryImage;
template<class T>
static int I(T a) { return a == "" ? 0 : Convert::ToInt32(a); }
static int Comp(KeyValuePair<String^, int> o) { return o.Value; }
Void treeView1_AfterSelect(Object^ sender, TreeViewEventArgs^ e) {
tabControl1->SelectedIndex = e->Node->Index + 4 * e->Node->Level;
//获取并添加tabControl页面内容
switch (tabControl1->SelectedIndex) {
case 0:
listView1->Items->Clear();
for each (DataRow^ drow in dataTable1->Rows) {
String^ strState = drow["状态"]->ToString();
ListViewItem^ lvi = gcnew ListViewItem(drow["工号"] + " " + drow["姓名"] + " " + drow["房床位"]->ToString()->Remove(4));
lvi->SubItems->AddRange(gcnew array<String^>(2) { drow["备注"]->ToString(), strState });
if (strState->Contains("上钟")) {
lvi->BackColor = Color::FromArgb(0, 153, 204);
lvi->ForeColor = Color::White;
}
else if (strState->Contains("待起钟")) {
lvi->BackColor = Color::FromArgb(245, 121, 59);
lvi->ForeColor = Color::White;
}
else if (strState->Contains("派工")) {
lvi->BackColor = Color::FromArgb(48, 178, 154);
lvi->ForeColor = Color::White;
}
listView1->Items->Add(lvi);
}
dataGridView1->DataSource = dataTable2;
//下面需据实情重写
dataGridView2->DataSource = dataTable2;
dataGridView3->DataSource = dataTable2;
break;
case 1:
dataGridView4->DataSource = dataTable3;
dataGridView5->DataSource = dataTable4;
break;
default:break;
}
}
Void splitContainer1_Panel2_MouseClick(Object^ sender, MouseEventArgs^ e) {
splitContainer1->Panel1Collapsed = !splitContainer1->Panel1Collapsed;
pictureBox12->Image->RotateFlip(RotateFlipType::Rotate180FlipNone);
}
Void pictureBox234_MouseEnter(Object^ sender, EventArgs^ e) {
if (((PictureBox^)sender)->Name == "pictureBox2") panel2->Visible = true;
((PictureBox^)sender)->BackgroundImage = Image::FromFile(Application::StartupPath + "\\0.png");
}
Void pictureBox234_MouseLeave(Object^ sender, EventArgs^ e) {
if (((PictureBox^)sender)->Name == "pictureBox2") {
if (!panel2->RectangleToScreen(panel2->ClientRectangle).Contains(MousePosition)) {
((PictureBox^)sender)->BackgroundImage = nullptr;
panel2->Visible = false;
}
}else {
((PictureBox^)sender)->BackgroundImage = nullptr;
}
}
Void panel2_MouseLeave(Object^ sender, EventArgs^ e) {
if (!pictureBox2->RectangleToScreen(pictureBox2->ClientRectangle).Contains(MousePosition))
pictureBox234_MouseLeave(pictureBox2, e);
}
Void pictureBox4_Click(Object^ sender, EventArgs^ e) {
Application::Exit();
}
Void pictureBox10_Click(Object^ sender, EventArgs^ e) {
pictureBox10->BackColor = Color::Black;
pictureBox11->BackColor = Color::Gray;
listView1->View = View::Tile;
}
Void pictureBox11_Click(Object^ sender, EventArgs^ e) {
pictureBox11->BackColor = Color::Black;
pictureBox10->BackColor = Color::Gray;
listView1->View = View::Details;
}
3、编辑第二张图的事件
Void panel4_Paint(Object^ sender, PaintEventArgs^ e) {
//textBox值类型检查
Regex^ rx = gcnew Regex("[^0-9]");
for each(Control^ col in panel4->Controls) {
if (col->Name->Contains("textBox") && rx->IsMatch(col->Text)) {
MessageBox::Show("文本框内必须是数字");
col->Text = "";
}
}
//获得主要付款方式
Dic = gcnew Dictionary<String^, int>();
Dic->Add("现金", I(textBox1->Text));
Dic->Add("银行卡", I(textBox2->Text));
Dic->Add("会员卡", I(textBox3->Text));
Dic->Add("代金券", I(textBox4->Text));
Dic->Add("微信", I(textBox5->Text));
Dic->Add("支付宝", I(textBox6->Text));
Dic->Add("免单", I(textBox7->Text));
KeyValuePair<String^, int> KeyofMaxValue = Enumerable::Last(Enumerable::OrderBy(Dic, gcnew Func<KeyValuePair<String^, int>, int>(&Comp)));
if (KeyofMaxValue.Value != 0) dataGridView4->Rows[0]->Cells[4]->Value = KeyofMaxValue.Key;
//获得账单明细
int discount = 10, deposit = 0, orig = I(dataTable3->Compute("sum(支付额)", "")) + I(dataTable4->Compute("sum(支付额)", ""));
int money = orig - discount - deposit;
linkLabel2->Text = L"应收¥" + money + L" 实收¥" + Enumerable::Sum(Dic->Values) + L" 原价¥" + orig + L" 优惠¥" + discount + " 押金" + deposit;
linkLabel2->LinkArea = Windows::Forms::LinkArea(0, 3 + money.ToString()->Length);
}
//Refresh Data
Void pictureBox13_Click(Object^ sender, EventArgs^ e) {
ReadData();
int count = 4;
while (count--) {
pictureBox13->Image->RotateFlip(RotateFlipType::Rotate90FlipNone);
pictureBox13->Refresh();
Thread::Sleep(50);
}
}
//Print
Void button1_Click(Object^ sender, EventArgs^ e) {
Graphics^ mygraphics = this->CreateGraphics();
memoryImage = gcnew Bitmap(tabPage2->Width, tabPage2->Height - 90 * scale.Y, mygraphics);
Graphics^ memoryGraphics = Graphics::FromImage(memoryImage);
memoryGraphics->CopyFromScreen(this->Location.X + 150 * scale.X, this->Location.Y + 73 * scale.Y, 0, 0, tabPage2->Size);
if (printDialog1->ShowDialog() == Windows::Forms::DialogResult::OK) {
if (pageSetupDialog1->ShowDialog() == Windows::Forms::DialogResult::OK) {
printPreviewDialog1->ShowDialog();
};
};
}
Void printDocument1_PrintPage(Object^ sender, Printing::PrintPageEventArgs^ e) {
e->Graphics->DrawImage(memoryImage, 0, 0);
}
//Update Excel
Void button2_Click(Object^ sender, EventArgs^ e) {
WriteData();
}
以下可选
//static SizeF OrigSize;
//static PointF scale = PointF(1.0F, 1.0F);
//Record Form1 size
//Void setTag(Control^ cons) {
// for each (Control^ con in cons->Controls) {
// con->Tag = con->Width + ":" + con->Height + ":" + con->Left + ":" + con->Top + ":" + con->Font->Size + ":" + Convert::ToString(con->Font->Style);
// if (con->Controls->Count > 0) setTag(con);
// }
//}
//Void Form1_Load(Object^ sender, EventArgs^ e) {
// OrigSize = SizeF(this->Width, this->Height);
// setTag(this);
//}
Resize
//Void setControls(PointF scale, Control^ cons) {
// for each (Control^ con in cons->Controls) {
// array<String^>^ mytag = con->Tag->ToString()->Split(gcnew array<wchar_t>{':'});
// con->Width = I(mytag[0]) * scale.X;
// con->Height = I(mytag[1]) * scale.Y;
// con->Left = I(mytag[2]) * scale.X;
// con->Top = I(mytag[3]) * scale.Y;
// con->Font = gcnew Drawing::Font(con->Font->Name, I(mytag[4]) * scale.Y, (FontStyle)Enum::Parse(FontStyle::typeid, mytag[5]));
// if (con->Controls->Count > 0) setControls(scale, con);
// }
// splitContainer1->SplitterDistance = 387 * scale.Y;
//}
//Void Form1_Resize(Object^ sender, EventArgs^ e) {
// scale = PointF(this->Width / OrigSize.Width, this->Height / OrigSize.Height);
// setControls(scale, this);
//}
//Void listView1_SizeChanged(Object^ sender, EventArgs^ e) {
// for each(ColumnHeader^ ch in listView1->Columns) {
// ch->Width = listView1->Width / listView1->Columns->Count;
// }
//}
//Void listView1_ColumnWidthChanging(Object^ sender, ColumnWidthChangingEventArgs^ e) {
// e->Cancel = true;
// e->NewWidth = listView1->Columns[e->ColumnIndex]->Width;
//}
需要的素材(对上图截图)和Excel文档内容:
表:Sheet1
工号 | 姓名 | 时间 | 房床位 | 状态 | 备注 |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1001 | 张鹏 | 20:00 | P111/01 | 待起钟(括钟) | |
1002 | 廖莎 | 20:00 | 空闲 | 晚班 | |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1001 | 张鹏 | 20:00 | P111/01 | 待起钟(14:00-15:00) | |
1000 | 安大鹏 | 20:00 | P111/01 | 派工 | 专科足疗(高级) |
1002 | 廖莎 | 20:00 | 空闲 | 晚班 | |
1002 | 廖莎 | 20:00 | 空闲 | 晚班 | |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1002 | 廖莎 | 20:00 | 空闲 | 晚班 | |
1000 | 安大鹏 | 20:00 | P111/01 | 派工 | 专科足疗(高级) |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1002 | 廖莎 | 20:00 | 空闲 | 晚班 | |
1000 | 安大鹏 | 20:00 | P111/01 | 派工 | 专科足疗(高级) |
1002 | 廖莎 | 20:00 | 空闲 | 晚班 | |
1002 | 廖莎 | 20:00 | 空闲 | 晚班 | |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1001 | 张鹏 | 20:00 | P111/01 | 待起钟(括钟) | |
1002 | 廖莎 | 20:00 | 空闲 | 晚班 | |
1000 | 安大鹏 | 20:00 | P111/01 | 上钟(点钟) | 专科足疗(高级) |
1001 | 张鹏 | 20:00 | P111/01 | 待起钟(14:00-15:00) |
表:结账A03
ID | 列表 | 名称 | 技师 | 价格 | 数量 | 支付额 | 支付方式 | 现金 | 银行卡 | 会员卡 | 代金券 | 微信 | 支付宝 | 免单 | 套票 | 选择套票 | 挂账 | 选择挂账 | 备注 |
1 | 项目 | 基础足浴 | 097 | 100 | 1 | 100 | |||||||||||||
2 | 项目 | 精油SPA | 099 | 90 | 1 | 90 | |||||||||||||
3 | 产品 | 八宝粥 | # | 4.5 | 2 | 9 |
效果: