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

原创 2004年09月15日 14:55:00

这是最终的运行结果。下面给出代码。

------------------------------------------窗口的代码-------------------

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

 

VC2005 MFC静态文本框背景色设置

最近在需要用MFC做一个上位机,第一次接触这个东西我想把我在这过程中遇到的问题写下来,希望也能帮助遇到同样问题的朋友。有什么地方不对的请各位指正。 我在利用MFC做登录界面时出现了一个问题。我发现如果...
  • shijian67520
  • shijian67520
  • 2017年03月10日 10:20
  • 504

如何为PreferenceScreen分别设置窗口背景图片和actionBar背景图片/拖动时不黑屏

先看下需要的效果图: PreferenceScreen里,actionBar是深色木质纹理贴图,内容是浅色木质纹理贴图。 步骤如下: 1. 打开values目录下的styles.xml(没有...
  • kagami1983
  • kagami1983
  • 2015年06月10日 21:09
  • 1021

unity添加图片背景

1.       创建一个新的摄像机,命名为Background Camera 2.       新建GUI Texture,命名为Background Image 3.       在Backg...
  • pzw0416
  • pzw0416
  • 2013年07月16日 17:55
  • 3748

MFC如何设置背景图片

方法有很多种,有难有易。接下来从简单到难来讲解,并举例。一、利用WM_CTLCOLOR消息将图片绘制上去步骤如下: 1.为对话框添加WM_CTLCOLOR消息 2.添加两个成员变量 CBit...
  • qq_18297675
  • qq_18297675
  • 2017年02月28日 21:14
  • 1978

使用9Patch图片作为背景

将图片作为View组件的背景时,当View中要呈现的文字内容太多时,Android会自动缩放整张图片,以保证背景图片能覆盖整个View。但这种缩放图片的效果可能并不好。可能存在的情况是我们只想缩放图片...
  • u011026329
  • u011026329
  • 2015年12月10日 19:41
  • 1539

css使用背景图片制作搜索框

使用图片制作搜索框 .searchCon { width: 330px; height: 35px; background: url(images/search.jpg) no-...
  • wuzhe128520
  • wuzhe128520
  • 2015年11月30日 14:49
  • 1130

MFC 基础知识:对话框背景添加图片和按钮Button添加图片

很长时间没有接触MFC相关的知识了,我大概是在大二时候学习的MFC相关知识及图像处理,现在由于要帮个朋友完成个基于C++的程序,所以又回顾了下相关知识。的确,任何知识一段时间过后都比较容易忘记,但回顾...
  • baobei0112
  • baobei0112
  • 2016年07月23日 16:46
  • 4835

mfc对话框程序如何添加背景图片以及使控件背景图片与对话框一致

首先,你需要一张适合自己对话框程序大小的
  • ytuo1223
  • ytuo1223
  • 2014年07月27日 09:33
  • 1479

unity学习之添加图片作为背景

unity学习,希望我的博客能给喜欢unity的朋友带来帮助 准备了几天的项目今天开始做了,我今天主要的任务就是做登陆注册界面,登陆注册界面要想好看点就要有背景图片,在unity中添...
  • jingjingcrystal
  • jingjingcrystal
  • 2015年01月12日 22:15
  • 6589

iOS开发 键盘处理 (隐藏,自动转到下个文本框,轻触背景隐藏)

一、点击“Return”键 隐藏自身软键盘         当对于有多个UITextField控件都想通过点击“Return”来隐藏自身软键盘的情况,这时的最好办法是使用Did End on Exi...
  • qq_25475307
  • qq_25475307
  • 2015年05月26日 19:43
  • 612
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在 普通的文本框 里加入图片背景
举报原因:
原因补充:

(最多只允许输入30个字)