|
楼主 |
发表于 2018-12-14 17:14
|
显示全部楼层
本帖最后由 amao47kiki2 于 2018-12-14 22:08 编辑
接下来说说我修改后,这个案例里面的关键代码。
如上,是工程窗口。关键的、不影响二维码生成算法的sub,集中在“模块1”和“clsQRCode”。
模块1中的Function ByteArrayToPicture就是生成二维码点阵的。原作者是把这个点阵作为image的方法/属性,赋值给窗体中的image控件。
而我为了生成图片格式,参考了案例一中作者的做法——用把点阵放到剪贴板中,然后粘贴在sheet1中。代码如下:
- Public Function ByteArrayToPicture(ByVal lp As Long, ByVal nWidth As Long, ByVal nHeight As Long, Optional ByVal nLeftPadding As Long, _
- Optional ByVal nTopPadding As Long, Optional ByVal nRightPadding As Long, Optional ByVal nBottomPadding As Long, Optional times As Double = 5) As StdPicture
- 'times: 二维码图形放大倍数,函数缺省值为5 这个5倍基本够用,如果是1倍的话,二维码要变大的话,就容易模糊,5倍比较适中,可以自行调整。
-
- Dim tBMI As BITMAPINFO
- Dim h As Long, hdc As Long, hBmp As Long, wdth As Long, hght As Long
- Dim hbr As Long
- Dim r As RECT
- With tBMI.bmiHeader
- .biSize = 40&
- .biWidth = nWidth
- .biHeight = -nHeight
- .biPlanes = 1
- .biBitCount = 8
- .biSizeImage = nWidth * nHeight
- .biClrUsed = 256
- End With
- tBMI.bmiColors(0) = &HFFFFFF
- tBMI.bmiColors(2) = &H808080
- h = GetDC(0)
- hdc = CreateCompatibleDC(h)
- r.Right = Round((nWidth) * times) + nLeftPadding + nRightPadding
- r.Bottom = Round((nHeight) * times) + nTopPadding + nBottomPadding
- hBmp = CreateCompatibleBitmap(h, r.Right, r.Bottom)
- hbr = CreateSolidBrush(vbWhite)
- hBmp = SelectObject(hdc, hBmp)
- FillRect hdc, r, hbr
- DeleteObject hbr
- StretchDIBits hdc, nLeftPadding, nTopPadding, Round((nWidth) * times), Round((nHeight) * times), 0, 0, nWidth, nHeight, ByVal lp, tBMI, 0, 13369376
- hBmp = SelectObject(hdc, hBmp)
- DeleteDC hdc
- ReleaseDC 0, h
- Set ByteArrayToPicture = BitmapToPicture(hBmp, 1)
-
-
- '以下就是参考案例http://club.excelhome.net/thread-1096940-1-1.html的放到剪贴板的做法。
复制代码
说实话,我是知其然而不知其所以然。应该是调用了api。因此最顶上的这一段是必须的!!!!
' Clipboard Manager Functions
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
|
-
评分
-
3
查看全部评分
-
|