有趣的API ...... [英] Fun with the API...

查看:76
本文介绍了有趣的API ......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚我感到有点无聊,我碰巧遇到了一些我在VB5时代写过的代码。这段代码,是一个简单的小bb应用程序 - 但是是使用windows

API严格创建的。换句话说,没有形式。只是一个带有子主体的模块。如果您对此有兴趣,可以在此查找代码:

http://www.mtogden.com/~tom/backup/code.html


无论如何,我认为将它转换为VB.NET的
VB.NET可能会很有趣。那么,你走了。一个简单的窗口应用程序 - 没有任何形式:


选项严格开启

选项明确开启


进口系统。 Runtime.InteropServices


公共类窗口

Private Const CLASS_NAME As String =" VB_NET_WINDOW"

Private Const APP_TITLE As String = 来自VB.NET的API窗口


''API常量

私有Const WS_CAPTION As Integer =& HC00000

Private Const WS_MAXIMIZEBOX As Integer =& H10000

Private Const WS_MINIMIZEBOX As Integer =& H20000

Private Const WS_OVERLAPPED As Integer =& H0

Private Const WS_SYSMENU As Integer =& H80000

Private Const WS_THICKFRAME As Integer =& H40000

Private Const WS_OVERLAPPEDWINDOW As Integer = _

(WS_OVERLAPPED或_

WS_CAPTION或_

WS_SYSMENU或_

WS_THICKFRAME或_

WS_MINIMIZEBOX或_

WS_MA XIMIZEBOX)


Private Const CS_HREDRAW As Integer =& H2

Private Const CS_VREDRAW As Integer =& H1

Private Const IDI_APPLICATION As Integer = 32512

Private Const IDC_ARROW As Integer = 32512

Private Const LTGRAY_BRUSH As Integer = 1

Private Const SW_SHOWNORMAL As Integer = 1

Private Const WM_DESTROY As Integer =& H2

Private Const WM_LBUTTONDOWN As Integer =& H201

Private Const WM_RBUTTONDOWN As Integer =& H204

Private Const MB_OK As Integer =& H0


''代表我们窗口程序的实例...

Private委托函数WndProcDelgate _

(ByVal hWnd As IntPtr,_

ByVal Message As Integer,_

ByVal wParam As IntPtr,_

ByVal lParam As IntPtr)作为IntPtr

''API定义类型

< StructLayout(LayoutKind.Sequential,CharSet:= CharSet。自动)> _

私有结构WNDCLASSEX

公共cbSize为整数''WNDCLASSEX结构的大小字节数

公共样式为整数''类样式

Public lpfnWndProc As WndProcDelgate''指向类的指针窗口过程

Public cbClsExtra As Integer''为类分配的额外字节数

Public cbWndExtra As Integer''为窗口分配的额外字节数

公共hInstance作为IntPtr''应用程序实例句柄类

公共hIcon作为IntPtr''处理类的图标

公共hCursor作为IntPtr''处理类光标

公共hbrBackground作为IntPtr''处理类背景画刷

公共lpszMenuName As字符串''类菜单的资源名称

Public lpszClassName As St ring''窗口类的名称

公共hIconSm作为IntPtr''处理类的小图标

结束结构


< StructLayout(LayoutKind.Sequential)GT; _

私人结构POINTAPI

Public x As Integer''X-Coordinate in pixels

Public y As Integer''Y-Coordinate,以像素为单位

结束结构


< StructLayout(LayoutKind.Sequential,CharSet:= CharSet.Auto)> _

私有结构MSG

公共hWnd为IntPtr''关联窗口的窗口句柄

公共消息为整数''消息标识符

Public wParam As IntPtr''其他消息信息

Public lParam As IntPtr''其他消息信息

公共时间As Integer''时间消息已发布

Public pt As POINTAPI''消息发布时的光标位置

结束结构


''API声明语句
私有声明自动功能LoadCursor Lib" user32" _

(ByVal hInstance作为IntPtr,_

ByVal lpCursorName作为IntPtr)作为IntPtr


私有声明自动函数LoadIcon Lib" ; USER32" _

(ByVal hInstance作为IntPtr,_

ByVal lpIconName作为IntPtr)作为IntPtr


私有声明自动函数RegisterClassEx Lib" ; USER32" _

(ByRef pcWndClassEx As WNDCLASSEX)作为整数


私有声明自动函数CreateWindowEx Lib" user32" _

