Private Declare Function CreateThread Lib ""kernel32"" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long)As Long
Private Declare Function CreateThread Lib ""kernel32"" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Private Declare Function ResumeThread Lib ""kernel32"" (ByVal hThread As Long) As Long
Private Declare Function SuspendThread Lib ""kernel32"" (ByVal hThread As Long) As Long
Private Declare Function TerminateThread Lib ""kernel32"" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Public Sub Initialize(ByVal lpfnBasFunc As Long) '初始化執行緒
Dim lStackSize As Long, lCreationFlags As Long, lpThreadId As Long, lNull As Long
Option Explicit
Public Type SYSTEM_INFO
dwOemID As Long
dwPageSize As Long
lpMinimumApplicationAddress As Long
lpMaximumApplicationAddress As Long
dwActiveProcessorMask As Long
dwNumberOrfProcessors As Long
dwProcessorType As Long
dwAllocationGranularity As Long
dwReserved As Long
End Type
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SYNCHRONIZE = &H100000
Public Const SPECIFIC_RIGHTS_ALL = &HFFFF
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
Public Const PROCESS_VM_OPERATION = &H8&
Public Const PROCESS_VM_READ = &H10&
Public Const PROCESS_VM_WRITE = &H20&
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'----------------------------------------------------------------------------------------------
'以下程式在Form中
Option Explicit
Dim hProcess As Long
Dim hProcessID As Long
Dim hThreadID As Long
Dim hWndOfApp As Long
Dim hSysInfo As SYSTEM_INFO
Dim lBassAddr As Long
Private Sub Command1_Click()
List2.Clear
Dim s() As Byte, n As Long, i As Long
lBassAddr = Text2.Text
n = Text3.Text
ReDim s(n - 1)
hWndOfApp = FindWindow(vbNullString, Text1.Text)
If hWndOfApp = 0 Then
MsgBox "無法找到該視窗"
Exit Sub
End If
hThreadID = GetWindowThreadProcessId(hWndOfApp, hProcessID)
If hProcessID = 0 Then
MsgBox "無法取得ProcessID"
Exit Sub
End If
If hProcess = 0 Then
MsgBox "無法開啟該Process"
Exit Sub
End If
ReadProcessMemory hProcess, ByVal lBassAddr, s(0), n, ByVal 0&
For i = 0 To n - 1
List2.AddItem "位址:" & (i + lBassAddr) & "= " & s(i)
Next
CloseHandle hProcess
End Sub
Private Sub Command2_Click()
Dim s() As Byte, n As Long, i As Long
Dim Data1 As Byte, Data2 As Integer, Data4 As Long
lBassAddr = Text4.Text
If Option1(0).Value Then
n = 1
ReDim s(0)
Data1 = Text5.Text
CopyMemory s(0), Data1, n
ElseIf Option1(1).Value Then
n = 2
ReDim s(0 To 1)
Data2 = Text5.Text
CopyMemory s(0), Data2, n
ElseIf Option1(2).Value Then
n = 4
ReDim s(0 To 3)
Data4 = Text5.Text
CopyMemory s(0), Data4, n
End If
hWndOfApp = FindWindow(vbNullString, Text1.Text)
hThreadID = GetWindowThreadProcessId(hWndOfApp, hProcessID)
hProcess = OpenProcess(PROCESS_ALL_ACCESS _
, 0, hProcessID)
WriteProcessMemory hProcess, ByVal lBassAddr, s(0), n, ByVal 0&
CloseHandle hProcess
End Sub
Private Sub Form_Load()
GetSystemInfo hSysInfo '取得應用程式最小及最大定址
Text2.Text = hSysInfo.lpMinimumApplicationAddress '應用程式最小定址
Text4.Text = hSysInfo.lpMinimumApplicationAddress '應用程式最小定址
Label5.Caption = "可用位址從" & hSysInfo.lpMinimumApplicationAddress & _
" 到 " & hSysInfo.lpMaximumApplicationAddress
End Sub
'偵測是否按下Enter 鍵,若是則執行 同Command2_Click程式
Private Sub Text1_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles Text1.KeyDown
Dim KeyCode As Short = eventArgs.KeyCode
Dim Shift As Short = eventArgs.KeyData \ &H10000
If KeyCode = 13 Then Call Command2_Click(Command2, New System.EventArgs())
End Sub
------------------------------------------------------------------------------------
Private Sub Command1_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, _
Thread)
'Display the message box
MsgBox "This message box has been positioned at (0,0)."
End Sub
Private Sub Command2_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, _
Thread)
'Display the message box
MsgBox "This message box is centered over Form1."
End Sub
=============================================================================
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) _
As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias _
"SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function SetWindowPos Lib "user32" ( _
ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd _
As Long, lpRect As RECT) As Long
Public Const GWL_HINSTANCE = (-6)
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOACTIVATE = &H10
Public Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5
Public hHook As Long
Function WinProc1(ByVal lMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
If lMsg = HCBT_ACTIVATE Then
'Show the MsgBox at a fixed location (0,0)
SetWindowPos wParam, 0, 0, 0, 0, 0, _
SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProc1 = False
End Function
Function WinProc2(ByVal lMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim rectForm As RECT, rectMsg As RECT
Dim x As Long, y As Long
'On HCBT_ACTIVATE, show the MsgBox centered over Form1
If lMsg = HCBT_ACTIVATE Then
'Get the coordinates of the form and the message box so that
'you can determine where the center of the form is located
GetWindowRect Form1.hwnd, rectForm
GetWindowRect wParam, rectMsg
x = (rectForm.Left + (rectForm.Right - rectForm.Left) / 2) - _
((rectMsg.Right - rectMsg.Left) / 2)
y = (rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2) - _
((rectMsg.Bottom - rectMsg.Top) / 2)
'Position the msgbox
SetWindowPos wParam, 0, x, y, 0, 0, _
SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProc2 = False
procedure TForm1.Button1Click(Sender: TObject);
var
h: HWnd;
p: array[0..254] of char;
begin
h := GetWindow(Handle, GW_HWNDFIRST);
while h <> 0 do
begin
if GetWindowText(h, p, 255) > 0 then Memo1.Lines.Add(p);
h := GetWindow(h, GW_HWNDNEXT);
end;
end;
-------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Csharp_TEST
{
public partial class Form1 : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
const int MOUSEEVENTF_MOVE = 0x0001; //移動滑鼠
const int MOUSEEVENTF_LEFTDOWN = 0x0002; //左鍵下
const int MOUSEEVENTF_LEFTUP = 0x0004; //左鍵上
const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //右鍵下
const int MOUSEEVENTF_RIGHTUP = 0x0010; //右鍵上
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;//中鍵下
const int MOUSEEVENTF_MIDDLEUP = 0x0040;// 中鍵上
const int MOUSEEVENTF_ABSOLUTE = 0x8000; //絕對座標
public Form1()
{
InitializeComponent();
}
'後來修正列印的語法後,就可以依CrystalReport設計時所設定的印表機紙張進行列印
Dim customerReport As ReportDocument = New ReportDocument
customerReport.Load(Test.rpt)
customerReport.SetDataSource(ds) 'ds=dataset
CrystalReportViewer1.ReportSource = customerReport
Public Class Form1
Const PROCESS_WM_READ As Integer = &H10
<DllImport("kernel32.dll")>
Public Shared Function OpenProcess(dwDesiredAccess As UInteger, bInheritHandle As Boolean, dwProcessId As Integer) As Integer
End Function
<DllImport("kernel32.dll")>
Public Shared Function ReadProcessMemory(hProcess As Integer, lpBaseAddress As Integer, buffer As Byte(), size As Integer, lpNumberOfBytesRead As Integer) As Boolean
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim notepadProcess As Process = Process.GetProcessesByName("程式名稱")(0)
Dim processHandle As IntPtr = OpenProcess(PROCESS_WM_READ, False, notepadProcess.Id)
Dim bytesRead As Integer = 0
Dim buffer As Byte() = New Byte(23) {}
ReadProcessMemory(CInt(processHandle), &HXXXXXX, buffer, buffer.Length, bytesRead)
'&HXXXXXX是指定程式字串位置
MsgBox(Encoding.UTF8.GetString(buffer))
End Sub
Public Class TextLine
Public Line As String
' 建構子(1)
Public Sub New()
Line = ""
End Sub
' 建構子(2)
Public Sub New(text As String)
Line = text
End Sub
' 物件方法
Public Function GetWord() As String
Dim arrWords() As String = Split(Line, " ")
Return (arrWords(0))
End Function
' 可覆寫的方法
Public Overridable Sub ShowText()
Console.WriteLine(LCase(Line))
End Sub
End Class
Class 2
Public Class MyLine
Inherits TextLine
' 建構子
Public Sub New()
MyBase.New()
End Sub
' 新增屬性
ReadOnly Property Length() As Long
Get
Return Len(MyBase.Line)
End Get
End Property
' 新增方法
Public Function InStr(str As String) As Integer
Dim arrWords() As String =
Split(MyBase.Line, " ")
Dim i As Integer
For i = 0 To UBound(arrWords)
If arrWords(i) = str Then
Exit For
End If
Next i
If i < UBound(arrWords) Then
Return i + 1
End If
Return -1
End Function
' 覆寫方法
Public NotOverridable Overrides Sub ShowText()
Console.WriteLine(MyBase.Line)
End Sub
End Class
Module 1
Module Module1
Sub Main()
Dim line As MyLine = New MyLine()
Dim txtline As TextLine =
New TextLine("THIS IS A BOOK.")
txtline.ShowText()
Console.WriteLine("第1個字: " & txtline.GetWord())
line.Line = "That is a pen."
line.ShowText()
Console.WriteLine("字串長度: " & line.Length)
Console.WriteLine("第1個字: " & line.GetWord())
Console.WriteLine("找尋is: " & line.InStr("is"))
line = Nothing
txtline = Nothing
Console.Read()
End Sub
End Module
1.寫Class or form 要有封裝的效果,所謂封裝就是該對外面通訊才Public,不然就用Private.為什麼,你有看過一隻Class有好幾千個屬性,方法嗎?,你開的越多越難用,而每個Public 是 Property 而不要是 Field,而每個輸入都要有Check的動作.
2.Class or Form 之間要低偶合,所謂低偶合就是 A跟B之間的關係越小越好.怎麼將偶那?就是你在設計門面(方法),介面的通道越小越好.比如我今天要FormB 去修改 FormA的TextBoxA ,我今天設計 FormB去接收 FormA,那我真的只能改 FormA裡的TextBoxA. 如果我今天改一下 FormB去接TextBox的參數,我FormB就可以去改 FormA的TextBoxA,TextBoxB 或 FormC的TextBox.
接著第二個降偶的方式,是用第三方物件去傳參數.(範例還在思考中).
第三個比較複雜一點.比如說我 FormB 要執行 FormA 裡面三個方法.這個你就要將這三個方法去抽象化或介面化後當成參數去傳.
Exp: 我FormB 是做列印對話盒的動作.功能是要做印表機參數設定,列印的內容取自FormA的三個方法.所以你將三個方法取出並產生IPrint的介面,你列印時只要將IPrint 參數傳到 FormB就可以列印.而今天我要列印FormC,我也只要FormC實作IPrint 就可以列印FormC.
3.2個Control間內容要同步,動 ControlA ,ControlB連動,底層商業物件內容連動.這個你們要去學DataBinding 的物件及IPropertyChange.ICollectionChange(上面二個介面名稱可能不很正確,你們要在驗證一下).
有了這些,你就可以將資料庫或其它來源的資料Binding 到 Control 呈現,你在UI的修改會自動更新其他的畫面,及底層的資料來源.
-------------------------------------------------------------------------------------------
For Each MyRow In ds.Tables.Item("DataTable1").Rows
ComboBox1.Items.Add(ds.Tables.Item(0).Rows(n).Item(0).ToString())
n += 1
Next