先是VB6
使用方法是创建一个标准exe程序,然后删除form,添加module将代码拷贝进去即可使用
Attribute VB_Name = "Module1"
Option Explicit
Public Const CW_USEDEFAULT As Long = &H80000000
Public Const WS_CHILD As Long = &H40000000
Public Const WS_VISIBLE As Long = &H10000000
Public Const WS_OVERLAPPED As Long = &H0
Public Const WS_CAPTION As Long = &HC00000 ' WS_BORDER Or WS_DLGFRAME
Public Const WS_SYSMENU As Long = &H80000
Public Const WS_THICKFRAME As Long = &H40000
Public Const WS_MINIMIZEBOX As Long = &H20000
Public Const WS_MAXIMIZEBOX As Long = &H10000
Public Const WS_OVERLAPPEDWINDOW As Long = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
Public Const WS_VSCROLL As Long = &H200000
Public Const WS_HSCROLL As Long = &H100000
Public Const WS_EX_CLIENTEDGE As Long = &H200
Public Const WS_EX_ACCEPTFILES As Long = &H10&
Public Const WM_DESTROY As Long = &H2
Public Const WM_CLOSE As Long = &H10
Public Const COLOR_WINDOW As Long = 5
Public Const SW_SHOWNORMAL As Long = &H1
Public Type WNDCLASS
style As Long
lpfnwndproc As Long
cbClsextra As Long
cbWndExtra2 As Long
hInstance As Long
hIcon As Long
hCursor As Long
hbrBackground As Long
lpszMenuName As String
lpszClassName As String
End Type
Public Type POINTAPI
x As Long
y As Long
End Type
Public Type msg
hwnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Public Const gClassName As String = "First Class Name"
Public Const gAppName As String = "SDK"
Public gHwnd As Long
Public gAccTable As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As WNDCLASS) As Long
Public 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
Public Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Public Declare Function TranslateMessage Lib "user32" (lpMsg As msg) As Long
Public Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As msg) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, 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
Public Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Public Declare Function TranslateAccelerator Lib "user32" Alias "TranslateAcceleratorA" (ByVal hwnd As Long, ByVal hAccTable As Long, lpMsg As msg) As Long
Private Function CreateWindows() As Boolean
gHwnd = CreateWindowEx(WS_EX_ACCEPTFILES, gClassName, gAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0&, 0&, GetModuleHandle(vbNullString), ByVal 0&)
If (gHwnd <> 0) Then
Call ShowWindow(gHwnd, SW_SHOWNORMAL)
Call SetForegroundWindow(gHwnd)
Call UpdateWindow(gHwnd)
End If
CreateWindows = (gHwnd <> 0)
End Function
Private Function GetAddress(ByRef lngAddr As Long) As Long
GetAddress = lngAddr
End Function
Private Function RegisterWindowClass() As Boolean
Dim wc As WNDCLASS
With wc
.style = 0&
.lpfnwndproc = GetAddress(AddressOf WndProc)
.hInstance = App.hInstance
.hIcon = 0
.hCursor = 0
.hbrBackground = COLOR_WINDOW
.lpszClassName = gClassName
.lpszMenuName = vbNullString
.cbClsextra = 0&
.cbWndExtra2 = 0&
End With
RegisterWindowClass = RegisterClass(wc) <> 0
End Function
Public Sub main()
Dim wMsg As msg
If RegisterWindowClass = False Then
Exit Sub
End If
If CreateWindows Then
Do While (GetMessage(wMsg, 0&, 0&, 0&) > 0)
If TranslateAccelerator(gHwnd, gAccTable, wMsg) = 0 Then
Call TranslateMessage(wMsg)
Call DispatchMessage(wMsg)
End If
Loop
End If
End Sub
Public Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case WM_CLOSE
Call DestroyWindow(hwnd)
Case WM_DESTROY
Call PostQuitMessage(0&)
Case Else
WndProc = DefWindowProc(hwnd, uMsg, wParam, lParam)
Exit Function
End Select
WndProc = 0
End Function
再接着是PASCAL的
program winSDK;
{$APPTYPE GUI}
uses
System,SysUtils,Windows,Messages;
function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM):integer; stdcall;
begin
case Msg of
WM_DESTROY:
begin
PostQuitMessage(0);
end;
else
result := DefWindowProc( hWnd , Msg , wParam , lParam );
end;
end;
var
msg:TMsg;
wcex:TWndClassEx;
hWin:HWND;
begin
wcex.cbSize := sizeof(TWndClassEx);
wcex.style := CS_HREDRAW or CS_VREDRAW;
wcex.lpfnWndProc := @WndProc;
wcex.hInstance := 0;
wcex.hbrBackground := COLOR_WINDOWFRAME;
wcex.lpszClassName := 'classname';
wcex.hIcon :=0;
wcex.hCursor := 0;
wcex.lpszMenuName := nil;
wcex.hIconSm :=0;
RegisterClassEx( wcex );
hWin := CreateWindow( 'classname' , 'delphi', WS_MINIMIZEBOX or WS_SYSMENU or WS_VISIBLE,integer(CW_USEDEFAULT),integer(CW_USEDEFAULT), 400, 300, 0 , 0, 0, nil);
if hWin <> 0 then
begin
ShowWindow(hWin, SW_SHOW);
UpdateWindow(hWin);
while GetMessage( msg, 0 , 0, 0) do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
end;
end.
FreeBasic
#include once "windows.bi"
declare function WinMain ( byval hInstance as HINSTANCE, byval hPrevInstance as HINSTANCE, byval szCmdLine as string, byval iCmdShow as integer ) as integer
end WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )
'':::::
function WndProc ( byval hWnd as HWND, byval wMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
function = 0
select case( wMsg )
case WM_CREATE
exit function
case WM_PAINT
dim rct as RECT
dim pnt as PAINTSTRUCT
dim hDC as HDC
hDC = BeginPaint( hWnd, @pnt )
GetClientRect( hWnd, @rct )
DrawText( hDC, _
"Hello, World!", _
-1, _
@rct, _
DT_SINGLELINE or DT_CENTER or DT_VCENTER )
EndPaint( hWnd, @pnt )
exit function
case WM_KEYDOWN
if( lobyte( wParam ) = 27 ) then
PostMessage( hWnd, WM_CLOSE, 0, 0 )
end if
case WM_DESTROY
PostQuitMessage( 0 )
exit function
end select
function = DefWindowProc( hWnd, wMsg, wParam, lParam )
end function
'':::::
function WinMain ( byval hInstance as HINSTANCE, byval hPrevInstance as HINSTANCE, byval szCmdLine as string, byval iCmdShow as integer ) as integer
dim wMsg as MSG
dim wcls as WNDCLASS
dim hWnd as HWND
function = 0
with wcls
.style = CS_HREDRAW or CS_VREDRAW
.lpfnWndProc = @WndProc
.cbClsExtra = 0
.cbWndExtra = 0
.hInstance = hInstance
.hIcon = LoadIcon( NULL, IDI_APPLICATION )
.hCursor = LoadCursor( NULL, IDC_ARROW )
.hbrBackground = GetStockObject( WHITE_BRUSH )
.lpszMenuName = NULL
.lpszClassName = @"HelloWin"
end with
if( RegisterClass( @wcls ) = FALSE ) then
MessageBox( null, "Failed to register wcls", "Error", MB_ICONERROR )
exit function
end if
hWnd = CreateWindowEx( 0, _
@"HelloWin", _
"The Hello Program", _
WS_OVERLAPPEDWINDOW, _
CW_USEDEFAULT, _
CW_USEDEFAULT, _
CW_USEDEFAULT, _
CW_USEDEFAULT, _
NULL, _
NULL, _
hInstance, _
NULL )
ShowWindow( hWnd, iCmdShow )
UpdateWindow( hWnd )
while( GetMessage( @wMsg, NULL, 0, 0 ) <> FALSE )
TranslateMessage( @wMsg )
DispatchMessage( @wMsg )
wend
function = wMsg.wParam
end function
MASM32
.386
.model flat,stdcall
option casemap:none
include d:\masm32\include\windows.inc
include d:\masm32\include\user32.inc
includelib d:\masm32\lib\user32.lib ; calls to functions in user32.lib and kernel32.lib
include d:\masm32\include\kernel32.inc
includelib d:\masm32\lib\kernel32.lib
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
.DATA ; initialized data
ClassName db "SimpleWinClass",0 ; the name of our window class
AppName db "Our First Window",0 ; the name of our window
.DATA? ; Uninitialized data
hInstance HINSTANCE ? ; Instance handle of our program
CommandLine LPSTR ?
.CODE ; Here begins our code
start:
invoke GetModuleHandle, NULL ; get the instance handle of our program.
; Under Win32, hmodule==hinstance mov hInstance,eax
mov hInstance,eax
invoke GetCommandLine ; get the command line. You don't have to call this function IF
; your program doesn't process the command line.
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT ; call the main function
invoke ExitProcess, eax ; quit our program. The exit code is returned in eax from WinMain.
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX ; create local variables on stack
LOCAL msg:MSG
LOCAL hwnd:HWND
mov wc.cbSize,SIZEOF WNDCLASSEX ; fill values in members of wc
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInstance
pop wc.hInstance
mov wc.hbrBackground,COLOR_WINDOW+1
mov wc.lpszMenuName,NULL
mov wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,NULL,IDI_APPLICATION
mov wc.hIcon,eax
mov wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
invoke RegisterClassEx, addr wc ; register our window class
invoke CreateWindowEx,NULL,\
ADDR ClassName,\
ADDR AppName,\
WS_OVERLAPPEDWINDOW,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
NULL,\
NULL,\
hInst,\
NULL
mov hwnd,eax
invoke ShowWindow, hwnd,CmdShow ; display our window on desktop
invoke UpdateWindow, hwnd ; refresh the client area
.WHILE TRUE ; Enter message loop
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW
mov eax,msg.wParam ; return exit code in eax
ret
WinMain endp
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
.IF uMsg==WM_DESTROY ; if the user closes our window
invoke PostQuitMessage,NULL ; quit our application
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam ; Default message processing
ret
.ENDIF
xor eax,eax
ret
WndProc endp
end start
C/C++
#include "windows.h"
#pragma comment(lib,"user32.lib")
HINSTANCE hInst; // current instance
TCHAR szTitle[100]; // The title bar text
TCHAR szWindowClass[100]; // The title bar text
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
strcpy( szWindowClass, "FirstWindowClass" );
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
return FALSE;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszClassName = szWindowClass;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[100];
strcpy(szHello, "HelloWorld");
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
C#
using System;
using System.Runtime.InteropServices;
namespace SDK
{
public enum WM
{
DESTROY = 2,
PAINT = 15,
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct PAINTSTRUCT
{
public IntPtr hdc;
public uint fErase;
public RECT rcPaint;
public uint fRestore;
public uint fIncUpdate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=0x20)]
public byte[] rgbReserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct MSG
{
public IntPtr hwnd;
public uint message;
public IntPtr wParam;
public IntPtr lParam;
public IntPtr time;
public POINT pt;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct WNDCLASS
{
public uint style;
public WNDPROC lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public int hInstance;
public int hIcon;
public int hCursor;
public int hbrBackground;
public int lpszMenuName;
public string lpszClassName;
}
public class Gdi32
{
// Methods
[DllImport("gdi32.dll")]
public static extern IntPtr GetStockObject(int i);
}
internal class Kernel32
{
// Methods
[DllImport("Kernel32.dll")]
public static extern IntPtr GetLastError();
}
public class User32
{
// Methods
[DllImport("user32.dll")]
public static extern IntPtr BeginPaint(IntPtr hwnd, out PAINTSTRUCT lpPaint);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr CreateWindowEx(uint dwStyleEx, string lpClassName, string lpWindowName, uint dwStyle, uint x, uint y, uint nWidth, uint nHeight, uint hWndParent, uint hMenu, uint hInstance, uint lpParam);
[DllImport("user32.dll", CharSet=CharSet.Unicode)]
public static extern IntPtr CreateWindowExW(uint dwStyleEx, string lpClassName, string lpWindowName, uint dwStyle, uint x, uint y, uint nWidth, uint nHeight, uint hWndParent, uint hMenu, uint hInstance, uint lpParam);
[DllImport("user32.dll")]
public static extern IntPtr DefWindowProc(IntPtr hwnd, WM msg, IntPtr w, IntPtr l);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr DispatchMessage(ref MSG lpMsg);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr DrawText(IntPtr hDC, string lpString, int nCount, ref RECT lpRect, uint uFormat);
[DllImport("user32.dll")]
public static extern IntPtr EndPaint(IntPtr hwnd, ref PAINTSTRUCT lpPaint);
[DllImport("user32.dll")]
public static extern IntPtr GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
[DllImport("user32.dll")]
public static extern void PostQuitMessage(uint i);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr RegisterClass(ref WNDCLASS WndClass);
[DllImport("user32.dll")]
public static extern IntPtr ShowWindow(IntPtr hwnd, int show);
[DllImport("user32.dll", CharSet=CharSet.Unicode)]
public static extern IntPtr TranslateMessage(ref MSG lpMsg);
[DllImport("user32.dll")]
public static extern IntPtr UpdateWindow(IntPtr hwnd);
}
public delegate IntPtr WNDPROC(IntPtr hwnd, WM msg, IntPtr w, IntPtr l);
public class Program
{
public static void Main(string[] args)
{
WNDCLASS wndclass;
string strClassName = "abcdefg";
wndclass.style = 3;
wndclass.lpfnWndProc = new WNDPROC(Program.WndProc);
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = 0;
wndclass.hIcon = 0;
wndclass.hCursor = 0;
wndclass.hbrBackground = Gdi32.GetStockObject(0).ToInt32();
wndclass.lpszMenuName = 0;
wndclass.lpszClassName = strClassName;
if (User32.RegisterClass(ref wndclass).ToInt32() == 0)
{
Console.WriteLine("RegisterClass失败");
}
else
{
string strWindowText = "C#程序";
IntPtr hWnd = User32.CreateWindowExW(0, strClassName, strWindowText, 0xcf0000, 0x80000000, 0x80000000, 0x80000000, 0x80000000, 0, 0, 0, 0);
if (hWnd.ToInt32() == 0)
{
IntPtr error = Kernel32.GetLastError();
}
else
{
MSG msg;
User32.ShowWindow(hWnd, 5);
User32.UpdateWindow(hWnd);
for (IntPtr ret = User32.GetMessage(out msg, hWnd, 0, 0); ret.ToInt32() != 0; ret = User32.GetMessage(out msg, hWnd, 0, 0))
{
if (-1 == ret.ToInt32())
{
break;
}
User32.TranslateMessage(ref msg);
User32.DispatchMessage(ref msg);
}
Console.WriteLine("end");
}
}
}
public static IntPtr WndProc(IntPtr hwnd, WM msg, IntPtr w, IntPtr l)
{
switch (msg)
{
case WM.DESTROY:
User32.PostQuitMessage(0);
return new IntPtr(0);
case WM.PAINT:
{
PAINTSTRUCT ps;
RECT rect;
IntPtr hDC = User32.BeginPaint(hwnd, out ps);
User32.GetClientRect(hwnd, out rect);
string str = "Hello World";
User32.DrawText(hDC, str, str.Length, ref rect, 0x25);
User32.EndPaint(hwnd, ref ps);
break;
}
}
return User32.DefWindowProc(hwnd, msg, w, l);
}
}
}
发表于 @ 2009年06月12日 20:29:00 | 评论( loading... ) | 举报| 收藏