📄 copyitems.asm
字号:
;-----------------------------------------------------------------------------------------
; Prototypes.
;-----------------------------------------------------------------------------------------
SaveItems PROTO :DWORD, :DWORD
CopyItemsToClipboard PROTO :DWORD, :DWORD
CopyItemsToBuffer PROTO :DWORD
CopyHeadersToBuffer PROTO :DWORD, :DWORD
CopyItemToBuffer PROTO :DWORD, :DWORD, :DWORD
.code
;-----------------------------------------------------------------------------------------
; Purpose: Asks the user for a filename and saves all or the selected items (depending
; on IDMI) of the result list view in this file.
;
; Inputs: IDMI - identifier of the selected menu item
; hWnd - owner window
;
; Outputs: Creates or overwrites a file containing the entries.
;
; Notes: The identifier has to be equal to the one used in the resource file.
;-----------------------------------------------------------------------------------------
SaveItems PROC IDMI:DWORD, hWnd:DWORD
LOCAL hMem_Buffer :DWORD
LOCAL hFile :DWORD
LOCAL BytesWritten :DWORD
;---------------------------------------------------------------------------------------
;-- Get the filename.
mov OFN.lStructSize, SIZEOF OFN
mov eax, hWnd
mov OFN.hWndOwner, eax
mov eax, hInstance
mov OFN.hInstance, eax
mov OFN.lpstrFilter, OFFSET FILTER_SaveAs
mov OFN.lpstrFile, OFFSET filename
mov OFN.nMaxFile, SIZEOF filename
mov eax, lpSTR_TITLE_SAVEAS
mov OFN.lpstrTitle, eax
mov OFN.Flags, OFN_EXPLORER or OFN_LONGNAMES or \
OFN_HIDEREADONLY or OFN_PATHMUSTEXIST
.IF SeparatingChar == 59
;-----------------------------------------------------------------------------------
;-- If the semicolon is selected as separator then select '*.csv' as standard
;-- filter.
mov OFN.nFilterIndex, 2
.ELSE
;-----------------------------------------------------------------------------------
;-- Select '*.txt' as standard filter.
mov OFN.nFilterIndex, 0
.ENDIF
invoke GetSaveFileName, OFFSET OFN
;---------------------------------------------------------------------------------------
;-- Did the user press cancel?
test eax, eax
jz @Return
;---------------------------------------------------------------------------------------
;-- No. Fill the buffer with the informations.
xor eax, eax
.IF IDMI == IDMI_SAVEALL
;-----------------------------------------------------------------------------------
;-- If all item shall be copied/saved then the argument has to be -1, otherwise 0.
dec eax
.ENDIF
invoke CopyItemsToBuffer, eax
;---------------------------------------------------------------------------------------
;-- If an error has occured return.
test eax, eax
jz @Return
;---------------------------------------------------------------------------------------
;-- No. Save the handle.
mov hMem_Buffer, eax
;---------------------------------------------------------------------------------------
;-- Get the length of the filename.
invoke StrLen, OFFSET filename
push eax
;-------------------------------------------------------------------------------------
;-- Append the selected extension.
.IF OFN.nFilterIndex == 1
mov eax, EXTENSION_TXT
.ELSE
mov eax, EXTENSION_CSV
.ENDIF
invoke szCatStr, OFFSET filename, eax
;-------------------------------------------------------------------------------------
;-- Create the file.
invoke CreateFile, OFFSET filename, GENERIC_WRITE, FILE_SHARE_READ, \
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
;---------------------------------------------------------------------------------------
;-- Remove the extension.
pop ebx
mov BYTE PTR [OFFSET filename + ebx], NULL
;---------------------------------------------------------------------------------------
;-- Is an error occured?
test eax, eax
jz @Return
;---------------------------------------------------------------------------------------
;-- No. Save the handle.
mov hFile, eax
;---------------------------------------------------------------------------------------
;-- Prohibit the movement of the allocated memory block.
invoke GlobalLock, hMem_Buffer
;---------------------------------------------------------------------------------------
;-- Get the length of the buffer.
push eax
invoke StrLen, eax
pop ecx
;---------------------------------------------------------------------------------------
;-- Write the buffer into the file.
lea ebx, BytesWritten
invoke WriteFile, hFile, ecx, eax, ebx, NULL
;---------------------------------------------------------------------------------------
;-- Close the file.
invoke CloseHandle, hFile
;---------------------------------------------------------------------------------------
;-- Free the allocated memory.
invoke GlobalFree, hMem_Buffer
@Return:
ret
SaveItems ENDP
;-----------------------------------------------------------------------------------------
; Purpose: Copies all or the selected items of the result list view in a buffer.
;
; Inputs: items - 0 = copy selected items; -1 = copy all items
;
; Outputs: EAX - handle to an allocated memory block containing the entries
;
; Notes: none
;-----------------------------------------------------------------------------------------
CopyItemsToBuffer PROC items:DWORD
LOCAL hMem_Buffer :HGLOBAL
LOCAL lpMem_Buffer :HGLOBAL
LOCAL ItemCount :DWORD
LOCAL colArray[LVResult_ColCount] :DWORD
mov hMem_Buffer, NULL
;---------------------------------------------------------------------------------------
;-- Shall only the selected items be copied?
.IF items != -1
;-----------------------------------------------------------------------------------
;-- Yes. Get the number of selected items.
invoke SendMessage, hTableDialog_LV_Result, LVM_GETSELECTEDCOUNT, NULL, NULL
;-----------------------------------------------------------------------------------
;-- If no item is selected return.
test eax, eax
jz @Return
.ENDIF
;---------------------------------------------------------------------------------------
;-- Get the number of items in the result list view.
invoke SendMessage, hTableDialog_LV_Result, LVM_GETITEMCOUNT, NULL, NULL
;-----------------------------------------------------------------------------------
;-- If the number is 0 return, otherwise save this number.
test eax, eax
jz @Return
mov ItemCount, eax
;---------------------------------------------------------------------------------------
;-- Allocate a buffer (number of items*512 byte)
inc eax
shl eax, 9
invoke GlobalAlloc, GMEM_MOVEABLE, eax
;---------------------------------------------------------------------------------------
;-- If an error occured return, otherwise save the handle.
test eax, eax
jz @Return
mov hMem_Buffer, eax
;---------------------------------------------------------------------------------------
;-- Prohibit the movement of the allocated memory block.
invoke GlobalLock, eax
;---------------------------------------------------------------------------------------
;-- If an error occured free the allocated memory and return, otherwise save the offset
;-- of the memory block.
test eax, eax
jz @Return
mov lpMem_Buffer, eax
;---------------------------------------------------------------------------------------
;-- Get the current order of columns in the result list view.
invoke SendMessage, hTableDialog_LV_Result, LVM_GETCOLUMNORDERARRAY, LVResult_ColCount, ADDR colArray
;---------------------------------------------------------------------------------------
;-- If an error occured free the allocated memory and return.
test eax, eax
jz @Return
;---------------------------------------------------------------------------------------
;-- Mark columns with a width of 0 pixels with a -1 in the array.
lea ebx, colArray
mov ecx, LVResult_ColCount
@@:
;-------------------------------------------------------------------------------------
;-- If all columns have been processed return.
dec ecx
js @F
;-------------------------------------------------------------------------------------
;-- Otherwise get the width of the current column.
push ebx
push ecx
invoke SendMessage, hTableDialog_LV_Result, LVM_GETCOLUMNWIDTH, DWORD PTR [ecx*4 + ebx], NULL
pop ecx
pop ebx
;-------------------------------------------------------------------------------------
;-- If the width is 0 then set the entry in the array colArray to -1.
test eax, eax
jnz @B
mov DWORD PTR [ecx*4 + ebx], -1
;-------------------------------------------------------------------------------------
;-- Process the next column.
jmp @B
@@:
.IF fHeaders
;-----------------------------------------------------------------------------------
;-- Copy the headers, too.
invoke CopyHeadersToBuffer, lpMem_Buffer, ADDR colArray
dec eax
.ELSE
;-----------------------------------------------------------------------------------
;-- Don't copy the headers.
xor eax, eax
.ENDIF
;---------------------------------------------------------------------------------------
;-- Add the length of the headers to the offset of the buffer.
mov edx, lpMem_Buffer
add edx, eax
xor ecx, ecx
;---------------------------------------------------------------------------------------
;-- Create the new content for the clipboard.
.IF items == -1
;-----------------------------------------------------------------------------------
;-- Copy all items.
@@:
;---------------------------------------------------------------------------------
;-- Copy the current item to the buffer.
push ecx
push edx
invoke CopyItemToBuffer, ecx, edx, ADDR colArray
pop edx
pop ecx
;---------------------------------------------------------------------------------
;-- Add the length of the last string to the offset of the buffer.
dec eax
add edx, eax
;---------------------------------------------------------------------------------
;-- If not all items have been processed copy the next one.
inc ecx
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -