PDA

View Full Version : سوال: ساخت برنامه و اجرا از طریق Main



IamOverlord
یک شنبه 14 فروردین 1390, 19:05 عصر
سلام دوستان،
همون طور که خیلی هاتون می دونید می شه این جوری هم برنامه ساخت، به طوری که هیج فرم و چیز دیگه ای در زمان طراحی به جز یک Module نذاشته باشیم:
Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

Sub main()
' ...
' .
' .
' .
' .
MessageBox x, "Hello World!", "Message Box", 0
' .
' .
' ...
' .
' .
End Sub

حالا سوال من اینه که آیا با این روش می شه کاملا یه برنامه ای رو که با روش های دیگه می نوشتیم نوشت؟ (مثلا یه ماشن حساب ساده) کسی می تونه یه نمونه ی این جوری بذاره یا توضیح بده؟ خیلی ممنون.

returnx
یک شنبه 14 فروردین 1390, 20:51 عصر
اگه تمامي کلاس ها(فرم ها ، دکمه هاو...) رو خودتون تعريف کنيد،فکر کنم بشه...
اما چه کاريه !؟ :متعجب:
خوب ما از زبان هاي ويژوالي استفاده ميکنيم که اين شي ها رو هي درست نکنيم و حجم کد نويسي رو بالا نبريم وگرنه با همون C++ کد مينوشتيم ديگه...:بامزه:

IamOverlord
یک شنبه 14 فروردین 1390, 23:03 عصر
هدفم این بود که بفهمم حقیقت چیه!

این هم یه سورس کد که یه همچین کاری می کنه :

باید 2 تا Label، یه CheckBox، یه TextBox و یه CommandButton بذارید رو فرم.

Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
'The progress bar's Form_Resize() module level variables
Private hProgBar As Long 'hWnd
Private dwPBPosLeft As Long 'static x position when vertical
Private dwPBPosTop As Long 'static y position when horizontal

'SetWindowLong param
Private Const GWL_STYLE As Long = (-16)

'Restricts input in the text box control to digits only
Private Const ES_NUMBER As Long = &H2000
Private Const WS_VISIBLE As Long = &H10000000
Private Const WS_CHILD As Long = &H40000000
Private Const WM_USER = &H400
Private Const PBM_SETRANGE = (WM_USER + 1)
Private Const PBM_SETPOS = (WM_USER + 2)
Private Const PBM_SETSTEP = (WM_USER + 4)
Private Const PBM_STEPIT = (WM_USER + 5)
Private Const PBM_SETRANGE32 = (WM_USER + 6)
Private Const PBM_GETRANGE = (WM_USER + 7)
Private Const PROGRESS_CLASS = "msctls_progress32"
Private Const PBS_VERTICAL = &H4
Private Const ICC_PROGRESS_CLASS = &H20

Private Type tagINITCOMMONCONTROLSEX
dwSize As Long
dwICC As Long
End Type

Private Declare Function InitCommonControlsEx Lib "comctl32.dll" _
(lpInitCtrls As tagINITCOMMONCONTROLSEX) As Boolean

Private Declare Sub InitCommonControls Lib "comctl32.dll" ()

Private 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

Private Declare Function DestroyWindow Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function MoveWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal bRepaint As Long) As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, _
ByVal Length As Long)


Private Sub Form_Load()

Call IsNewComctl32

With Text1
.Text = 25000
.Move 1000, 300, 1000, 285

Call SetWindowLong(.hwnd, _
GWL_STYLE, _
GetWindowLong(Text1.hwnd, GWL_STYLE) _
Or ES_NUMBER)
End With

With Check1
.Caption = "Vertical"
.Value = vbUnchecked
.Move 2200, 300, 1200, 285
End With

With Command1
.Caption = "Create && Run ProgressBar..."
.Cancel = True
.Move 1000, 700, 2500, 375
End With

With Label1
.AutoSize = True
.WordWrap = False
.Caption = ""
.Move 900, 1200
End With

With Label2
.AutoSize = True
.WordWrap = False
.Caption = "n %"
.Move 3700, 780 ', 2500, 375
End With

Me.ScaleMode = vbPixels

End Sub


Private Sub Form_Resize()

'adjust the width & height w/the form
If IsWindow(hProgBar) Then

MoveWindow hProgBar, _
dwPBPosLeft, dwPBPosTop, _
Me.ScaleWidth - dwPBPosLeft, _
Me.ScaleHeight - dwPBPosTop, _
1

