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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// WKButton.cpp : implementation file
//
 
#include "stdafx.h"
//#include "    \ add additional includes here"
#include "WKButton.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
/////////////////////////////////////////////////////////////////////////////
// CWKButton
 
CWKButton::CWKButton()
{
    m_rgbColor1 = RGB(255,255,255);
    m_rgbColor2 = RGB(0,0,0);
    m_rgbText = RGB(0,0,0);
    m_dStyle = WK_BUTTON_COLOR_ONE;
    m_dShape = WK_BUTTON_SHPAE_RECTANGLE;
    m_dButtonStatus = WK_BUTTON_STATUS_MOUSELEAVE;
    m_bValueChange = FALSE;
 
/*    m_logFont.lfHeight = 20;
    m_logFont.lfWidth = 0; 
    m_logFont.lfEscapement = GM_ADVANCED; 
    m_logFont.lfOrientation = 0; 
    m_logFont.lfWeight = FW_DONTCARE; 
    m_logFont.lfItalic = 0; 
    m_logFont.lfUnderline = 0; 
    m_logFont.lfStrikeOut = 0; 
    m_logFont.lfCharSet = DEFAULT_CHARSET; 
    m_logFont.lfOutPrecision = OUT_CHARACTER_PRECIS; 
    m_logFont.lfClipPrecision = CLIP_CHARACTER_PRECIS; 
    m_logFont.lfQuality = DEFAULT_QUALITY; 
    m_logFont.lfPitchAndFamily = FF_DONTCARE;
    strcpy(m_logFont.lfFaceName,"±¼¸²"); */
    
    m_Font.CreatePointFont(100,_T("±¼¸²"));
    m_strCaption = _T("Button");
}
 
CWKButton::~CWKButton()
{
    
}
 
 
BEGIN_MESSAGE_MAP(CWKButton, CButton)
    //{{AFX_MSG_MAP(CWKButton)
    ON_WM_DRAWITEM()
    ON_WM_PAINT()
    ON_WM_CREATE()
    ON_WM_LBUTTONDOWN()
    ON_WM_MOUSEMOVE()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
/////////////////////////////////////////////////////////////////////////////
// CWKButton message handlers
BOOL CWKButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
    m_rtButton = rect;
    m_nWidth = rect.right - rect.left;
    m_nHeight = rect.bottom - rect.top;
    SetButtonText(lpszCaption);
    return CButton::Create(lpszCaption, dwStyle, rect, pParentWnd, nID);
}
 
BOOL CWKButton::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
    // TODO: Add your specialized code here and/or call the base class
    
    m_rtButton = rect;
    m_nWidth = rect.right - rect.left;
    m_nHeight = rect.bottom - rect.top;
    return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
 
void CWKButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
    // TODO: Add your code to draw the specified item
    
}
 
void CWKButton::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
    // TODO: Add your message handler code here and/or call default
    
    CButton::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
 
void CWKButton::PreSubclassWindow() 
{
    // TODO: Add your specialized code here and/or call the base class
    ModifyStyle(0, BS_OWNERDRAW | BS_AUTOCHECKBOX);
    
    CButton::PreSubclassWindow();
}
 
 
void CWKButton::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
 
    // TODO: Add your message handler code here
    if(m_bValueChange)
    {
        CreateButtonImage();
        m_bValueChange = FALSE;
    }
 
    CDC MemDC;
    CRect rt;
    CBitmap* pOldBitmap = NULL;
    GetClientRect(&rt);
    MemDC.CreateCompatibleDC(((CWnd*)(dc.GetWindow()))->GetDC());
 
    switch(m_dButtonStatus)
    {
        case WK_BUTTON_STATUS_MOUSEOVER:
            pOldBitmap = (CBitmap*)MemDC.SelectObject(&m_bmpImage2);
            break;
        case WK_BUTTON_STATUS_MOUSELEAVE:
            pOldBitmap = (CBitmap*)MemDC.SelectObject(&m_bmpImage1);
            break;
        case WK_BUTTON_STATUS_MOUSECLICK:
            pOldBitmap = (CBitmap*)MemDC.SelectObject(&m_bmpImage3);
            break;
    }
    dc.BitBlt(0,0,rt.right,rt.bottom,&MemDC,0,0,SRCCOPY);
    if (pOldBitmap)
        dc.SelectObject(pOldBitmap);
    // Do not call CButton::OnPaint() for painting messages
}
 