(ByVal dwExStyle As Integer,_

ByVal lpClassName As String,_

ByVal lpWindowName As String,_

ByVal dwStyle As Integer,_

ByVal x As Integer,_

ByVal y As Integer,_

ByVal nWidth As Integer,_

ByVal nHeight As Integer,_

ByVal hWndParent As IntPtr,_

ByVal hMenu作为IntPtr,_

ByVal hInstance作为IntPtr,_

ByVal lpParam作为IntPtr)作为IntPtr


私有声明函数ShowWindow Lib" user32" _

(ByVal hWnd As IntPtr,_

ByVal nCmdShow As Integer)As Boolean


私有声明函数UpdateWindow Lib" USER32" _

(ByVal hWnd As IntPtr)As Boolean


私有声明自动函数GetMessage Lib" user32" _

(ByRef lpMsg作为MSG,_

ByVal hWnd作为IntPtr,_

ByVal wMsgFilterMin作为整数,_

ByVal wMsgFilterMax As Integer)As Boolean


(ByRef lpMsg作为MSG)作为布尔值

私有声明自动函数DispatchMessage Lib" user32" _

(ByRef lpMsg作为MSG)作为IntPtr


私有声明Sub PostQuitMessage Lib" user32" _

(ByVal nExitCode As Integer)


私有声明函数GetStockObject Lib" gdi32" _

(ByVal nIndex As Integer)As IntPtr


私有声明自动功能MessageBox Lib" user32" _

(ByVal hWnd As IntPtr,_

ByVal lpText As String,_

ByVal lpCaption As String,_

ByVal wType as Integer)作为整数


私有声明函数SetFocus Lib" user32" _

(ByVal hWnd As IntPtr)作为IntPtr


(ByVal hWnd As IntPtr,_

ByVal wMsg As Integer,_

ByVal wParam As IntPtr,_

ByVal lParam As IntPtr)作为IntPtr

Public Sub New()

Dim wndClass As WNDCLASSEX

Dim hWnd As IntPtr

昏暗的消息作为MSG

Dim hInstance作为IntPtr = Marshal.GetHINSTANCE(GetType(Window).Module)


使用wndClass

.cbSize = Marshal.SizeOf(wndClass)

.style = CS_HREDRAW或CS_VREDRAW

.lpfnWndProc = New WndProcDelgate(AddressOf WndProc)

.cbClsExtra = 0

.cbWndExtra = 0

.hInstance = hInstance

.hIcon = LoadIcon(hInstance,New IntPtr(IDI_APPLICATION) )

.hIconSm = LoadIcon(hInstance,New IntPtr(IDI_APPLICATION))

.hCursor = LoadCursor(hInstance,New IntPtr(IDC_ARROW))

.hbrBackground = GetStockObject(LTGRAY_BRUSH)

.lpszMenuName = Nothing

.lpszClassName = CLASS_NAME



''注册课程

RegisterClassEx(wndClass)


''创建窗口

hWnd = CreateWindowEx _

(0,_

CLASS_NAME,_

APP_TITLE,_

WS_OVERLAPPEDWINDOW, _

0,_

0,_

640,_

480,_

IntPtr.Zero,_

IntPtr.Zero,_

hInstance,_

IntPtr.Zero)


ShowWindow(hWnd,SW_SHOWNORMAL)

UpdateWindow(hWnd)

SetFocus(hWnd)


Do While GetMessage(消息,IntPtr.Zero,0,0)

TranslateMessage(消息)

DispatchMessage(消息)

循环


结束子


私人功能WndProc(ByVal hWnd为IntPtr,_

ByVal消息为整数,_

ByVal wParam As IntPtr,ByVal lParam As IntPtr)作为IntPtr


选择案例消息

案例WM_LBUTTONDOWN

MessageBox( hWnd,左鼠标按下按钮,APP_TITLE,MB_OK)

返回IntPtr.Zero

案例WM_RBUTTONDOWN

MessageBox(hWnd,鼠标右键按下, APP_TITLE,MB_OK)

返回IntPtr.Zero

案例WM_DESTROY

PostQuitMessage(0)

返回IntPtr.Zero

Case Else

返回DefWindowProc(hWnd,Message,wParam,lParam)

结束选择

结束功能


Public Shared Sub Main()

Dim wnd As New Window

End Sub

End Class


它什么都不做 - 但它有点令人沮丧:)

-

Tom Shelton

MVP [Visual Basic]