'display the progress bar's current size
Label1.Caption = "Width: " & Me.ScaleWidth - dwPBPosLeft & _
" Height: " & Me.ScaleHeight - dwPBPosTop & _
" (pixels)"
Else

Label1.Caption = "(click button to create a progress bar...)"

End If

End Sub


Private Sub Command1_Click()

'Dynamically create a progress bar and place it at the
'bottom (or the right) of the form, do stuff that takes a
'while & show the progress, then destroy the progress bar.

'(The progress bar could be hidden using ShowWindow() instead
'of it being destroyed, but the time it takes to create it is
'negligible & its resources are also freed with this method)
Static bRunning As Boolean 'cancel flag
Dim bIsIE3 As Boolean
Dim dwIterations As Long
Dim dwRange As Long
Dim dwStyle As Long
Dim cnt As Long

On Local Error GoTo progbar_exit

If bRunning Then bRunning = False: Exit Sub

'Set the default progress bar styles
dwStyle = WS_CHILD Or WS_VISIBLE

'if vertical selected, make it so!
If Check1.Value = vbChecked Then
dwStyle = dwStyle Or PBS_VERTICAL
End If

'Create and show the status bar. Additional standard
'or extended window styles can be specified to alter the
'default appearance of the progress bar. The progress
'bar can also be easily created as a child window of a
'status bar part (VB "Panel"). Substitute the status bar's
'hWnd and a part's bounding rectangle (via SB_GETRECT) in
'CreateWindowEx()'s respective params below.
hProgBar = CreateWindowEx(0, PROGRESS_CLASS, _
vbNullString, _
dwStyle, _
0, 0, 0, 0, _
hwnd, 0, _
App.hInstance, _
ByVal 0)

If hProgBar = 0 Then MsgBox "CreateWindowEx failed.": Exit Sub

'Here we go... the progress bar's style can't
'be changed after its been created.
bRunning = True
Command1.Caption = "Stop"
Text1.Enabled = False
Check1.Enabled = False

'Set the progress bar's static x (or y) position
'so its initially 20 pixels wide (or high). The code
'in the form_resize event uses these values to position
'the progress bar
If Check1.Value = vbChecked Then
dwPBPosLeft = Me.ScaleWidth - 20
dwPBPosTop = 0
Else
dwPBPosTop = Me.ScaleHeight - 20
dwPBPosLeft = 0
End If

'MoveWindow() in the Form_Resize event will set the
'progress bar's initial position & size. It will also
'display the prog bar's current size in the label.
Call Form_Resize

'get the number of iterations entered into the textbox
dwIterations = Val(Text1.Text)

'set the range and step interval of the progress bar
dwRange = MAKELPARAM(0, dwIterations)
Call SendMessage(hProgBar, PBM_SETRANGE, 0&, ByVal dwRange)
Call SendMessage(hProgBar, PBM_SETSTEP, ByVal 1, 0&)

'Let's do some stuff...
For cnt = 1 To dwIterations

'call DoEvents to allow the loop to
'respond to a cancel (stop) command
DoEvents
If Not bRunning Then Exit For

'<your app-specific code goes here>

'advance the current position of the progress bar
'by the step increment.
Call SendMessage(hProgBar, PBM_STEPIT, 0&, ByVal 0&)
Label2.Caption = Int((cnt / dwIterations) * 100) & " %"

Next

progbar_exit:

'Free all resources associated with the progress bar.
'If not destroyed here, the progress bar will automatically
'be destroyed when the parent window - the window specified in
'hWndParent of CreateWindowEx() - is destroyed.
If IsWindow(hProgBar) Then Call DestroyWindow(hProgBar)

're-initialize form's controls
bRunning = False
Command1.Caption = "Create && Run ProgressBar..."
Text1.Enabled = True
Check1.Enabled = True
Label2.Caption = "n %"

End Sub


Private Function MAKELPARAM(wLow As Long, wHigh As Long) As Long

'Combines two integers into a long
MAKELPARAM = MAKELONG(wLow, wHigh)

End Function


Private Function MAKELONG(wLow As Long, wHigh As Long) As Long

MAKELONG = LoWord(wLow) Or (&H10000 * LoWord(wHigh))

End Function


Private Function LoWord(dwValue As Long) As Integer

CopyMemory LoWord, dwValue, 2

End Function


Private Function IsNewComctl32() As Boolean

'ensures that the Comctl32.dll library is loaded
Dim icc As tagINITCOMMONCONTROLSEX

On Error GoTo Err_InitOldVersion

icc.dwSize = Len(icc)
icc.dwICC = ICC_PROGRESS_CLASS