int CWKButton::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CButton::OnCreate(lpCreateStruct) == -1)
        return -1;
    
    // TODO: Add your specialized creation code here
    CPaintDC dc(this);
 
    // ¹öưÀÇ DC¿Í È£È¯ µÇ´Â ºñÆ®¸Ê »ý¼º
    m_bmpImage1.CreateCompatibleBitmap(((CWnd*)(dc.GetWindow()))->GetDC(),m_nWidth,m_nHeight);    // Æò»ó½Ã À̹ÌÁö
    m_bmpImage2.CreateCompatibleBitmap(((CWnd*)(dc.GetWindow()))->GetDC(),m_nWidth,m_nHeight);    // ¸¶¿ì½º ¿À¹ö½Ã À̹ÌÁö
    m_bmpImage3.CreateCompatibleBitmap(((CWnd*)(dc.GetWindow()))->GetDC(),m_nWidth,m_nHeight);    // Å¬¸¯½Ã À̹ÌÁö
 
    // »ý¼ºµÈ ºñÆ®¸Ê¿¡ À̹ÌÁö ÀÛ¾÷
    CreateButtonImage();    
 
    return 0;
}
 
BOOL CWKButton::SetButtonStyle(DWORD dShape, DWORD dStyle, COLORREF rgbColor1, COLORREF rgbColor2)
{
    m_dShape = dShape;            // ¸ð¾ç ÀúÀå
    m_dStyle = dStyle;            // Ä÷¯ ½ºÅ¸ÀÏ ÀúÀå
    m_rgbColor1 = rgbColor1;    // Ã¹¹øÂ° »ö ÀúÀå
    m_rgbColor2 = rgbColor2;    // µÎ¹øÂ° »ö ÀúÀå
 
    m_bValueChange = TRUE;
 
    return TRUE;
}
 
BOOL CWKButton::SetButtonStyle(DWORD dShape, COLORREF rgbColor1)
{
    return SetButtonStyle(dShape, WK_BUTTON_COLOR_ONE, rgbColor1, rgbColor1);
}
 
BOOL CWKButton::SetButtonText(CString strCaption, CFont* font, COLORREF rgbText)
{
    // ¹öư Ä¸¼Ç ÀúÀå
    m_strCaption = strCaption;
 
    // ÆùÆ® ÀúÀå
    LOGFONT logFont;
    font->GetLogFont(&logFont);
    m_Font.DeleteObject();
    m_Font.CreateFontIndirect(&logFont);
 
    // ±ÛÀÚ»ö ÀúÀå
    m_rgbText = rgbText;
 
    m_bValueChange = TRUE;
 
    if(this->m_hWnd)
        Invalidate(FALSE);
    return TRUE;
}
 
BOOL CWKButton::SetButtonText(CString strCaption)
{
    SetButtonText(strCaption, &m_Font, m_rgbText);
    return TRUE;
}
 
BOOL CWKButton::SetButtonText(CString strCaption, COLORREF rgbText)
{
    SetButtonText(strCaption, &m_Font, rgbText);
    return TRUE;
}
 
BOOL CWKButton::CreateButtonImage()
{
    switch(m_dShape)    // ¹öư ¸ð¾ç
    {
        case WK_BUTTON_SHPAE_RECTANGLE:    // »ç°¢Çü
            DrawFormRectangle();
            break;
        case WK_BUTTON_SHAPE_CIRCLE:    // ¿ø
            DrawFormRectangle();
            break;
        case WK_BUTTON_SHAPE_ROUNDRECT:    // µÕ±Ù »ç°¢Çü
            DrawFormRectangle();
            break;
    }
    return TRUE;
}
 