Last night I was feeling a little bored and I happend to come across a
bit of code I wrote way back in the VB5 days. This code, was a simple
little windows application - but was created strictly using the windows
API. In other words, no forms. Just a module with a sub main. If your
interested in looking at it, you can find the code here:

http://www.mtogden.com/~tom/backup/code.html

Anyway, I thought it might be kind of interesting to convert this to
VB.NET. So, here you go. A simple windows application - with no forms:

Option Strict On
Option Explicit On

Imports System.Runtime.InteropServices

Public Class Window
Private Const CLASS_NAME As String = "VB_NET_WINDOW"
Private Const APP_TITLE As String = "API Window from VB.NET"

'' API Constants
Private Const WS_CAPTION As Integer = &HC00000
Private Const WS_MAXIMIZEBOX As Integer = &H10000
Private Const WS_MINIMIZEBOX As Integer = &H20000
Private Const WS_OVERLAPPED As Integer = &H0
Private Const WS_SYSMENU As Integer = &H80000
Private Const WS_THICKFRAME As Integer = &H40000
Private Const WS_OVERLAPPEDWINDOW As Integer = _
(WS_OVERLAPPED Or _
WS_CAPTION Or _
WS_SYSMENU Or _
WS_THICKFRAME Or _
WS_MINIMIZEBOX Or _
WS_MAXIMIZEBOX)

Private Const CS_HREDRAW As Integer = &H2
Private Const CS_VREDRAW As Integer = &H1
Private Const IDI_APPLICATION As Integer = 32512
Private Const IDC_ARROW As Integer = 32512
Private Const LTGRAY_BRUSH As Integer = 1
Private Const SW_SHOWNORMAL As Integer = 1
Private Const WM_DESTROY As Integer = &H2
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const MB_OK As Integer = &H0

'' Delegate instance for our window procedure...
Private Delegate Function WndProcDelgate _
(ByVal hWnd As IntPtr, _
ByVal Message As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr

'' API Defined Types
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure WNDCLASSEX
Public cbSize As Integer '' Size in bytes of the WNDCLASSEX structure
Public style As Integer '' Class style
Public lpfnWndProc As WndProcDelgate '' Pointer to the classes Window Procedure
Public cbClsExtra As Integer '' Number of extra bytes to allocate for class
Public cbWndExtra As Integer '' Number of extra bytes to allocate for window
Public hInstance As IntPtr '' Applications instance handle Class
Public hIcon As IntPtr '' Handle to the classes icon
Public hCursor As IntPtr '' Handle to the classes cursor
Public hbrBackground As IntPtr '' Handle to the classes background brush
Public lpszMenuName As String '' Resource name of class menu
Public lpszClassName As String '' Name of the Window Class
Public hIconSm As IntPtr '' Handle to the classes small icon
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure POINTAPI
Public x As Integer '' X-Coordinate in pixels
Public y As Integer '' Y-Coordinate in pixels
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure MSG
Public hWnd As IntPtr '' Window handle of the associated window
Public Message As Integer '' Message identifier
Public wParam As IntPtr '' Additional message info
Public lParam As IntPtr '' Additional message info
Public time As Integer '' Time message was posted
Public pt As POINTAPI '' Cursor position when message was posted
End Structure

'' API Declare Statements
Private Declare Auto Function LoadCursor Lib "user32" _
(ByVal hInstance As IntPtr, _
ByVal lpCursorName As IntPtr) As IntPtr

Private Declare Auto Function LoadIcon Lib "user32" _
(ByVal hInstance As IntPtr, _
ByVal lpIconName As IntPtr) As IntPtr

Private Declare Auto Function RegisterClassEx Lib "user32" _
(ByRef pcWndClassEx As WNDCLASSEX) As Integer

Private Declare Auto Function CreateWindowEx Lib "user32" _
(ByVal dwExStyle As Integer, _
ByVal lpClassName As String, _
ByVal lpWindowName As String, _
ByVal dwStyle As Integer, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hWndParent As IntPtr, _
ByVal hMenu As IntPtr, _
ByVal hInstance As IntPtr, _
ByVal lpParam As IntPtr) As IntPtr

Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal nCmdShow As Integer) As Boolean

Private Declare Function UpdateWindow Lib "user32" _
(ByVal hWnd As IntPtr) As Boolean

Private Declare Auto Function GetMessage Lib "user32" _
(ByRef lpMsg As MSG, _
ByVal hWnd As IntPtr, _
ByVal wMsgFilterMin As Integer, _
ByVal wMsgFilterMax As Integer) As Boolean