'VB will generate error 453 "Specified DLL function not found"
'here if the new version isn't installed
IsNewComctl32 = InitCommonControlsEx(icc)

Exit Function

Err_InitOldVersion:
InitCommonControls

End Function



حالا یه فایل project1.exe.manifest کنار برنامه تون بسازید و این ها رو توش بنویسید:


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="X86"
name="CompanyName.ProductName.YourAppName"
type="win32" />
<description>Your application description here</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*" />
</dependentAssembly>
</dependency>
</assembly>

بعد برنامه تون رو Compile کنید و از روی فایل project1.exe اجراش کنید!

IamOverlord
سه شنبه 11 بهمن 1390, 00:05 صبح
Option Explicit
Private 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
Private Declare Function LoadIcon Lib "user32" Alias "LoadIconA" (ByVal hInstance As Long, ByVal lpIconName As String) As Long
Private Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As String) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function RegisterClassEx Lib "user32" Alias "RegisterClassExA" (pcWndClassEx As WNDCLASSEX) As Integer
Private Declare Function ShowWindow Lib "user32" (ByVal lhwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function UpdateWindow Lib "user32" (ByVal lhwnd As Long) As Long
Private Declare Function SetFocus Lib "user32" (ByVal lhwnd As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal lhwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal lhwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As MSG, ByVal lhwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Private Declare Function TranslateMessage Lib "user32" (lpMsg As MSG) As Long
Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As MSG) As Long
Private Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
Private Declare Function BeginPaint Lib "user32" (ByVal lhwnd As Long, lpPaint As PAINTSTRUCT) As Long
Private Declare Function EndPaint Lib "user32" (ByVal lhwnd As Long, lpPaint As PAINTSTRUCT) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal lhwnd As Long, lpRect As RECT) As Long
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long

Private Type WNDCLASSEX
cbSize As Long
style As Long
lpfnWndProc As Long
cbClsExtra As Long
cbWndExtra As Long
hInstance As Long
hIcon As Long
hCursor As Long
hbrBackground As Long
lpszMenuName As String
lpszClassName As String
hIconSm As Long
End Type

Private Type CREATESTRUCT
lpCreateParams As Long
hInstance As Long
hMenu As Long
hWndParent As Long
cy As Long
cx As Long
y As Long
x As Long
style As Long
lpszName As String
lpszClass As String
ExStyle As Long
End Type

Private Type POINTAPI
x As Long
y As Long
End Type

Private Type MSG
lhwnd As Long
tMessage As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type PAINTSTRUCT
hdc As Long
fErase As Long
rcPaint As RECT
fRestore As Long
fIncUpdate As Long
rgbReserved(32) As Byte 'this was declared incorrectly in VB API viewer
End Type

Private Const WS_VISIBLE As Long = &H10000000
Private Const WS_VSCROLL As Long = &H200000
Private Const WS_TABSTOP As Long = &H10000
Private Const WS_THICKFRAME As Long = &H40000
Private Const WS_MAXIMIZE As Long = &H1000000
Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Const WS_MINIMIZE As Long = &H20000000
Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const WS_SYSMENU As Long = &H80000
Private Const WS_BORDER As Long = &H800000
Private Const WS_CAPTION As Long = &HC00000 ' WS_BORDER Or WS_DLGFRAME
Private Const WS_CHILD As Long = &H40000000
Private Const WS_CHILDWINDOW As Long = (WS_CHILD)
Private Const WS_CLIPCHILDREN As Long = &H2000000
Private Const WS_CLIPSIBLINGS As Long = &H4000000
Private Const WS_DISABLED As Long = &H8000000
Private Const WS_DLGFRAME As Long = &H400000
Private Const WS_EX_ACCEPTFILES As Long = &H10&
Private Const WS_EX_DLGMODALFRAME As Long = &H1&
Private Const WS_EX_NOPARENTNOTIFY As Long = &H4&
Private Const WS_EX_TOPMOST As Long = &H8&
Private Const WS_EX_TRANSPARENT As Long = &H20&
Private Const WS_GROUP As Long = &H20000
Private Const WS_HSCROLL As Long = &H100000
Private Const WS_ICONIC As Long = WS_MINIMIZE
Private Const WS_OVERLAPPED As Long = &H0&
Private Const WS_OVERLAPPEDWINDOW As Long = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
Private Const WS_POPUP As Long = &H80000000
Private Const WS_POPUPWINDOW As Long = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Private Const WS_SIZEBOX As Long = WS_THICKFRAME
Private Const WS_TILED As Long = WS_OVERLAPPED
Private Const WS_TILEDWINDOW As Long = WS_OVERLAPPEDWINDOW
Private Const CW_USEDEFAULT As Long = &H80000000
Private Const CS_HREDRAW As Long = &H2
Private Const CS_VREDRAW As Long = &H1
Private Const IDI_APPLICATION As Long = 32512&
Private Const IDC_ARROW As Long = 32512&
Private Const WHITE_BRUSH As Integer = 0
Private Const BLACK_BRUSH As Integer = 4
Private Const WM_KEYDOWN As Long = &H100
Private Const WM_CLOSE As Long = &H10
Private Const WM_DESTROY As Long = &H2
Private Const WM_PAINT As Long = &HF
Private Const SW_SHOWNORMAL As Long = 1
Private Const DT_CENTER As Long = &H1
Private Const DT_SINGLELINE As Long = &H20
Private Const DT_VCENTER As Long = &H4
Private Const WS_EX_STATICEDGE = &H20000
Private Const SW_NORMAL = 1


