------------------------------------------窗口的代碼-------------------
窗體:form1
圖片框 picture1
文本框 text1
private sub form_load()
set pic = loadrespicture(102, 0)
set picture1.picture = pic
dim hdc as long
hdc = getdc(text1.hwnd) '建立一個臨時dc
memdc = createcompatibledc(hdc)
membitmap = createcompatiblebitmap(hdc, text1.width, text1.height)
selectobject memdc, membitmap
stretchblt memdc, 0, 0, text1.width, text1.height, picture1.hdc, 0, 0, text1.width, text1.height, srccopy
releasedc text1.hwnd, hdc
if memdc = 0 or membitmap = 0 then
msgbox "error create dc"
end
end if
oldproc = setwindowlong(text1.hwnd, gwl_wndproc, addressof winproc)
oldwndproc = setwindowlong(me.hwnd, gwl_wndproc, addressof winproc1)
end sub
private sub form_unload(cancel as integer)
deleteobject membitmap
deletedc memdc
setwindowlong me.hwnd, gwl_wndproc, oldwndproc
setwindowlong text1.hwnd, gwl_wndproc, oldproc
end sub
private sub text1_dblclick()
sendmessage text1.hwnd, wm_paint, 0, 0
end sub
private sub text1_mousedown(button as integer, shift as integer, x as single, y as single)
sendmessage text1.hwnd, wm_paint, 0, 0
end sub
private sub text1_mousemove(button as integer, shift as integer, x as single, y as single)
'選定文本的時候如果文本選頂發生了變化,則通知更
static startpos0 as long, endpos0 as long
dim startpos as long, endpos as long
if button = 1 then
dim v as long
v = sendmessage(text1.hwnd, em_getsel, 0, 0)
endpos = v / 65536: startpos = v mod 65536 '-->獲得選定文本位置
if startpos <> endpos then '--->發現有選定時候檢查選定是否和上次的相同?不同的話則重畫
if startpos0 = startpos and endpos = endpos0 then
else '---->內容發生變化的時候發送消息請求重畫
sendmessage text1.hwnd, wm_paint, 0, 0
startpos0 = startpos: endpos0 = endpos
end if
end if
end if
end sub
private sub text1_mouseup(button as integer, shift as integer, x as single, y as single)
postmessage text1.hwnd, wm_paint, 0, 0
end sub
private sub text1_change()
sendmessage form1.text1.hwnd, wm_paint, 0, 0
end sub
--------------------------------------------模塊代碼-------------------------------------------------
public declare function setwindowlong lib "user32" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long
public declare function callwindowproc lib "user32" alias "callwindowproca" (byval lpprevwndfunc as long, byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as long
public declare function bitblt lib "gdi32" (byval hdestdc as long, byval x as long, byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc as long, byval xsrc as long, byval ysrc as long, byval dwrop as long) as long
public declare function sendmessage lib "user32" alias "sendmessagea" (byval hwnd as long, byval wmsg as long, byval wparam as long, lparam as any) 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 createcompatibledc lib "gdi32" (byval hdc as long) as long
public declare function createcompatiblebitmap lib "gdi32" (byval hdc as long, byval nwidth as long, byval nheight as long) as long
public declare function selectobject lib "gdi32" (byval hdc as long, byval hobject as long) as long
public declare function deleteobject lib "gdi32" (byval hobject as long) as long
public declare function deletedc lib "gdi32" (byval hdc as long) as long
public declare function getdc lib "user32" (byval hwnd as long) as long
public declare function releasedc lib "user32" (byval hwnd as long, byval hdc as long) as long
public const wm_erasebkgnd = &h14
public const en_vscroll = &h602
public const wm_command = &h111
public const en_hscroll = &h601
public const en_change = &h300
public const en_update = &h400
public const em_getsel = &hb0
public const srccopy = &hcc0020 ' (dword) dest = source
public const srcand = &h8800c6 ' (dword) dest = source and dest
public const srcpaint = &hee0086 ' (dword) dest = source or dest
public const srcerase = &h440328 ' (dword) dest = source and (not dest )
public const em_scroll = &hb5
public const gwl_wndproc = (-4)
public const wm_paint = &hf
public memdc as long
public membitmap as long
public oldwndproc as long
public oldproc as long
public pic as picture
public function winproc(byval hwnd as long, byval msg as long, byval wp as long, byval lp as long) as long
with form1.text1
if msg = wm_paint then
debug.print token
dim hdc as long
winproc = callwindowproc(oldproc, form1.text1.hwnd, msg, wp, lp)
if wp = 1 then .visible = false: .visible = true
hdc = getdc(form1.text1.hwnd)
bitblt hdc, 0, 0, form1.text1.width, form1.text1.height, memdc, 0, 0, srcand
releasedc form1.text1.hwnd, hdc
exit function
end if
winproc = callwindowproc(oldproc, form1.text1.hwnd, msg, wp, lp)
end with
end function
public function winproc1(byval hwnd as long, byval msg as long, byval wp as long, byval lp as long) as long
if msg = wm_command then
select case wp / 65536
case en_vscroll '---->獲得文本框縱向滾動消息
sendmessage form1.text1.hwnd, wm_paint, 1, 0
case en_hscroll '----->獲得橫向滾動消息
sendmessage form1.text1.hwnd, wm_paint, 1, 0
case en_update
sendmessage form1.text1.hwnd, wm_paint, 0, 0
end select
end if
winproc1 = callwindowproc(oldwndproc, hwnd, msg, wp, lp)
end function
-------------------------------------------------------------------------------------------------------
這樣就可以 在圖片里加如圖片的背景了。
本程序在 2000/xp 下調試通過。有一個缺點是閃動比較厲害,希望各位大蝦指正。