请大家帮忙,有什么办法可以替换一个ocx的功能的办法,谢谢大家。

Mohoman 2002-04-17 08:31:12
大家好:

在vb里面有一个ocx是PictureClip什么的,可以把一张图片按照分配的大小
分割,然后按照顺序播放,怎么样vb里面不使用这个ocx来实现这个效果。
完全用代码完成,不要使用第三方控件。

因为怕没有人回答,所以先给50分,要是还不够请说。我会另行增加的,还有几个贴字没有人回答,答案已经有了,我会把分数也给你加的

谢谢各位
...全文
126 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jienao 2002-04-18
  • 打赏
  • 举报
回复
看MSDN吧,我也没用过,不过找到个相关例子(BitBlt),你参照一下:

HOWTO: Drawing Transparent Bitmaps
Last reviewed: July 23, 1997
Article ID: Q79212
The information in this article applies to:
Microsoft Windows Software Development Kit (SDK) for Windows, versions 3.0 and 3.1



SUMMARY
A portion of a graphic image that does not change the contents of the screen is termed "transparent." The DrawIcon function can create an image that contains transparent portions. It is also possible to obtain this functionality using the BitBlt function; however, there are some additional steps involved.

The first step is to obtain the contents of the area where the bitmap will be drawn and to store this background image in a memory display context (DC). Mask out the area of the background that corresponds to the nontransparent portion of the image bitmap and mask out all transparent pixels from the image bitmap. Use the XOR raster operation to merge the image bitmap into the background bitmap. Finally, use the BitBlt function to move the merged image to the destination DC.



MORE INFORMATION
The following nine steps describe a process used to draw transparent bitmaps:


Create a DC to hold the image bitmap.

Select the image bitmap into the DC.

Create a memory DC to hold the final image. This is the destination DC.

Copy the portion of the screen that will be covered by the image into the destination DC.

Create an "AND mask" that contains the mask of the colors to draw (the nontransparent portions of the image). To do this, perform the following three steps:
a. Set the background color of the image DC to the color that will


be transparent in the image.

b. Create a monochrome DC.

c. BitBlt the image into the monochrome DC.

This will create an AND mask of the bitmap by setting pixels that match the background color to white (1), and setting all other pixels to black (0).


Use BitBlt with the SRCAND raster operation code to copy the AND mask onto the destination DC.

Use BitBlt with the SRCAND raster operation code to copy the inverse of the AND mask onto the image DC.

Use BitBlt with the SRCPAINT raster operation code to copy the image DC onto the destination DC.

Use BitBlt to copy the contents of the destination DC to the appropriate portion of the screen.

The following code is a function that demonstrates the above steps:

void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, short xStart,
short yStart, COLORREF cTransparentColor)
{
BITMAP bm;
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
POINT ptSize;

hdcTemp = CreateCompatibleDC(hdc);
SelectObject(hdcTemp, hBitmap); // Select the bitmap

GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
// to logical points

// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);

// Create a bitmap for each DC. DCs are required for a number of
// GDI functions.

// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);

// Each DC must select a bitmap object to store pixel data.
bmBackOld = SelectObject(hdcBack, bmAndBack);
bmObjectOld = SelectObject(hdcObject, bmAndObject);
bmMemOld = SelectObject(hdcMem, bmAndMem);
bmSaveOld = SelectObject(hdcSave, bmSave);

// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(hdc));

// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);

// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor(hdcTemp, cTransparentColor);

// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
SRCCOPY);

// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcTemp, cColor);

// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
NOTSRCCOPY);

// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
SRCCOPY);

// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);

// Mask out the transparent colored pixels on the bitmap.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);

// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);

// Copy the destination to the screen.
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
SRCCOPY);

// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);

// Delete the memory bitmaps.
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));

// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}


The following is an example of how the DrawTransparentBitmap function might be called:

DrawTransparentBitmap(hdc, // The destination DC.
hBitmap, // The bitmap to be drawn.
xPos, // X coordinate.
yPos, // Y coordinate.
0x00FFFFFF); // The color for transparent
// pixels (white, in this
// example).

--------------------------------------------------------------------------------

Keywords : GdiBmp kb16bitonly kbprg
Version : 3.0 3.1
Platform : WINDOWS
Issue type : kbhowto
Mohoman 2002-04-18
  • 打赏
  • 举报
回复
还没有来解决吗?
lyqof908 2002-04-18
  • 打赏
  • 举报
