我的程序的主要思想是:利用API函数创建N个按钮,然后在各该钮上根据起止颜色的不用,分别画上不同的渐变色。
消息处理里,在PAINT、LBUTTONDOWN、LBUTTONUP中我都调用同一个过程<下有代码>进行渐变 色的绘制,但是点击大约40多次后,系统提示“内存溢出”。删除该段过程后,系统能正常执行。
我也不知道问题到底出在哪里,是不是有些什么资源用完后没有释放干净,导致内存溢出啊??? ;
程序如下,请高手帮我看看~~~~~~~~~~~~~急~~~~~~~~~~~~~~~~~在线等'****************
'* 消息处理 *
'****************
Public Function OnTime_ButtonProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongDim SColor As Long
Dim Ecolor As Long
Dim Heigh As Integer
Dim Width As Integer
Dim SRed As Single
Dim SGreen As Single
Dim SBlue As Single
Dim ERed As Single
Dim EGreen As Single
Dim EBlue As Single
Dim hdc As Long
Dim i As Integeri = 0
OnTime_ButtonProc = CallWindowProc(lpButtonProc, hwnd, uMsg, wParam, lParam)' 寻找储存窗口句柄的数组下标 <查看类模块有具体的文字说明>
Do While (ButtonStyle(i).hwnd <> 0 And i < IButton And ButtonStyle(i).hwnd <> hwnd)
i = i + 1
Loop
' 安全效验<可删除>
If ButtonStyle(i).hwnd <> hwnd Then
Exit Function
End If
' 获得该按钮的HDC
hdc = ButtonStyle(i).hdcSColor = ButtonStyle(i).StartColor ' 获得起点颜色
Ecolor = ButtonStyle(i).EndColor ' 获得终点颜色
SRed = SColor And &HFF ' 获得起点和终点的RGB值
SGreen = Int(SColor / &H100) And &HFF
SBlue = Int(SColor / &H10000)
ERed = Ecolor And &HFF
EGreen = Int(Ecolor / &H100) And &HFF
EBlue = Int(Ecolor / &H10000)
Heigh = ButtonStyle(i).Heigh ' 渐变区域的高度
Width = ButtonStyle(i).Width ' 渐变区域的宽度Select Case uMsg
' 重画窗体中自定义部分的颜色
Case WM_PAINT
' 绘制各按钮的个性颜色
Call DrawOnTimeStyleColor(hwnd, hdc, SRed, SGreen, SBlue, ERed, EGreen, EBlue, Heigh, Width)
' 左右按钮被鼠标左键点击<需要重画>
Case WM_LBUTTONDOWN
'SetCapture hwnd
Call DrawOnTimeStyleColor(hwnd, hdc, ERed, EGreen, EBlue, SRed, SGreen, SBlue, Heigh, Width)
Case WM_LBUTTONUP
Call DrawOnTimeStyleColor(hwnd, hdc, SRed, SGreen, SBlue, ERed, EGreen, EBlue, Heigh, Width)
End SelectEnd Function***********************
* 渐变色绘制过程 *
***********************
Public Sub DrawOnTimeStyleColor(ByVal hwnd As Long, ByVal hdc As Long, ByVal SRed As Single, ByVal SGreen As Single, ByVal SBlue As Single, ByVal ERed As Single, ByVal EGreen As Single, ByVal EBlue As Single, ByVal Heigh As Integer, ByVal Width As Integer)
'On Error Resume Next
Dim hPen As Long, hBrush As Long
Dim p As Long, i As Integer
Dim FRect As RECT
Dim BlueArea As Single
Dim GreenArea As Single
Dim RedArea As Single
Dim Red As Single
Dim Blue As Single
Dim Green As Single
Dim ApiPointNull As POINTAPI
UserStyle = STYLE_SHADE If NTest = True Then ' 调试时使用Select Case UserStyle
' 渐变风格<垂直渐变>
Case STYLE_SHADE
' 设置渐变区域
RedArea = (ERed - SRed) / Heigh
GreenArea = (EGreen - SGreen) / Heigh
BlueArea = (EBlue - SBlue) / Heigh
For i = 0 To Heigh - 1
Tnum = Tnum + 1
Red = SRed + i * RedArea
Green = SGreen + i * GreenArea
Blue = SBlue + i * BlueArea
hdc = GetDC(hwnd) ' 获得hdc
trgb = RGB(Red, Green, Blue)
hPen = CreatePen(PS_DASH, 1, RGB(Red, Green, Blue))
p0 = SelectObject(hdc, hPen)
p1 = MoveToEx(hdc, 0, i, ApiPointNull) ; ' 0 表示失败
p2 = LineTo(hdc, Width, i) ' 0 表示失败
p3 = DeleteObject(hPen)
' p = ReleaseDC(hwnd, hdc)
' 2420 tnum:662 hpen : 0 p0: 0 1 p3 0
If hPen = 0 Then
'Debug.Print "看看我画的效果 : " & hwnd & " tnum:" & Tnum & "hpen : " & hPen & p0 & " " & p1 & " p3 " & p3
' frm_Ontime.Text2.Text = Frm_Ontime.Text2.Text & hwnd & " tnum:" &Tnum & " hpen : " & hPen & " p0: " & p0 & " " & p1 & " p3 " & p3 & "RGB:" & trgb & Chr(13) & Chr(10)
End If
Next iEnd Select' p = DeleteDC(hdc)
' Debug.Print "释放HDC的返回值 : " & p
End If
消息处理里,在PAINT、LBUTTONDOWN、LBUTTONUP中我都调用同一个过程<下有代码>进行渐变 色的绘制,但是点击大约40多次后,系统提示“内存溢出”。删除该段过程后,系统能正常执行。
我也不知道问题到底出在哪里,是不是有些什么资源用完后没有释放干净,导致内存溢出啊??? ;
程序如下,请高手帮我看看~~~~~~~~~~~~~急~~~~~~~~~~~~~~~~~在线等'****************
'* 消息处理 *
'****************
Public Function OnTime_ButtonProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongDim SColor As Long
Dim Ecolor As Long
Dim Heigh As Integer
Dim Width As Integer
Dim SRed As Single
Dim SGreen As Single
Dim SBlue As Single
Dim ERed As Single
Dim EGreen As Single
Dim EBlue As Single
Dim hdc As Long
Dim i As Integeri = 0
OnTime_ButtonProc = CallWindowProc(lpButtonProc, hwnd, uMsg, wParam, lParam)' 寻找储存窗口句柄的数组下标 <查看类模块有具体的文字说明>
Do While (ButtonStyle(i).hwnd <> 0 And i < IButton And ButtonStyle(i).hwnd <> hwnd)
i = i + 1
Loop
' 安全效验<可删除>
If ButtonStyle(i).hwnd <> hwnd Then
Exit Function
End If
' 获得该按钮的HDC
hdc = ButtonStyle(i).hdcSColor = ButtonStyle(i).StartColor ' 获得起点颜色
Ecolor = ButtonStyle(i).EndColor ' 获得终点颜色
SRed = SColor And &HFF ' 获得起点和终点的RGB值
SGreen = Int(SColor / &H100) And &HFF
SBlue = Int(SColor / &H10000)
ERed = Ecolor And &HFF
EGreen = Int(Ecolor / &H100) And &HFF
EBlue = Int(Ecolor / &H10000)
Heigh = ButtonStyle(i).Heigh ' 渐变区域的高度
Width = ButtonStyle(i).Width ' 渐变区域的宽度Select Case uMsg
' 重画窗体中自定义部分的颜色
Case WM_PAINT
' 绘制各按钮的个性颜色
Call DrawOnTimeStyleColor(hwnd, hdc, SRed, SGreen, SBlue, ERed, EGreen, EBlue, Heigh, Width)
' 左右按钮被鼠标左键点击<需要重画>
Case WM_LBUTTONDOWN
'SetCapture hwnd
Call DrawOnTimeStyleColor(hwnd, hdc, ERed, EGreen, EBlue, SRed, SGreen, SBlue, Heigh, Width)
Case WM_LBUTTONUP
Call DrawOnTimeStyleColor(hwnd, hdc, SRed, SGreen, SBlue, ERed, EGreen, EBlue, Heigh, Width)
End SelectEnd Function***********************
* 渐变色绘制过程 *
***********************
Public Sub DrawOnTimeStyleColor(ByVal hwnd As Long, ByVal hdc As Long, ByVal SRed As Single, ByVal SGreen As Single, ByVal SBlue As Single, ByVal ERed As Single, ByVal EGreen As Single, ByVal EBlue As Single, ByVal Heigh As Integer, ByVal Width As Integer)
'On Error Resume Next
Dim hPen As Long, hBrush As Long
Dim p As Long, i As Integer
Dim FRect As RECT
Dim BlueArea As Single
Dim GreenArea As Single
Dim RedArea As Single
Dim Red As Single
Dim Blue As Single
Dim Green As Single
Dim ApiPointNull As POINTAPI
UserStyle = STYLE_SHADE If NTest = True Then ' 调试时使用Select Case UserStyle
' 渐变风格<垂直渐变>
Case STYLE_SHADE
' 设置渐变区域
RedArea = (ERed - SRed) / Heigh
GreenArea = (EGreen - SGreen) / Heigh
BlueArea = (EBlue - SBlue) / Heigh
For i = 0 To Heigh - 1
Tnum = Tnum + 1
Red = SRed + i * RedArea
Green = SGreen + i * GreenArea
Blue = SBlue + i * BlueArea
hdc = GetDC(hwnd) ' 获得hdc
trgb = RGB(Red, Green, Blue)
hPen = CreatePen(PS_DASH, 1, RGB(Red, Green, Blue))
p0 = SelectObject(hdc, hPen)
p1 = MoveToEx(hdc, 0, i, ApiPointNull) ; ' 0 表示失败
p2 = LineTo(hdc, Width, i) ' 0 表示失败
p3 = DeleteObject(hPen)
' p = ReleaseDC(hwnd, hdc)
' 2420 tnum:662 hpen : 0 p0: 0 1 p3 0
If hPen = 0 Then
'Debug.Print "看看我画的效果 : " & hwnd & " tnum:" & Tnum & "hpen : " & hPen & p0 & " " & p1 & " p3 " & p3
' frm_Ontime.Text2.Text = Frm_Ontime.Text2.Text & hwnd & " tnum:" &Tnum & " hpen : " & hPen & " p0: " & p0 & " " & p1 & " p3 " & p3 & "RGB:" & trgb & Chr(13) & Chr(10)
End If
Next iEnd Select' p = DeleteDC(hdc)
' Debug.Print "释放HDC的返回值 : " & p
End If
解决方案 »
- vb调用com组件中的方法,以传出字符串数据,类型不匹配的问题
- 用TestBox+ListView實現ComboBox加強時所遇到的問題,請高手們指點
- 谁知道哪有BASIC语言教程?
- 怎样有vb控制windows系统的声道,在线,解决就给分!
- 在VB里如何打开一个指定网页(地址形式为:xx.xxx.xxx.xx的IP地址形式)
- 请问:能否用VB,VC生成Word的宏?
- 请问如何在程序中调用chm帮助文件?
- 关于MsHFlexGrid 的Additem后的显示问题!!!!Help Me!!!!
- 各位VB爱好者:简单问题就送分!!!!!
- 我这台机器是怎么了,怎么会出现两个鼠标箭头???
- CSDN历史上最牛的贴,将近1000层
- 1000分求好的视频会议软件
ReleaseDC hWnd,hdc
但是你们应该看到我程序的最后,我把ReleaseDc注销了。其实用ReleaseDc仍然解决不了我的问题。今天终于想了个办法解决了。
谢谢大家