BOOL CWKButton::DrawFormRectangle()
{
    switch(m_dStyle)
    {
        case WK_BUTTON_COLOR_ONE:
        case WK_BUTTON_COLOR_TWO:
        default:
            // Æò»ó½Ã À̹ÌÁö »ý¼º
            DrawGradation(&m_bmpImage1, m_rgbColor1, m_rgbColor2);
            DrawBorder(&m_bmpImage1, m_rgbColor2, m_rgbColor1);
            DrawText(&m_bmpImage1,WK_BUTTON_STATUS_MOUSELEAVE);
            // ¸¶¿ì½º ¿À¹ö
            DrawGradation(&m_bmpImage2, LightValueControl(m_rgbColor1,+50), LightValueControl(m_rgbColor2,+50));
            DrawBorder(&m_bmpImage2, LightValueControl(m_rgbColor2,+50), LightValueControl(m_rgbColor1,+50));
            DrawText(&m_bmpImage2,WK_BUTTON_STATUS_MOUSEOVER);
            // Å¬¸¯½Ã
            DrawGradation(&m_bmpImage3, LightValueControl(m_rgbColor1,+20), LightValueControl(m_rgbColor2,+20));
            DrawBorder(&m_bmpImage3, LightValueControl(m_rgbColor1,-50), LightValueControl(m_rgbColor2,-50), 1);
            DrawText(&m_bmpImage3,WK_BUTTON_STATUS_MOUSECLICK);
            break;
    }
    return TRUE;
}
 
BOOL CWKButton::DrawFormCircle()
{
    return TRUE;
}
 