回复
你看我的代码吧,不用控件,可以把屏幕分成多份并移动,稍改一下就变成
了拼图游戏。
Option Explicit
Dim BlankX As Long, BlankY As Long 'Location of blank square
Dim BlockWidth As Single, BlockHeight As Single 'Dimensions of each block
Dim FormDC As Long 'Cached value of hDC
Dim i As Long 'Index for loops
Dim LastX As Long, LastY As Long 'Location of last blank square
Dim NumBlocksX As Long, NumBlocksY As Long 'Number of blocks in each dimension
Dim RetVal As Long 'Value returned by functions
Dim ScreenhWnd As Long, ScreenDC As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Declare Function BitBlt Lib "gdi32" (ByVal hDC1 As Long, ByVal x1 As Long, ByVal y1 As Long, ByVal wd As Long, ByVal ht As Long, ByVal hDC2 As Long, ByVal x2 As Long, ByVal y2 As Long, ByVal pattern As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long

Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Const BLACKNESS = &HCC0020 '&H42&
Const SRCCOPY = &HCC0020

'
'SHUFFLE
'
Sub Main()
NumBlocksX = 8 'Important: Value must be an even divisor of horizontal resolution
NumBlocksY = 5 'Important: Value must be an even divisor of vertical resolution

Shuffle.Width = Screen.Width 'Make form the size of the screen
Shuffle.Height = Screen.Height
Shuffle.Left = 0
Shuffle.Top = 0

BlockWidth = Shuffle.ScaleWidth \ NumBlocksX 'Figure width and height of blocks
BlockHeight = Shuffle.ScaleHeight \ NumBlocksY

ScreenhWnd = GetDesktopWindow() 'Get hWnd to Desktop
ScreenDC = GetDC(ScreenhWnd) 'Get hDC of Desktop

RetVal = BitBlt(Shuffle.hDC, 0, 0, Shuffle.ScaleWidth, Shuffle.ScaleHeight, ScreenDC, 0, 0, SRCCOPY)
RetVal = ReleaseDC(ScreenhWnd, ScreenDC) 'Release DC

Shuffle.Show 'Show the form
Shuffle.AutoRedraw = 0 'AutoRedraw no longer needed
FormDC = Shuffle.hDC 'Get new DC, now that AutoRedraw is off

'Draw vertical lines

For i = 0 To NumBlocksX - 1
Shuffle.Line (i * BlockWidth, 0)-(i * BlockWidth, Shuffle.ScaleHeight)
Next i

'Draw horizontal lines

For i = 0 To NumBlocksY - 1
Shuffle.Line (0, i * BlockHeight)-(Shuffle.ScaleWidth, i * BlockHeight)
Next i

'Choose and display blank square

Randomize

BlankX = Int(Rnd * NumBlocksX)
BlankY = Int(Rnd * NumBlocksY)
RetVal = BitBlt(FormDC, BlankX * BlockWidth, BlankY * BlockHeight, BlockWidth, BlockHeight, 0, 0, 0, BLACKNESS)

'Initialize previous location (first move doesn't matter)

LastX = -1
LastY = -1

While -1
Select Case Int(Rnd * 4)
Case 0
If (BlankX > 0) And (LastX <> BlankX - 1) Then MoveX (-4)
Case 1
If (BlankX < (NumBlocksX - 1)) And (LastX <> BlankX + 1) Then MoveX (4)
Case 2
If (BlankY > 0) And (LastY <> BlankY - 1) Then MoveY (-1)
Case 3
If (BlankY < (NumBlocksY - 1)) And (LastY <> BlankY + 1) Then MoveY (1)
End Select

RetVal = DoEvents() 'Process pending events
Sleep 50
If Not Shuffle.Visible Then Exit Sub
Wend
End Sub

Sub MoveX(HowMuch As Long)
Dim LocY As Long 'Y coordinate of moving square

LastY = BlankY
LastX = BlankX
BlankX = BlankX + Sgn(HowMuch)
LocY = BlankY * BlockHeight

For i = BlankX * BlockWidth To LastX * BlockWidth + HowMuch Step -HowMuch
RetVal = BitBlt(FormDC, i - HowMuch, LocY, BlockWidth, BlockHeight, FormDC, i, LocY, SRCCOPY)

If HowMuch < 0 Then
RetVal = BitBlt(FormDC, i, LocY, Abs(HowMuch), BlockHeight, 0, 0, 0, BLACKNESS)
Else
RetVal = BitBlt(FormDC, i + BlockWidth - HowMuch, LocY, Abs(HowMuch), BlockHeight, 0, 0, 0, BLACKNESS)
End If
Next i
End Sub