Private Declare Function TranslateMessage Lib "user32" _
(ByRef lpMsg As MSG) As Boolean

Private Declare Auto Function DispatchMessage Lib "user32" _
(ByRef lpMsg As MSG) As IntPtr

Private Declare Sub PostQuitMessage Lib "user32" _
(ByVal nExitCode As Integer)

Private Declare Function GetStockObject Lib "gdi32" _
(ByVal nIndex As Integer) As IntPtr

Private Declare Auto Function MessageBox Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As Integer) As Integer

Private Declare Function SetFocus Lib "user32" _
(ByVal hWnd As IntPtr) As IntPtr

Private Declare Auto Function DefWindowProc Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
Public Sub New()
Dim wndClass As WNDCLASSEX
Dim hWnd As IntPtr
Dim message As MSG
Dim hInstance As IntPtr = Marshal.GetHINSTANCE(GetType(Window).Module)

With wndClass
.cbSize = Marshal.SizeOf(wndClass)
.style = CS_HREDRAW Or CS_VREDRAW
.lpfnWndProc = New WndProcDelgate(AddressOf WndProc)
.cbClsExtra = 0
.cbWndExtra = 0
.hInstance = hInstance
.hIcon = LoadIcon(hInstance, New IntPtr(IDI_APPLICATION))
.hIconSm = LoadIcon(hInstance, New IntPtr(IDI_APPLICATION))
.hCursor = LoadCursor(hInstance, New IntPtr(IDC_ARROW))
.hbrBackground = GetStockObject(LTGRAY_BRUSH)
.lpszMenuName = Nothing
.lpszClassName = CLASS_NAME
End With

'' register the class
RegisterClassEx(wndClass)

'' Create the window
hWnd = CreateWindowEx _
(0, _
CLASS_NAME, _
APP_TITLE, _
WS_OVERLAPPEDWINDOW, _
0, _
0, _
640, _
480, _
IntPtr.Zero, _
IntPtr.Zero, _
hInstance, _
IntPtr.Zero)

ShowWindow(hWnd, SW_SHOWNORMAL)
UpdateWindow(hWnd)
SetFocus(hWnd)

Do While GetMessage(message, IntPtr.Zero, 0, 0)
TranslateMessage(message)
DispatchMessage(message)
Loop

End Sub

Private Function WndProc(ByVal hWnd As IntPtr, _
ByVal Message As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

Select Case Message
Case WM_LBUTTONDOWN
MessageBox(hWnd, "Left Mouse Button Pressed", APP_TITLE, MB_OK)
Return IntPtr.Zero
Case WM_RBUTTONDOWN
MessageBox(hWnd, "Right Mouse Button Pressed", APP_TITLE, MB_OK)
Return IntPtr.Zero
Case WM_DESTROY
PostQuitMessage(0)
Return IntPtr.Zero
Case Else
Return DefWindowProc(hWnd, Message, wParam, lParam)
End Select
End Function

Public Shared Sub Main()
Dim wnd As New Window
End Sub
End Class

It doesn''t do anything - but it was sort of ammusing :)
--
Tom Shelton
MVP [Visual Basic]

推荐答案

嘿!这是无处不在的你好世界。 C Windows应用程序! :-)

这正是使我想要删除C并使用VB的代码!

我只是teasin ya :-)很酷的东西Tom。


-Rob Teixeira [MVP]


" Tom Shelton" < to*@mtogden.com>在留言中写道

新闻:Os ************* @ TK2MSFTNGP11.phx.gbl ...
Hey! It''s the ubiquitous "hello world" C Windows App! :-)
This is exactly the code that made me want to drop C and use VB!
I''m just teasin ya :-) Cool stuff Tom.

-Rob Teixeira [MVP]

"Tom Shelton" <to*@mtogden.com> wrote in message
news:Os*************@TK2MSFTNGP11.phx.gbl...
昨晚我感觉到了有点无聊,我碰巧遇到了一些我在VB5时代写过的代码。这段代码是一个简单的小窗口应用程序 - 但是是使用windows
API严格创建的。换句话说,没有形式。只是一个带有子主体的模块。如果您有兴趣查看它,可以在这里找到代码:

http://www.mtogden.com/~tom/backup/code.html

无论如何,我觉得它可能是善良的有趣的是将其转换为
VB.NET。那么,你走了。一个简单的窗口应用程序 - 没有任何形式:

Option Strict On
Option Explicit On

