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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// SellDlg.cpp: 实现文件
//
#include "common/pch.h"
#include "FloatTrade.h"
#include "SellDlg.h"
#include "afxdialogex.h"
#include "../common/NetworkApi.h"
#include "../common/JsonUtil.h"
#include <thread>
 
 
// SellDlg 对话框
 
IMPLEMENT_DYNAMIC(SellDlg, CDialogEx)
 
void SellDlg::initView(SellDlg* context)
{
    //context->btnSell.SetTextColor(RGB(0, 255, 0));
    //context->btnSell.SetFaceColor(RGB(0, 0, 0), TRUE);
    //btnSell.Invalidate();
    //context->btnClose.SubclassDlgItem(IDC_BUTTON_CLOSE, context);
 
    HBITMAP hBitmap = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_PNG_CLOSE));   //IDB_BITMAP1 为BITMAP资源
    context->btnClose.SetBitmap(hBitmap);
}
 
void SellDlg::requestCurrentPriceInfo(SellDlg* context)
{
    while (TRUE) {
    
        try
        {
            if (context->killed) {
                break;
            }
            string result = NetworkApi::get_l2_deal_price(context->code);
            auto doc = JsonUtil::parseUTF16(result);
            if (context->killed) {
                break;
            }
            if (doc.IsObject()) {
                if (doc.HasMember(L"code") && doc[L"code"].GetInt() == 0) {
                    auto data = doc[L"data"].GetObjectW();
                    if (data.HasMember(L"rate")) {
                        float rate = ((int)(data[L"rate"].GetDouble() * 10000)) / 100.f;
                        if (rate > 0)
                        {
                            context->rateColor = RGB(205, 54, 54);
                        }
                        else if (rate < 0) {
                            context->rateColor = RGB(0, 230, 0);
                        }
                        else {
                            context->rateColor = RGB(0, 0, 0);
                        }
                        std::ostringstream oss;
                        oss << std::fixed << std::setprecision(2) << rate;
                        CString priceRateStr = CString(oss.str().c_str());
                        priceRateStr.Append(L"%");
                        context->labelPriceRate.SetWindowTextW(priceRateStr);
                        context->labelPriceRate.Invalidate();
                    }
                }
                else {
                    context->rateColor = RGB(0, 0, 0);
                    context->labelPriceRate.SetWindowTextW(L"-");
                    context->labelPriceRate.Invalidate();
                }
            }
        }
        catch (...)
        {
 
        }
        Sleep(1000);
    }
 
 
}
 
void SellDlg::SetBtnSellText()
{
    // 设置卖信息
    CString st = L"";
    st.Append(codeName);
    //st.Append(L"-");
    //st.Append(to_wstring(position.total - position.available).c_str());
    //st.Append(L"/");
    //st.Append(to_wstring(position.total).c_str());
    btnSell.SetWindowTextW(st);
}
 
SellDlg::SellDlg(string code, CString name, CodePosition position, SellCallbackFunc callback, CloseCallbackFunc closeCallback, CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_DIALOG_SELL, pParent)
    this->pParent = pParent;
    this->code = code;
    this->codeName = name;
    this->position = position;
    this->sellCallback = callback;
    this->closeCallback = closeCallback;
}
 
SellDlg::~SellDlg()
{
}
 
void SellDlg::SetPositionInfo(int total, int avaiable)
{
    this->position.total = total;
    this->position.available = avaiable;
    SetBtnSellText();
}
 
void SellDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_MFCBUTTON_SELL, btnSell);
    DDX_Control(pDX, IDC_BUTTON_CLOSE, btnClose);
    DDX_Control(pDX, IDC_STATIC_RATE, labelPriceRate);
}
 
 
BEGIN_MESSAGE_MAP(SellDlg, CDialogEx)
    ON_WM_NCHITTEST()
    ON_BN_CLICKED(IDC_BUTTON_CLOSE, &SellDlg::OnBnClickedButtonClose)
    ON_BN_CLICKED(IDC_MFCBUTTON_SELL, &SellDlg::OnBnClickedMfcbuttonSell)
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
 
 
// SellDlg 消息处理程序
 
 
 
LRESULT SellDlg::OnNcHitTest(CPoint point)
{
 
 
    return HTCAPTION;
}
 
 
BOOL SellDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    // 设置对话框背景颜色
    SetBackgroundColor(RGB(0,0,0));
    SetBtnSellText();
    int scale = GetDpiForSystem();
    font.CreateFont(27, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, L"微软雅黑");
 
    // 设置透明度(0透明,255完全不透明)
    ModifyStyleEx(0, WS_EX_LAYERED);
    BYTE opacity = 255; // 透明度值取值范围为0~255
    SetLayeredWindowAttributes(RGB(0, 0, 0), opacity, LWA_ALPHA);
 
    //设置字体
    labelPriceRate.SetFont(&font, FALSE);
 
    killed = FALSE;
 
 
    std::thread t1(initView, this);
    t1.detach();
 
    std::thread t2(requestCurrentPriceInfo, this);
    t2.detach();
 
    return TRUE;
}
 
void SellDlg::OnBnClickedButtonClose()
{
    killed = TRUE;
    this->closeCallback(code, this->pParent);
}
 
 
void SellDlg::OnBnClickedMfcbuttonSell()
{
    this->sellCallback(code, this->pParent);
}
 
 
 
 
 
 
 
HBRUSH SellDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 
    if (nCtlColor == CTLCOLOR_STATIC) {
        int nID = pWnd->GetDlgCtrlID();
        switch (nID) {
        case IDC_STATIC_RATE:
            pDC->SetTextColor(rateColor);
            break;
        }
    }
    return hbr;
}