SDC C-Project CF Review 프로그램
LYW
2021-08-25 03152a241b9463c582b56896f5f5f73717497ab4
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
 
#include "stdafx.h"
#include "StaticGNUPlot.h"
 
#pragma warning (disable:4996)
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
#define PARENTCLASS _T("wgnuplot_parent")
#define TEXTCLASS   _T("wgnuplot_text")
#define GRAPHCLASS  _T("wgnuplot_graph")
 
CStaticGNUPlot::CStaticGNUPlot()
{
    _hWndParent    = NULL;
    _hWndText   = NULL;
    _hWndGraph  = NULL;
}
 
CStaticGNUPlot::~CStaticGNUPlot()
{
    if(_hWndGraph)    ::SendMessage(_hWndGraph, WM_CLOSE, 0, 0);
    if(_hWndText)    ::SendMessage(_hWndText, WM_CLOSE, 0, 0);
    if(_hWndParent) ::SendMessage(_hWndParent, WM_CLOSE, 0, 0);
}
 
BEGIN_MESSAGE_MAP(CStaticGNUPlot, CStatic)
    ON_WM_PAINT()
END_MESSAGE_MAP()
 
BOOL CALLBACK cbGetTextWindow(HWND hWnd, LPARAM  lParam)
{
    CStaticGNUPlot *pThis = (CStaticGNUPlot *)lParam;    
 
    HWND hWndFinded = FindWindowEx(hWnd, NULL, TEXTCLASS, NULL);
    if (hWnd && hWndFinded) {
        pThis->_hWndParent = hWnd;
        pThis->_hWndText   = hWndFinded;
    }
    return TRUE;
}
 
BOOL CALLBACK cbGetGraphWindow(HWND hWnd, LPARAM  lParam)
{
    CStaticGNUPlot *pThis = (CStaticGNUPlot *)lParam;
 
    HWND hWndFinded = FindWindowEx(NULL, hWnd, GRAPHCLASS, NULL);
    if (hWndFinded) {        
        pThis->_hWndGraph = hWndFinded;
    }
    return TRUE;
}
 
void CStaticGNUPlot::init (const char *gnuplotName)
{
    if(_hWndGraph)    ::SendMessage(_hWndGraph, WM_CLOSE, 0, 0);
    if(_hWndText)    ::SendMessage(_hWndText,  WM_CLOSE, 0, 0);
    if(_hWndParent) ::SendMessage(_hWndParent, WM_CLOSE, 0, 0);
 
    _hWndParent    = NULL;
    _hWndText    = NULL;
    _hWndGraph    = NULL;
 
    STARTUPINFO si = {0, }; 
    si.cb = sizeof(si);
    si.wShowWindow = SW_SHOWMINIMIZED;
    si.dwFlags = STARTF_USESHOWWINDOW;
 
    PROCESS_INFORMATION pi = {0, };   
    BOOL bSuccess = CreateProcess(NULL, (char *)gnuplotName, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    if (!bSuccess) return;
 
    // Text Window°¡ »ý¼ºµÉ ¶§±îÁö ±â´Ù¸°´Ù.
    for (int i=0; i<100 && !_hWndText; ++i) {
        EnumThreadWindows(pi.dwThreadId, cbGetTextWindow, (LPARAM)this);        
        Sleep (10);
    }
 
    // ºñ¾îÀִ Graph window »ý¼º
    cmd("clear");
 
    // Graph Window°¡ »ý¼ºµÉ ¶§±îÁö ±â´Ù¸°´Ù.
    for (int i=0; i<100 && !_hWndGraph; ++i) {
        EnumThreadWindows(pi.dwThreadId, cbGetGraphWindow, (LPARAM)this);
        Sleep (10);
    }
 
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
 
    if (_hWndParent) {
        //Parent Window¸¦ ¼û±ä´Ù.
        ::ShowWindow (_hWndParent, SW_HIDE);
    }
    if (_hWndGraph) {
        //Graph Window°¡ Embedding µÇµµ·Ï ½ºÅ¸ÀÏÀ» º¯°æÇϰí Å©±â Á¶Á¤
        ::SetParent(_hWndGraph, this->m_hWnd);
 
        long lStyle = ::GetWindowLong(_hWndGraph, GWL_STYLE);
        lStyle &= ~(WS_BORDER | WS_CAPTION | WS_SIZEBOX );
        ::SetWindowLong(_hWndGraph, GWL_STYLE, lStyle);            
 
        RECT rc;
        GetClientRect(&rc);
 
        ::MoveWindow(_hWndGraph, rc.left, rc.top, rc.right, rc.bottom, TRUE);
        ::InvalidateRect (_hWndGraph, NULL, TRUE);
    }
}
 
void CStaticGNUPlot::cmd(const char *format, ...)
{
    va_list ap;
    char cmd[1024];
 
    va_start(ap, format);    
    vsprintf(cmd, format, ap);
    va_end(ap);
 
    if (_hWndText) {
        for (char *p = cmd; *p; ++p){
            ::PostMessage( _hWndText, WM_CHAR, *p, 1);
        }
        ::PostMessage( _hWndText, WM_CHAR, '\n', 1);
    }
}
 
void CStaticGNUPlot::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    // TODO: Add your message handler code here
    // Do not call CStatic::OnPaint() for painting messages
 
    if (_hWndGraph) {
        ::InvalidateRect (_hWndGraph, NULL, TRUE);
    }
}