Imports System.Runtime.InteropServices

公开类窗口
Private Const CLASS_NAME As String =" VB_NET_WINDOW"
Private Const APP_TITLE As String =" VB.NET的API窗口"

''API常量 Private Const WS_CAPTION As Integer =& HC00000
Private Const WS_MAXIMIZEBOX As Integer =& H10000
Private Const WS_MINIMIZEBOX As Integer =& H20000
Private Const WS_OVERLAPPED As Integer =& H0 <私有Const WS_SYSMENU作为整数=& H80000
私有Const WS_THICKFRAME作为整数=& H40000
私有Const WS_OVERLAPPEDWINDOW作为整数= _
(WS_OVERLAPPED或_
WS_CAPTION或_
WS_SYSMENU或_
WS_THICKFRAME或_
WS_MINIMIZEBOX或_
WS_MAXIMIZEBOX)

私有Const CS_HREDRAW As Integer = & H2
私有Const CS_VREDRAW As Integer =& H1
私有Const IDI_APPLICATION As Integer = 32512
私有Const IDC_ARROW As Integer = 32512
私有Const LTGRAY_BRUSH As Integer = 1
Private Const SW_SHOWNORMAL As Integer = 1
Private Const WM_DESTROY As Integer =& H2
Private Const WM_LBUTTONDOWN As Integer =& H201
Private Const WM_RBUTTONDOWN As Integer =& H204
Private Const MB_OK As Integer =& H0

''为我们的窗口程序委派实例...
私人代表功能WndProcDelgate _
(ByVal hWnd As IntPtr, _
ByVal消息作为整数,_
ByVal wParam作为IntPtr,_
ByVal lParam作为IntPtr)作为IntPtr
''API定义的类型
< ; StructLayout(LayoutKind.Sequential,CharSet:= CharSet.Auto)> _
私有结构WNDCLASSEX
公共cbSize为整数''
WNDCLASSEX结构的大小字节数公共样式为整数''类样式
公共lpfnWndProc作为WndProcDelgate''指向classes
窗口过程公共cbClsExtra As Integer''为
分配的额外字节数为类公共cbWndExtra As Integer''为
分配的额外字节数公共hInstance为IntPtr''应用程序instance
handle类Public HIcon As IntPtr''处理类
icon公共hCursor As IntPtr''处理类
cursor Public hbrBackground As IntPtr''处理类
background brush public lpszMenuName As String''class
menu Pub的资源名称lic lpszClassName As String''窗口类的名称
公共hIconSm作为IntPtr''处理类
小图标结束结构

< StructLayout(LayoutKind.Sequential)> ; _
私有结构POINTAPI
公共x为整数''X坐标以像素为单位
公共y为整数''Y坐标以像素为单位
结束结构

< StructLayout(LayoutKind.Sequential,CharSet:= CharSet.Auto)> _
私有结构MSG
公共hWnd As IntPtr''相关
窗口的窗口句柄公共消息为整数''消息标识符
公共wParam As IntPtr''附加消息信息<公共lParam As IntPtr''附加消息信息
公共时间为整数''时间消息已发布
公共pt为POINTAPI''消息为
时的光标位置结束结构

''API声明语句
私有声明自动功能LoadCursor Lib" user32" _
(ByVal hInstance作为IntPtr,_
ByVal lpCursorName作为IntPtr)作为IntPtr

私有声明自动功能LoadIcon Lib" user32" _
(ByVal hInstance作为IntPtr,_
ByVal lpIconName作为IntPtr)作为IntPtr
私有声明自动函数RegisterClassEx Lib" user32" _
(ByRef pcWndClassEx As WNDCLASSEX)作为整数

私有声明自动函数CreateWindowEx Lib" user32" _
(ByVal dwExStyle As Integer,_
ByVal lpClassName As String,_
ByVal lpWindowName As String,_
ByVal dwStyle As Integer,_
ByVal x As Integer ,_
ByVal y As Integer,_
ByVal nWidth As Integer,_
ByVal nHeight As Integer,_
ByVal hWndParent As IntPtr,_
ByVal hMenu As IntPtr ,_
ByVal hInstance作为IntPtr,_
ByVal lpParam作为IntPtr)作为IntPtr

私有声明函数ShowWindow Lib" user32" _
(ByVal hWnd As IntPtr,_
ByVal nCmdShow As Integer)As Boolean

私有声明函数UpdateWindow Lib" user32" _
(ByVal hWnd As IntPtr)As Boolean

