admin
8 天以前 6cd92a169cbc0db35042f243a09d976fd3e1445c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// CBuyDlg.cpp: 实现文件
//
 
#include "common/pch.h"
#include "FloatTrade.h"
#include "CBuyDlg.h"
#include "afxdialogex.h"
#include "../common/StringUtil.h"
#include "../common/NetworkApi.h"
#include <regex>
using namespace std;
 
 
// CBuyDlg 对话框
 
IMPLEMENT_DYNAMIC(CBuyDlg, CDialogEx)
 
CBuyDlg::CBuyDlg(CString codew, CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_BUY, pParent)
{
    this->codew = codew;
}
 
CBuyDlg::~CBuyDlg()
{
}
 
void CBuyDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, edit_code, editCode);
    DDX_Control(pDX, edit_money, editMoney);
    DDX_Control(pDX, edit_price, editPrice);
}
 
 
BEGIN_MESSAGE_MAP(CBuyDlg, CDialogEx)
    ON_BN_CLICKED(btn_sure, &CBuyDlg::OnBnClickedsure)
    ON_WM_COPYDATA()
    ON_CBN_SELCHANGE(IDC_COMBO_SELL_FIRST, &CBuyDlg::OnCbnSelchangeComboSellFirst)
    ON_CBN_SELCHANGE(IDC_COMBO_SELL_LEFT, &CBuyDlg::OnCbnSelchangeComboSellLeft)
END_MESSAGE_MAP()
 
 
// CBuyDlg 消息处理程序
 
 
void CBuyDlg::OnBnClickedsure()
{
    //确定
    CString codew;
    CString moneyw;
    CString pricew;
    editCode.GetWindowTextW(codew);
    editMoney.GetWindowTextW(moneyw);
    editPrice.GetWindowTextW(pricew);
    //正则判断整数
    string code = StringUtil::cstring2String(codew);
    string money= StringUtil::cstring2String(moneyw);
    string price = StringUtil::cstring2String(pricew);
    if (!regex_match(code,regex("^\\d{6}$"))) {
        AfxMessageBox(L"代码格式不正确");
        return;
    }
 
    if (!regex_match(money, regex("^\\d{2,7}$"))) {
        AfxMessageBox(L"金额必须为(2-7位)整数");
        return;
    }
 
    if (!regex_match(price, regex("^[0-9]+(.[0-9]{0,2})?$"))) {
        AfxMessageBox(L"价格不正确");
        return;
    }
 
 
    //转换为量
    int volume = round(std::stoi(money) / stof(price));
    if (volume % 100 != 0){
        volume = (volume / 100) * 100;
    }
 
    try {
        NetworkApi::buy(code, volume, 0);
        MessageBox(_T("下单成功"), _T("下单结果"), MB_TASKMODAL);
    }
    catch (CString st) {
        AfxMessageBox(st);
    }
    catch (wstring st) {
        AfxMessageBox(CString( st.c_str()));
    }
 
 
 
 
 
 
 
 
 
 
 
 
}
 
 
BOOL CBuyDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
    BYTE* pBuf = (BYTE*)(pCopyDataStruct->lpData);
    DWORD dwSize = (DWORD)(pCopyDataStruct->cbData);
 
    // 读取数据
    if (pBuf != nullptr && dwSize > 0)
    {
        std::string str(reinterpret_cast<const char*>(pBuf), dwSize);
        wstring wstr = StringUtil::Unescape(str);
        rapidjson::GenericDocument<rapidjson::UTF16<>> root;
        root.Parse(wstr.c_str());
 
        wstring type = root[_T("type")].GetString();
        if (type == _T("set_code")) {
            //this->SetForegroundWindow();
            //this->SetFocus();
            wstring codew = root[_T("data")][_T("code")].GetString();
            editCode.SetWindowTextW(codew.c_str());
        }
    }
    return CDialogEx::OnCopyData(pWnd, pCopyDataStruct);
}
 
 
BOOL CBuyDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    // 初始化
    editCode.SetWindowTextW(codew);
 
    return TRUE;  // return TRUE unless you set the focus to a control
                  // 异常: OCX 属性页应返回 FALSE
}
 
 
void CBuyDlg::OnCbnSelchangeComboSellFirst()
{
    // TODO: 在此添加控件通知处理程序代码
}
 
 
void CBuyDlg::OnCbnSelchangeComboSellLeft()
{
    // TODO: 在此添加控件通知处理程序代码
}