ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何运用VBA在窗体里面画画

如何运用VBA在窗体里面画画

作者:绿色风 分类: 时间:2022-08-17 浏览:164
楼主
xmyjk
Q:如何运用VBA在窗体里面画画?

A:用运用API调用GDI函数咯。
效果如图

 
  1. Option Explicit
  2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  3. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  4. Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
  5. Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long                    '
  6. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  7. Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  8. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

  9. Private Sub CommandButton1_Click()
  10.     Dim Dc As Long, Pen As Long, Color As Long
  11.     Dc = GetDC(FindWindow(vbNullString, UserForm1.Caption)) '获取窗体的上下文设备
  12.     Pen = CreatePen(0, 1, vbRed) '创建红色钢笔,用钢笔画边框
  13.     Color = CreateSolidBrush(vbBlack) '创建黑色画刷,用画填充实体
  14.     SelectObject Dc, Pen '将钢笔放入上下文设备
  15.     SelectObject Dc, Color '将画刷放入上下文设备
  16.     Ellipse Dc, 150 - 100, 150 - 100, 150 + 100, 150 + 100 '开始画画,并设置坐标位置和圆的内径
  17.     DeleteObject Pen '删除钢笔
  18.     DeleteObject Color '删除画刷
  19. End Sub



画圆.zip
2楼
い卋玑┾宝珼
继续 学习API
3楼
老糊涂
学习

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap