コード: 全て選択
'-----------------------------------------------------------------------------
' イベント プロシージャ
'-----------------------------------------------------------------------------
' このファイルには、ウィンドウ [MainWnd] に関するイベントをコーディングします。
' ウィンドウ ハンドル: hMainWnd
' TODO: この位置にグローバルな変数、構造体、定数、関数を定義します。
Declare Function MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As VoidPtr,pSrc As VoidPtr,length As Long) As Long'?
Type Undo
pNext As *Undo
nStart As Long
nEnd As Long
szText As String
nCount As Long
End Type
Const UNDO_INPUT=0
Const UNDO_DELETE=1
Dim hEdit As HWND
Dim pOldEditProc As VoidPtr
Dim pUndo As *Undo
Function CreateUndoBuffer() As *Undo
CreateUndoBuffer=malloc(20)
CreateUndoBuffer->pNext=0
End Function
Sub DestroyUndoBuffer(pBuffer As *Undo)
Dim p As *Undo
While pBuffer->pNext
p=pBuffer->pNext
free(pBuffer)
pBuffer=p
Wend
End Sub
Sub AddUndo(undokind As Long,nStart As Long,nEnd As Long,pszText As BytePtr,count As Long) As *Undo
Dim ptmp As *Undo
ptmp=pUndo
pUndo=malloc(20)
pUndo->pNext=ptmp
pUndo->nStart=nStart
pUndo->nEnd=nEnd
pUndo->szText=pszText
pUndo->nCount=count
End Sub
Function EditProc(hWnd As HWND,message As DWord,wParam As WPARAM,lParam As LPARAM) As LRESULT
Dim nStart As Long,nEnd As Long,length As Long
Dim buf As BytePtr,buf2 As BytePtr
Select Case message
Case WM_KEYDOWN
If wParam>=Asc("A") and wParam<=Asc("Z") Then
nStart=LOWORD(SendMessage(hEdit,EM_GETSEL,0,0))
nEnd=HIWORD(SendMessage(hEdit,EM_GETSEL,0,0))
length=1
AddUndo(UNDO_INPUT,nStart,nEnd,"",length)
ElseIf wParam=VK_DELETE Then
nStart=LOWORD(SendMessage(hEdit,EM_GETSEL,0,0))
nEnd=HIWORD(SendMessage(hEdit,EM_GETSEL,0,0))
If nStart=nEnd Then
If GetWindowTextLength(hEdit)=nStart Then goto *EditProcEnd'Selectから抜け出す記述がよくわからない
length=1
Else
length=nEnd-nStart
End If
buf=malloc(GetWindowTextLength(hEdit)+1)
buf2=calloc(length+1)
GetWindowText(hEdit,buf,GetWindowTextLength(hEdit)+1)
MoveMemory(buf2,buf,length)
AddUndo(UNDO_DELETE,nStart,nEnd,buf2,length)
free(buf)
free(buf2)
End If
End Select
*EditProcEnd
EditProc=CallWindowProc(pOldEditProc,hWnd,message,wParam,lParam)
End Function
'-----------------------------------------------------------------------------
' ウィンドウメッセージを処理するためのコールバック関数
Function MainWndProc(hWnd As HWND, dwMsg As DWord, wParam As WPARAM, lParam As LPARAM) As DWord
' TODO: この位置にウィンドウメッセージを処理するためのコードを記述します。
' イベントプロシージャの呼び出しを行います。
MainWndProc=EventCall_MainWnd(hWnd,dwMsg,wParam,lParam)
End Function
'-----------------------------------------------------------------------------
' ここから下は、イベントプロシージャを記述するための領域になります。
Sub MainWnd_Destroy()
DestroyUndoBuffer(pUndo)
Test_DestroyObjects()
PostQuitMessage(0)
End Sub
Sub MainWnd_Create(ByRef CreateStruct As CREATESTRUCT)
pUndo=CreateUndoBuffer()
hEdit=GetDlgItem(hMainWnd,EditBox1)
pOldEditProc=SetWindowLong(hEdit,GWL_WNDPROC,AddressOf(EditProc) As Long) As VoidPtr
End Sub
Sub MainWnd_Resize(SizeType As Long, cx As Integer, cy As Integer)
MoveWindow(hEdit,0,0,cx,cy,1)
End Sub
Sub MainWnd_Activate(state As Integer, minimized As Integer)
If state<>WA_INACTIVE Then SetFocus(hEdit)
End Sub