私有声明自动函数GetMessage Lib" user32" _
(ByRef lpMsg As MSG,_
ByVal hWnd As IntPtr,_
ByVal wMsgFilterMin As Integer,_
ByVal wMsgFilterMax As Integer)As Boolean

私有声明功能TranslateMessage Lib" user32" _
(ByRef lpMsg作为MSG)作为布尔值

私有声明自动功能DispatchMessage Lib" user32" _
(ByRef lpMsg作为MSG)作为IntPtr
私人声明Sub PostQuitMessage Lib" user32" _
(ByVal nExitCode As Integer)

私有声明函数GetStockObject Lib" gdi32" _
(ByVal nIndex As Integer)作为IntPtr
私有声明自动功能MessageBox Lib" user32" _
(ByVal hWnd As IntPtr,_
ByVal lpText As String,_
ByVal lpCaption As String,_
ByVal wType as Integer)As Integer
私有声明功能SetFocus Lib" user32" _
(ByVal hWnd As IntPtr)作为IntPtr
私有声明自动功能DefWindowProc Lib" user32" _
(ByVal hWnd As IntPtr,_
ByVal wMsg As Integer,_
ByVal wParam As IntPtr,_
ByVal lParam As IntPtr)As IntPtr

Public Sub New()
Dim wndClass As WNDCLASSEX
Dim hWnd As IntPtr
Dim message as MSG
Dim hInstance As IntPtr =
Marshal.GetHINSTANCE(GetType(Window) )。模块)
使用wndClass
.cbSize = Marshal.SizeOf(wndClass)
.style = CS_HREDRAW或CS_VREDRAW
.lpfnWndProc = New WndProcDelgate(AddressOf WndProc)
.cbClsExtra = 0
.cbWndExtra = 0
.hInstance = hInstance
.hIcon = LoadIcon(hInstance,New IntPtr(IDI_APPLICATION))
.hIconSm = LoadIcon(hInstance,New IntPtr (IDI_APPLICATION))
.hCursor = LoadCursor(hInstance,New IntPtr(IDC_ARROW))
.hbrBackground = GetStockObject(LTGRAY_B RUSH)
.lpszMenuName = Nothing
.lpszClassName = CLASS_NAME
结束

''注册课程
RegisterClassEx(wndClass)
< br'>''创建窗口
hWnd = CreateWindowEx _
(0,_
CLASS_NAME,_
APP_TITLE,_
WS_OVERLAPPEDWINDOW,_
0 ,_
0,_
640,_
480,_
IntPtr.Zero,_
IntPtr.Zero,_
hInstance,_
IntPtr.Zero)

ShowWindow(hWnd,SW_SHOWNORMAL)
UpdateWindow(hWnd)
SetFocus(hWnd)

Do While GetMessage(消息, IntPtr.Zero,0,0)
TranslateMessage(消息)
DispatchMessage(消息)
循环

结束子

私有函数WndPro c(ByVal hWnd As IntPtr,_
ByVal Message As Integer,_
ByVal wParam As IntPtr,ByVal lParam As IntPtr)As IntPtr

选择案例消息
案例WM_LBUTTONDOWN
MessageBox(hWnd,鼠标左键按下,APP_TITLE,
MB_OK)返回IntPtr.Zero
案例WM_RBUTTONDOWN
MessageBox(hWnd,鼠标右键按下) ,APP_TITLE,
MB_OK)返回IntPtr.Zero
案例WM_DESTROY
PostQuitMessage(0)
返回IntPtr.Zero
Case Else
返回DefWindowProc(hWnd,消息,wParam,lParam)
结束选择
结束功能

Public Shared Sub Main()
Dim wnd As New Window
End Sub 结束课

它没有做任何事 - 但它有点令人困惑:)
-
Tom Shelton
MVP [Visual Basic]
Last night I was feeling a little bored and I happend to come across a
bit of code I wrote way back in the VB5 days. This code, was a simple
little windows application - but was created strictly using the windows
API. In other words, no forms. Just a module with a sub main. If your
interested in looking at it, you can find the code here:

http://www.mtogden.com/~tom/backup/code.html

Anyway, I thought it might be kind of interesting to convert this to
VB.NET. So, here you go. A simple windows application - with no forms:

Option Strict On
Option Explicit On

Imports System.Runtime.InteropServices

Public Class Window
Private Const CLASS_NAME As String = "VB_NET_WINDOW"
Private Const APP_TITLE As String = "API Window from VB.NET"