'Start running the routine from here
Sub Main()
CreateForm
End Sub


Private Sub CreateForm()
Const CLASSNAME = "Custom_Form"
Const TITLE = "TITLE"

Dim lhwndWindow As Long, lHwndLabel As Long

Dim tCreate As CREATESTRUCT
Dim tWinClass As WNDCLASSEX
Dim tMessage As MSG

'Set up and register window class
tWinClass.cbSize = Len(tWinClass)
tWinClass.style = CS_HREDRAW Or CS_VREDRAW
tWinClass.lpfnWndProc = FunctionPointer(AddressOf WindowProc)
tWinClass.cbClsExtra = 0&
tWinClass.cbWndExtra = 0&
tWinClass.hInstance = App.hInstance
tWinClass.hIcon = LoadIcon(App.hInstance, IDI_APPLICATION)
tWinClass.hCursor = LoadCursor(App.hInstance, IDC_ARROW)
tWinClass.hbrBackground = GetStockObject(WHITE_BRUSH)
tWinClass.lpszMenuName = 0&
tWinClass.lpszClassName = CLASSNAME
tWinClass.hIconSm = LoadIcon(App.hInstance, IDI_APPLICATION)

RegisterClassEx tWinClass


'Create a window
lhwndWindow = CreateWindowEx(0&, CLASSNAME, TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0&, 0&, App.hInstance, 0&)

'Show the window
ShowWindow lhwndWindow, SW_SHOWNORMAL
UpdateWindow lhwndWindow
SetFocus lhwndWindow

'Create a label
lHwndLabel = CreateWindowEx(WS_EX_STATICEDGE Or WS_EX_TRANSPARENT, "STATIC", "Label Created on Window", WS_CHILD, 200, 0, 300, 50, lhwndWindow, 0, App.hInstance, tCreate)
'Show label
ShowWindow lHwndLabel, SW_NORMAL

'Message loop
Do While 0 <> GetMessage(tMessage, 0&, 0&, 0&) 'Retrieve a message from the calling thread’s message queue
TranslateMessage tMessage 'Translate virtual-key messages into character messages (character messages are posted to the calling thread's message queue).
DispatchMessage tMessage 'Dispatch message to window procedure (WindowProc)
Loop

End Sub

'Message handler for this window
Private Function WindowProc(ByVal lhwnd As Long, ByVal tMessage As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim tPaint As PAINTSTRUCT
Dim tRect As RECT
Dim lHdc As Long
Dim sCaption As String

Select Case tMessage
Case WM_PAINT
lHdc = BeginPaint(lhwnd, tPaint)
Call GetClientRect(lhwnd, tRect)
sCaption = "Label Printed on Window"
Call DrawText(lHdc, sCaption, Len(sCaption), tRect, DT_SINGLELINE Or DT_CENTER Or DT_VCENTER)
Call EndPaint(lhwnd, tPaint)
Exit Function

Case WM_KEYDOWN
'Close window when the user presses a key
Call PostMessage(lhwnd, WM_CLOSE, 0, 0)
Exit Function

Case WM_DESTROY
'Fired when the X button is pressed
PostQuitMessage 0&
Exit Function
End Select

'pass all other messages to default window procedure
WindowProc = DefWindowProc(lhwnd, tMessage, wParam, lParam)

End Function

'Returns the value from the AddressOf unary operator.
Function FunctionPointer(ByVal lPtr As Long) As Long
FunctionPointer = lPtr
End Function