找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 4901|回复: 3

【VB】VB用纯API设计图形界面窗口程序

[复制链接]
发表于 2014-4-16 16:51:11 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
这个略鬼畜,请看图:
20140416164820.png
看起来好像很正常,但是它是用纯API就像C语言写界面一样设计出来的,而并不是用Form弄出来的界面。
这是VB最蛋疼的写法了。有Form不用,用API创建窗体。
当然对于大家来说,这个可以帮助你们了解VB使用窗口API的方法。
下载地址:
VBAPIWin32.7z (4.91 KB, 下载次数: 22, 售价: 1 个宅币)

本帖被以下淘专辑推荐:

回复

使用道具 举报

 楼主| 发表于 2020-10-6 11:14:06 | 显示全部楼层
  1. Option Explicit '保存为bas文件

  2. 'VB完全可以不这么来的,因为VB本身就是这些API封装好了的语言。
  3. '当然我这样写其实是为了演示Windows程序如何通过API进行窗口创建和消息循环。
  4. '这也证明了VB在这方面的实力。
  5. '从VB6开始,VB的程序不再是解释执行的了。

  6. '结构体定义
  7. Type WNDCLASSEX
  8.     cbSize As Long
  9.     style As Long
  10.     lpfnWndProc As Long
  11.     cbClsExtra As Long
  12.     cbWndExtra As Long
  13.     hInstance As Long
  14.     hIcon As Long
  15.     hCursor As Long
  16.     hbrBackground As Long
  17.     lpszMenuName As String
  18.     lpszClassName As String
  19.     hIconSm As Long
  20. End Type
  21. Type POINTAPI
  22.     X As Long
  23.     Y As Long
  24. End Type
  25. Type Msg
  26.     hWnd As Long
  27.     Message As Long
  28.     wParam As Long
  29.     lParam As Long
  30.     time As Long
  31.     pt As POINTAPI
  32. End Type

  33. 'API声明
  34. Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, lpCursorName As Any) As Long
  35. Declare Function RegisterClassEx Lib "user32" Alias "RegisterClassExA" (pcWndClassEx As WNDCLASSEX) As Long
  36. Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, lpClassName As Any, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
  37. Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
  38. Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
  39. Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
  40. Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long
  41. Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (lpClassName As Any, ByVal hInstance As Long) As Long
  42. Declare Function UpdateWindow Lib "user32" (ByVal hWnd As Long) As Long
  43. Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  44. Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)

  45. '常数定义
  46. Global Const WM_DESTROY = &H2
  47. Global Const IDC_ARROW = 32512
  48. Global Const CW_USEDEFAULT = &H80000000
  49. Global Const WS_CAPTION = &HC00000
  50. Global Const WS_SYSMENU = &H80000
  51. Global Const WS_THICKFRAME = &H40000
  52. Global Const WS_MINIMIZEBOX = &H20000
  53. Global Const WS_MAXIMIZEBOX = &H10000
  54. Global Const WS_VISIBLE = &H10000000
  55. Global Const WS_OVERLAPPEDWINDOW = WS_VISIBLE Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX
  56. Global Const SW_SHOWNORMAL = 1

  57. '定义变量
  58. Global g_WCEx As WNDCLASSEX
  59. Global g_ClassAtom As Long
  60. Global g_hWnd As Long

  61. Sub Main()
  62. With g_WCEx
  63.     .cbSize = LenB(g_WCEx)
  64.     .lpfnWndProc = GetAddressOfFunction(AddressOf WndProc)
  65.     .hInstance = App.hInstance
  66.     .hCursor = LoadCursor(0, ByVal IDC_ARROW) '默认光标
  67.     .hbrBackground = (vbButtonFace And &H7FFFFFFF) + 1 '去掉最高位就是COLOR_BTNFACE了
  68.     .lpszClassName = "VBWIN32APPBYUSINGAPI" '翻译成中文就是“用API的VB的Win32的应用程序”
  69. End With
  70. g_ClassAtom = RegisterClassEx(g_WCEx)
  71. If g_ClassAtom = 0 Then
  72.     MsgBox "注册窗口类失败。", vbExclamation
  73.     Exit Sub
  74. End If
  75. g_hWnd = CreateWindowEx(0, ByVal g_ClassAtom, "VB的API窗口", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 888, 666, 0, 0, App.hInstance, ByVal 0)
  76. If g_hWnd = 0 Then
  77.     MsgBox "创建窗口失败。", vbExclamation
  78.     UnregisterClass ByVal g_ClassAtom, App.hInstance
  79.     Exit Sub
  80. End If
  81. ShowWindow g_hWnd, SW_SHOWNORMAL
  82. UpdateWindow g_hWnd
  83. Dim Message As Msg
  84. Do While GetMessage(Message, 0, 0, 0)
  85.     TranslateMessage Message
  86.     DispatchMessage Message
  87. Loop
  88. UnregisterClass ByVal g_ClassAtom, App.hInstance
  89. End Sub

  90. Function WndProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal WP As Long, ByVal LP As Long) As Long
  91. Select Case wMsg
  92.     Case WM_DESTROY
  93.         PostQuitMessage 0
  94.     Case Else
  95.         WndProc = DefWindowProc(hWnd, wMsg, WP, LP)
  96. End Select
  97. End Function

  98. Function GetAddressOfFunction(ByVal Value As Long) As Long
  99. GetAddressOfFunction = Value
  100. End Function

复制代码
回复 赞! 靠!

使用道具 举报

发表于 2022-5-10 16:40:43 | 显示全部楼层

论坛有你真的精彩~
回复 赞! 靠!

使用道具 举报

发表于 2023-7-6 10:48:08 | 显示全部楼层
占排位
回复

使用道具 举报

本版积分规则

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-12-22 11:03 , Processed in 0.037674 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表