forked from kenjinote/QR-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
150 lines (146 loc) · 3.95 KB
/
Source.cpp
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
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#include <windows.h>
#include "QR_Encode.h"
VOID DrawQR(LPCSTR lpszSourceText, HBITMAP *phBitmap, int *pnSymbleSize)
{
CQR_Encode* pQR_Encode = new CQR_Encode;
if (pQR_Encode)
{
if (pQR_Encode->EncodeData(1, 0, TRUE, -1, lpszSourceText))
{
HDC hMemDC = CreateCompatibleDC(NULL);
if (hMemDC)
{
if (*phBitmap == NULL || DeleteObject(*phBitmap))
{
*phBitmap = CreateBitmap(pQR_Encode->m_nSymbleSize, pQR_Encode->m_nSymbleSize, 1, 1, NULL);
if (*phBitmap)
{
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, *phBitmap);
*pnSymbleSize = pQR_Encode->m_nSymbleSize;
PatBlt(hMemDC, 0, 0, *pnSymbleSize, *pnSymbleSize, WHITENESS);
for (int i = 0; i < *pnSymbleSize; ++i)
{
for (int j = 0; j < *pnSymbleSize; ++j)
{
if (pQR_Encode->m_byModuleData[i][j])
{
SetPixel(hMemDC, i + QR_MARGIN, j + QR_MARGIN, RGB(0, 0, 0));
}
}
}
SelectObject(hMemDC, hOldBitmap);
}
}
DeleteDC(hMemDC);
}
}
delete pQR_Encode;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static BOOL bIsEmpty = TRUE;
static HFONT hFont;
static HWND hEdit;
static HBITMAP hBitmap;
static int nSymbleSize;
switch (msg)
{
case WM_CREATE:
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), 0, WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0, 0, 0, 0, hWnd, NULL, ((LPCREATESTRUCT)(lParam))->hInstance, NULL);
SendMessage(hEdit, EM_LIMITTEXT, 0, 0);
break;
case WM_COMMAND:
if (HIWORD(wParam) == EN_CHANGE)
{
const int nTextLength = GetWindowTextLengthA(hEdit);
if (nTextLength == 0)
{
bIsEmpty = TRUE;
}
else
{
bIsEmpty = FALSE;
LPSTR lpszText = (LPSTR)GlobalAlloc(GMEM_FIXED, nTextLength + 1);
GetWindowTextA(hEdit, lpszText, nTextLength + 1);
DrawQR(lpszText, &hBitmap, &nSymbleSize);
GlobalFree(lpszText);
}
InvalidateRect(hWnd, NULL, bIsEmpty);
}
break;
case WM_SETFOCUS:
SetFocus(hEdit);
SendMessage(hEdit, EM_SETSEL, 0, -1);
break;
case WM_SIZE:
MoveWindow(hEdit, 5, 5, LOWORD(lParam) - HIWORD(lParam) - 5, HIWORD(lParam) - 10, TRUE);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
const HDC hdc = BeginPaint(hWnd, &ps);
const HDC hMemDC = CreateCompatibleDC(hdc);
const HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
RECT rect;
GetClientRect(hWnd, &rect);
if (!bIsEmpty)
StretchBlt(hdc, rect.right - rect.bottom + 5, 5, rect.bottom - 10, rect.bottom - 10, hMemDC, 0, 0, nSymbleSize, nSymbleSize, SRCCOPY);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
DeleteObject(hBitmap);
DeleteObject(hFont);
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd, msg, wParam, lParam));
}
return 0;
}
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hPreInst, LPSTR pCmdLine, int nCmdShow)
{
TCHAR szClassName[] = TEXT("QRCODE");
WNDCLASS wndclass = { 0 };
if (!hPreInst)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.hInstance = hinst;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
wndclass.lpszClassName = szClassName;
if (!RegisterClass(&wndclass))
{
return 0;
}
}
const DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
RECT rect;
SetRect(&rect, 0, 0, 800, 400);
AdjustWindowRect(&rect, dwStyle, 0);
HWND hWnd = CreateWindow(szClassName,
TEXT("QR CODE"),
dwStyle,
CW_USEDEFAULT,
0,
rect.right - rect.left,
rect.bottom - rect.top,
0,
0,
hinst,
0);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}