'' API Constants
Private Const WS_CAPTION As Integer = &HC00000
Private Const WS_MAXIMIZEBOX As Integer = &H10000
Private Const WS_MINIMIZEBOX As Integer = &H20000
Private Const WS_OVERLAPPED As Integer = &H0
Private Const WS_SYSMENU As Integer = &H80000
Private Const WS_THICKFRAME As Integer = &H40000
Private Const WS_OVERLAPPEDWINDOW As Integer = _
(WS_OVERLAPPED Or _
WS_CAPTION Or _
WS_SYSMENU Or _
WS_THICKFRAME Or _
WS_MINIMIZEBOX Or _
WS_MAXIMIZEBOX)

Private Const CS_HREDRAW As Integer = &H2
Private Const CS_VREDRAW As Integer = &H1
Private Const IDI_APPLICATION As Integer = 32512
Private Const IDC_ARROW As Integer = 32512
Private Const LTGRAY_BRUSH As Integer = 1
Private Const SW_SHOWNORMAL As Integer = 1
Private Const WM_DESTROY As Integer = &H2
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const MB_OK As Integer = &H0

'' Delegate instance for our window procedure...
Private Delegate Function WndProcDelgate _
(ByVal hWnd As IntPtr, _
ByVal Message As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr

'' API Defined Types
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure WNDCLASSEX
Public cbSize As Integer '' Size in bytes of the WNDCLASSEX structure Public style As Integer '' Class style
Public lpfnWndProc As WndProcDelgate '' Pointer to the classes Window Procedure Public cbClsExtra As Integer '' Number of extra bytes to allocate for class Public cbWndExtra As Integer '' Number of extra bytes to allocate for window Public hInstance As IntPtr '' Applications instance handle Class Public hIcon As IntPtr '' Handle to the classes icon Public hCursor As IntPtr '' Handle to the classes cursor Public hbrBackground As IntPtr '' Handle to the classes background brush Public lpszMenuName As String '' Resource name of class menu Public lpszClassName As String '' Name of the Window Class
Public hIconSm As IntPtr '' Handle to the classes small icon End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure POINTAPI
Public x As Integer '' X-Coordinate in pixels
Public y As Integer '' Y-Coordinate in pixels
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure MSG
Public hWnd As IntPtr '' Window handle of the associated window Public Message As Integer '' Message identifier
Public wParam As IntPtr '' Additional message info
Public lParam As IntPtr '' Additional message info
Public time As Integer '' Time message was posted
Public pt As POINTAPI '' Cursor position when message was posted End Structure

'' API Declare Statements
Private Declare Auto Function LoadCursor Lib "user32" _
(ByVal hInstance As IntPtr, _
ByVal lpCursorName As IntPtr) As IntPtr

Private Declare Auto Function LoadIcon Lib "user32" _
(ByVal hInstance As IntPtr, _
ByVal lpIconName As IntPtr) As IntPtr

Private Declare Auto Function RegisterClassEx Lib "user32" _
(ByRef pcWndClassEx As WNDCLASSEX) As Integer

Private Declare Auto Function CreateWindowEx Lib "user32" _
(ByVal dwExStyle As Integer, _
ByVal lpClassName As String, _
ByVal lpWindowName As String, _
ByVal dwStyle As Integer, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hWndParent As IntPtr, _
ByVal hMenu As IntPtr, _
ByVal hInstance As IntPtr, _
ByVal lpParam As IntPtr) As IntPtr

Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal nCmdShow As Integer) As Boolean

Private Declare Function UpdateWindow Lib "user32" _
(ByVal hWnd As IntPtr) As Boolean

Private Declare Auto Function GetMessage Lib "user32" _
(ByRef lpMsg As MSG, _
ByVal hWnd As IntPtr, _
ByVal wMsgFilterMin As Integer, _
ByVal wMsgFilterMax As Integer) As Boolean

Private Declare Function TranslateMessage Lib "user32" _
(ByRef lpMsg As MSG) As Boolean

Private Declare Auto Function DispatchMessage Lib "user32" _
(ByRef lpMsg As MSG) As IntPtr

Private Declare Sub PostQuitMessage Lib "user32" _
(ByVal nExitCode As Integer)

Private Declare Function GetStockObject Lib "gdi32" _
(ByVal nIndex As Integer) As IntPtr

Private Declare Auto Function MessageBox Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As Integer) As Integer

