#include <windows.h>
#include "qrencode.h"
QRcode* pQRC = NULL;
LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
if (pQRC = QRcode_encodeString("测试二维码", 0, QR_ECLEVEL_H, QR_MODE_8, 1))
{
QRcode_NewBGR24(pQRC,0x00,0x00,0x00);
QRcode_SaveBMP(pQRC,"1.bmp");
char* clsName = "MyApp";
WNDCLASS wc;
ZeroMemory(&wc,sizeof(WNDCLASS));
wc.hInstance = hInstance;
wc.lpszClassName = clsName;
wc.lpfnWndProc = MyWindowProc;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
HWND hMainwind = CreateWindow(clsName,"二维码",WS_OVERLAPPEDWINDOW,
400,300,600,500,NULL,NULL,hInstance,NULL);
if(hMainwind == NULL) return 0;
ShowWindow(hMainwind,SW_NORMAL);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
QRcode_free(pQRC);
return 0;
}
return 1;
}
LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
RECT rect;
GetClientRect(hwnd,&rect);
BYTE *src_buff_ex = new BYTE[pQRC->rgbW*pQRC->rgbW*4];
for(int i=0, j=0; j < pQRC->rgbW*pQRC->rgbW*4; i+=3, j+=4)
{
*(src_buff_ex+j) = *(pQRC->rgbData+i);
*(src_buff_ex+j+1) = *(pQRC->rgbData+i+1);
*(src_buff_ex+j+2) = *(pQRC->rgbData+i+2);
*(src_buff_ex+j+3) = 0;
}
HDC hMemDC_ = CreateCompatibleDC(ps.hdc);
HBITMAP hBitmapSrc_ = CreateCompatibleBitmap(ps.hdc, pQRC->rgbW, pQRC->rgbW);
SelectObject(hMemDC_, hBitmapSrc_);
long err = SetBitmapBits(hBitmapSrc_, pQRC->rgbW*pQRC->rgbW*4, src_buff_ex);
if(err != 0)
{
BitBlt(ps.hdc, (rect.right-pQRC->rgbW)/2, (rect.bottom-pQRC->rgbW)/2,
pQRC->rgbW, pQRC->rgbW, hMemDC_, 0, 0, SRCCOPY);
}
DeleteObject(hBitmapSrc_);
DeleteDC(hMemDC_);
free(src_buff_ex);
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}return 0;
default:break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}