Sub MoveY(HowMuch As Long)
Dim LocX As Long 'X coordinate of moving square

LastY = BlankY
LastX = BlankX
BlankY = BlankY + Sgn(HowMuch)
LocX = BlankX * BlockWidth

For i = BlankY * BlockHeight To LastY * BlockHeight + HowMuch Step -HowMuch
RetVal = BitBlt(FormDC, LocX, i - HowMuch, BlockWidth, BlockHeight, FormDC, LocX, i, SRCCOPY)

If HowMuch < 0 Then
RetVal = BitBlt(FormDC, LocX, i, BlockWidth, Abs(HowMuch), 0, 0, 0, BLACKNESS)
Else
RetVal = BitBlt(FormDC, LocX, i + BlockHeight - HowMuch, BlockWidth, Abs(HowMuch), 0, 0, 0, BLACKNESS)
End If
Next i
End Sub


Private Sub Form_KeyPress(KeyAscii As Integer)
Unload Me
End Sub


Private Sub Form_Load()
NumBlocksX = 8 'Important: Value must be an even divisor of horizontal resolution
NumBlocksY = 5 'Important: Value must be an even divisor of vertical resolution

Shuffle.Width = Screen.Width 'Make form the size of the screen
Shuffle.Height = Screen.Height
Shuffle.Left = 0
Shuffle.Top = 0

BlockWidth = Shuffle.ScaleWidth \ NumBlocksX 'Figure width and height of blocks
BlockHeight = Shuffle.ScaleHeight \ NumBlocksY

ScreenhWnd = GetDesktopWindow() 'Get hWnd to Desktop
ScreenDC = GetDC(ScreenhWnd) 'Get hDC of Desktop

RetVal = BitBlt(Shuffle.hDC, 0, 0, Shuffle.ScaleWidth, Shuffle.ScaleHeight, ScreenDC, 0, 0, SRCCOPY)
RetVal = ReleaseDC(ScreenhWnd, ScreenDC) 'Release DC

'Shuffle.Show 'Show the form
Shuffle.AutoRedraw = 0 'AutoRedraw no longer needed
FormDC = Shuffle.hDC 'Get new DC, now that AutoRedraw is off

'Draw vertical lines

For i = 0 To NumBlocksX - 1
Shuffle.Line (i * BlockWidth, 0)-(i * BlockWidth, Shuffle.ScaleHeight)
Next i

'Draw horizontal lines

For i = 0 To NumBlocksY - 1
Shuffle.Line (0, i * BlockHeight)-(Shuffle.ScaleWidth, i * BlockHeight)
Next i

'Choose and display blank square

Randomize

BlankX = Int(Rnd * NumBlocksX)
BlankY = Int(Rnd * NumBlocksY)
RetVal = BitBlt(FormDC, BlankX * BlockWidth, BlankY * BlockHeight, BlockWidth, BlockHeight, 0, 0, 0, BLACKNESS)

'Initialize previous location (first move doesn't matter)

LastX = -1
LastY = -1

While -1
Select Case Int(Rnd * 4)
Case 0
If (BlankX > 0) And (LastX <> BlankX - 1) Then MoveX (-4)
Case 1
If (BlankX < (NumBlocksX - 1)) And (LastX <> BlankX + 1) Then MoveX (4)
Case 2
If (BlankY > 0) And (LastY <> BlankY - 1) Then MoveY (-1)
Case 3
If (BlankY < (NumBlocksY - 1)) And (LastY <> BlankY + 1) Then MoveY (1)
End Select

RetVal = DoEvents() 'Process pending events
Sleep 50
If Not Shuffle.Visible Then Exit Sub
Wend
End Sub



Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Unload Me
End Sub
Mohoman 2002-04-17
  • 打赏
  • 举报
回复
等你回家了帮我看看可以吗??

谢谢 uguess(uguess)
uguess 2002-04-17
  • 打赏
  • 举报
回复
事实上,就是这么简单!
uguess 2002-04-17
  • 打赏
  • 举报
回复
呵呵,这没办法了,我是在网吧,没有VB....

不过如果你对API函数不了解,那VB用起来可就...
Mohoman 2002-04-17
  • 打赏
  • 举报
回复
你说的简单大哥,你用一个具体的程序来演示一下如何
最好是写出来一个用户控件,我对这个api不理解
uguess 2002-04-17
  • 打赏
  • 举报
回复
这有什么,只要是用BitBlt函数,指明分割的长度,然后分别来画就可以了!

Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (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

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