BOOL CWKButton::DrawFormRoundRect()
{
    return TRUE;
}
 
 
void CWKButton::DrawGradation(CBitmap* bmpImage, COLORREF color1, COLORREF color2)
{
    CPaintDC dc(this);
    CDC BufferDC;
    CBitmap* pOldBitmap;
    BufferDC.CreateCompatibleDC(((CWnd*)(dc.GetWindow()))->GetDC());
    
    pOldBitmap = (CBitmap*)BufferDC.SelectObject(bmpImage);
 
    int C1Red, C1Green, C1Blue, C2Red, C2Green, C2Blue, x, y;
    
    // color1ÀÇ »ö»óº° °ªÀ» ºÐ·ù
    C1Red    = GetRValue(color1);
    C1Green    = GetGValue(color1);
    C1Blue    = GetBValue(color1);
 
    // color2ÀÇ »ö»óº° °ªÀ» ºÐ·ù
    C2Red    = GetRValue(color2);
    C2Green    = GetGValue(color2);
    C2Blue    = GetBValue(color2);
 
    float StepRed, StepGreen, StepBlue;
    COLORREF StepRGB;
    
    switch(m_dStyle)
    {
        case WK_BUTTON_COLOR_GRADATION_RIGHT:    // ¿À¸¥ÂÊ ¹æÇâÀ¸·Î
            StepRed        = ((float)C1Red        - (float)C2Red)        /(float)m_nWidth;    // REDÀÇ ´Ü°èº° Áõ°¡°ª ±¸Çϱâ
            StepGreen    = ((float)C1Green    - (float)C2Green)    /(float)m_nWidth;    // GREENÀÇ ´Ü°èº° Áõ°¡°ª ±¸Çϱâ
            StepBlue    = ((float)C1Blue    - (float)C2Blue)    /(float)m_nWidth;    // BLUEÀÇ ´Ü°èº° Áõ°¡°ª ±¸Çϱâ
            for(x = 0; x <= m_nWidth; x++)
            {
                StepRGB = RGB(C1Red - (int)(StepRed * x),
                            C1Green - (int)(StepGreen * x),
                            C1Blue - (int)(StepBlue * x));        // ´Ü°èº° RGB °ª ¸¸µé±â
                for(y = 0; y <= m_nHeight; y++)
                    BufferDC.SetPixel(x, y, StepRGB);            // ´Ü°èº° ¶óÀÎÀ¸·Î Ãâ·Â
            }
            break;
            
        case WK_BUTTON_COLOR_GRADATION_LEFT:    // ¿ÞÂÊ ¹æÇâÀ¸·Î
            StepRed        = ((float)C1Red        - (float)C2Red)        /(float)m_nWidth;
            StepGreen    = ((float)C1Green    - (float)C2Green)    /(float)m_nWidth;
            StepBlue    = ((float)C1Blue    - (float)C2Blue)    /(float)m_nWidth;
            for(x = m_nWidth; x >= 0; x--)
            {
                StepRGB = RGB(C1Red - (int)(StepRed * x),
                            C1Green - (int)(StepGreen * x),
                            C1Blue - (int)(StepBlue * x));
                for(y = 0; y <= m_nHeight; y++)
                    BufferDC.SetPixel(x, y, StepRGB);
            }
            break;
            
        case WK_BUTTON_COLOR_GRADATION_UP:        // À§ÂÊ ¹æÇâÀ¸·Î
            StepRed        = ((float)C1Red        - (float)C2Red)        /(float)m_nHeight;
            StepGreen    = ((float)C1Green    - (float)C2Green)    /(float)m_nHeight;
            StepBlue    = ((float)C1Blue    - (float)C2Blue)    /(float)m_nHeight;
            for(y = 0; y <= m_nHeight; y++)
            {
                StepRGB = RGB(C1Red - (int)(StepRed * y),
                            C1Green - (int)(StepGreen * y),
                            C1Blue - (int)(StepBlue * y));
                for(x = 0; x <= m_nWidth; x++)
                    BufferDC.SetPixel(x, y, StepRGB);
            }
            break;
            
        case WK_BUTTON_COLOR_GRADATION_DOWN:    // ¾Æ·¡ÂÊ ¹æÇâÀ¸·Î
            StepRed        = ((float)C1Red        - (float)C2Red)        /(float)m_nHeight;
            StepGreen    = ((float)C1Green    - (float)C2Green)    /(float)m_nHeight;
            StepBlue    = ((float)C1Blue    - (float)C2Blue)    /(float)m_nHeight;
            for(y = m_nHeight; y >= 0; y--)
            {
                StepRGB = RGB(C1Red - (int)(StepRed * y),
                            C1Green - (int)(StepGreen * y),
                            C1Blue - (int)(StepBlue * y));
                for(x = 0; x <= m_nWidth; x++)
                    BufferDC.SetPixel(x, y, StepRGB);
            }
            break;
    }
    BufferDC.SelectObject(pOldBitmap);
}
 
BOOL CWKButton::DrawBorder(CBitmap *pbmpImage, COLORREF color1, COLORREF color2, int nWidth)
{
    CPaintDC dc(this);
    CDC BufferDC;
    CBitmap* pOldBitmap;
    BufferDC.CreateCompatibleDC(((CWnd*)(dc.GetWindow()))->GetDC());
    
    pOldBitmap = (CBitmap*)BufferDC.SelectObject(pbmpImage);
 
    int i, j;
    for(i = 0; i < nWidth; i++)
        for(j = i; j <= (m_nWidth - i); j++)
        {
            BufferDC.SetPixel(j, i, color1);
            BufferDC.SetPixel(j, m_nHeight - i - 1, color2);
        }
 
    for(i = 0; i < nWidth; i++)
    {
        for(j = i; j <= (m_nHeight - i - 1); j++)
        {
            BufferDC.SetPixel(i, j, color1);
            BufferDC.SetPixel(m_nWidth - i - 1, j, color2);
        }
    }
    BufferDC.SelectObject(pOldBitmap);
 
    return TRUE;
}
 