Private Declare Function SetFocus Lib "user32" _
(ByVal hWnd As IntPtr) As IntPtr

Private Declare Auto Function DefWindowProc Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
Public Sub New()
Dim wndClass As WNDCLASSEX
Dim hWnd As IntPtr
Dim message As MSG
Dim hInstance As IntPtr = Marshal.GetHINSTANCE(GetType(Window).Module)
With wndClass
.cbSize = Marshal.SizeOf(wndClass)
.style = CS_HREDRAW Or CS_VREDRAW
.lpfnWndProc = New WndProcDelgate(AddressOf WndProc)
.cbClsExtra = 0
.cbWndExtra = 0
.hInstance = hInstance
.hIcon = LoadIcon(hInstance, New IntPtr(IDI_APPLICATION))
.hIconSm = LoadIcon(hInstance, New IntPtr(IDI_APPLICATION))
.hCursor = LoadCursor(hInstance, New IntPtr(IDC_ARROW))
.hbrBackground = GetStockObject(LTGRAY_BRUSH)
.lpszMenuName = Nothing
.lpszClassName = CLASS_NAME
End With

'' register the class
RegisterClassEx(wndClass)

'' Create the window
hWnd = CreateWindowEx _
(0, _
CLASS_NAME, _
APP_TITLE, _
WS_OVERLAPPEDWINDOW, _
0, _
0, _
640, _
480, _
IntPtr.Zero, _
IntPtr.Zero, _
hInstance, _
IntPtr.Zero)

ShowWindow(hWnd, SW_SHOWNORMAL)
UpdateWindow(hWnd)
SetFocus(hWnd)

Do While GetMessage(message, IntPtr.Zero, 0, 0)
TranslateMessage(message)
DispatchMessage(message)
Loop

End Sub

Private Function WndProc(ByVal hWnd As IntPtr, _
ByVal Message As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

Select Case Message
Case WM_LBUTTONDOWN
MessageBox(hWnd, "Left Mouse Button Pressed", APP_TITLE, MB_OK) Return IntPtr.Zero
Case WM_RBUTTONDOWN
MessageBox(hWnd, "Right Mouse Button Pressed", APP_TITLE, MB_OK) Return IntPtr.Zero
Case WM_DESTROY
PostQuitMessage(0)
Return IntPtr.Zero
Case Else
Return DefWindowProc(hWnd, Message, wParam, lParam)
End Select
End Function

Public Shared Sub Main()
Dim wnd As New Window
End Sub
End Class

It doesn''t do anything - but it was sort of ammusing :)
--
Tom Shelton
MVP [Visual Basic]


在文章< #t ************** @ TK2MSFTNGP12.phx.gbl>中,Rob Teixeira [MVP]写道:
In article <#t**************@TK2MSFTNGP12.phx.gbl>, Rob Teixeira [MVP] wrote:
嘿!这是无处不在的你好世界。 C Windows应用程序! :-)
这正是让我想要删除C并使用VB的代码!
我只是teasin ya :-)很酷的东西汤姆。

- Rob Teixeira [MVP]
Hey! It''s the ubiquitous "hello world" C Windows App! :-)
This is exactly the code that made me want to drop C and use VB!
I''m just teasin ya :-) Cool stuff Tom.

-Rob Teixeira [MVP]




哈哈!实际上,这段代码是我写的一些VB5代码的翻译,

是关于在C ++中使用Win32 API的教程的翻译...


当程序员感到厌倦时会怎样做很有趣:)

-

Tom Shelton

MVP [视觉效果基本的]



LOL! In fact, this code is a translation of some VB5 code I wrote that
was a translation of a tutorial on using the Win32 API in C++...

It''s funny what a programmer will do when they''re bored :)
--
Tom Shelton
MVP [Visual Basic]


尊重!


我有同样的VB 5.0应用程序,我写了一次虽然无聊但从未打扰过

将它移植到.NET。


hehe ...当你用.NET结束一个句子时然后是一段时间,看起来就像一个FORTRAN运营商那样只需要



-

Peace&快乐的计算,


Mike Labosh,MCSD

所有者,vbSensei.Com

Escriba coda ergo sum。 - vbSensei
Respect!

I have the same VB 5.0 app that I wrote once while bored, but never bothered
porting it to .NET.

hehe...When you end a sentence with ".NET" and then a period, it looks just
like a FORTRAN operator.

--
Peace & happy computing,

Mike Labosh, MCSD
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei


这篇关于有趣的API ......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