首页 > 开发 > 综合 > 正文

在 普通的文本框 里加入图片背景

2024-07-21 02:25:13
字体:
来源:转载
供稿:网友
------------------------------------------窗口的代码-------------------

窗体: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 下调试通过。有一个缺点是闪动比较厉害,希望各位大虾指正。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表