BOOL CWKButton::DrawText(CBitmap* pBitmap, int nStatus)
{
    CPaintDC dc(this);
    CDC BufferDC;
    CBitmap* pOldBitmap;
    CRect rt;
    GetClientRect(&rt);
    BufferDC.CreateCompatibleDC(((CWnd*)(dc.GetWindow()))->GetDC());
    
    pOldBitmap = (CBitmap*)BufferDC.SelectObject(pBitmap);
 
    CFont *pOldFont;
    pOldFont = (CFont*)BufferDC.SelectObject(&m_Font);
    BufferDC.SetBkMode(TRANSPARENT);
 
    if(nStatus == WK_BUTTON_STATUS_MOUSECLICK)
    {    // Å¬¸¯½Ã Æò»ó½Ãº¸´Ù ÇÑÇȼ¿ ¹Ø¿¡ ±Û¾¾¸¦ Ãâ·ÂÇÑ´Ù.
        rt.left++;
        rt.top++;
        BufferDC.DrawText(m_strCaption, &rt, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
    }
    else
    {
        BufferDC.DrawText(m_strCaption, &rt, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
    }
    BufferDC.SelectObject(pOldFont);
    BufferDC.SelectObject(pOldBitmap);
    
    return TRUE;
}
 
COLORREF CWKButton::LightValueControl(COLORREF rgb, int nValue)
{
    int r,g,b;
    // R,G,B °ªÀ» ºÐ¸®ÈÄ nValue¸¸Å­ Áõ°¡½ÃŲ´Ù.
    b = GetBValue(rgb)+nValue;
    g = GetGValue(rgb)+nValue;
    r = GetRValue(rgb)+nValue;
 
    if(b<0)
        b = 0;
    if(b>255)
        b = 255;
    
    if(g<0)
        g = 0;
    if(g>255)
        g = 255;
    
    if(r<0)
        r = 0;
    if(r>255)
        r = 255;
 
    return RGB(r,g,b);
}
 
void CWKButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    m_dButtonStatus = WK_BUTTON_STATUS_MOUSECLICK;
    Invalidate(FALSE);
    CButton::OnLButtonDown(nFlags, point);
}
 
void CWKButton::OnMouseMove(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    if(nFlags != MK_LBUTTON &&                            
       nFlags != MK_RBUTTON &&
       m_dButtonStatus != WK_BUTTON_STATUS_MOUSEOVER)    
    {// ¸¶¿ì½º ¹öư ÀÔ·ÂÀÌ ¾ø´Â »óÅ¿¡¼­ ¸¶¿ì½º Ä¿¼­°¡ ¹öưÀ§¸¦ Áö³ª°Å³ª »ç¶óÁú °æ¿ì À̺¥Æ®¸¦ ¹ß»ý½ÃŲ´Ù.
        TRACKMOUSEEVENT track = {sizeof(TRACKMOUSEEVENT),TME_HOVER | TME_LEAVE,m_hWnd,10};
        _TrackMouseEvent(&track);
    }
    
    CButton::OnMouseMove(nFlags, point);
}
 
BOOL CWKButton::PreTranslateMessage(MSG* pMsg) 
{
    // TODO: Add your specialized code here and/or call the base class    
    switch(pMsg->message)
    {
    case WM_MOUSELEAVE:    m_dButtonStatus = WK_BUTTON_STATUS_MOUSELEAVE;
                        Invalidate(FALSE);
                        break;
 
    case WM_MOUSEHOVER:    m_dButtonStatus = WK_BUTTON_STATUS_MOUSEOVER;
                        Invalidate(FALSE);
                        break;
    }
 
    return CButton::PreTranslateMessage(pMsg);
}
 
 
BOOL CWKButton::DestroyWindow() 
{
    // TODO: Add your specialized code here and/or call the base class
//    CPaintDC dc(this);
    m_bmpImage1.DeleteTempMap();
    m_bmpImage2.DeleteTempMap();
    m_bmpImage3.DeleteTempMap();
 
    m_Font.DeleteObject();
 
    return CButton::DestroyWindow();
}