; Draw image on desktop with GDI Plus If OpenLibrary(0, "gdiplus.dll") = 0 MessageRequester("Error","Required component gdiplus.dll is not found. Please install it and retry ", #MB_ICONERROR) End EndIf Structure GdiplusStartupInput GdiPlusVersion.i *DebugEventCallback.DebugEventProc SuppressBackgroundThread.i SuppressExternalCodecs.i EndStructure Structure RectF x.f y.f width.f height.f EndStructure Structure ColorMatrix ;{ M00.f M01.f M02.f M03.f M04.f M10.f M11.f M12.f M13.f M14.f M20.f M21.f M22.f M23.f M24.f M30.f M31.f M32.f M33.f M34.f M40.f M41.f M42.f M43.f M44.f EndStructure ;} Global ncolor.ColorMatrix With ncolor \M00=1 \M11=1 \M22=1 \M33=1 \M44=1 EndWith #UnitPixel=2 #Count=10 Global Dim Bx.w(#Count),Dim By.w(#Count) Global Dim adx.w(#Count),Dim ady.w(#Count) Global Dim nColors.f(#Count,3) Global D.w=180 Global Dw.w,Dh.w Global m_hdcMemory Global *imgc Import "gdiplus.lib" GdiplusStartup(token, *input.GdiplusStartupInput, output) GdiplusShutdown(token) GdipCreateFromHDC(hdc, *graphics) GdipReleaseDC(*graphics, hdc.l) GdipLoadImageFromFile(filename.p-unicode, *image) GdipCloneImage(*image, *cloneImage) GdipGetImageHeight(*image, *height.integer) GdipGetImageWidth(*image, *width.integer) GdipImageRotateFlip(*image, rotateFlipType) GdipGetImageBounds(*image, *srcRect.RectF, *srcUnit.integer) GdipDrawImage(*graphics, *image, x.f, y.f) GdipDrawImageI(*graphics, *image, x.l, y.l) GdipDrawImageRectI(*graphics, *image, x.l, y.l, width.l, height.l) GdipDrawImageRect(*graphics, *image, x.f, y.f, width.f, height.f) GdipGraphicsClear(*graphics, color) GdipDisposeImage(image) GdipDeleteGraphics(*graphics) GdipCreateImageAttributes(*imageattr) GdipDisposeImageAttributes(*imageattr) GdipResetImageAttributes(*imageattr, type.l) SetColorMatrix(*In_ImageAttr, *In_colorMatrix) GdipSetImageAttributesColorMatrix(*imageattr, type.l, enableFlag.l, *colorMatrix.ColorMatrix, *grayMatrix.ColorMatrix, flags.l) GdipDrawImageRectRect(*graphics, *image, dstx.f, dsty.f, dstwidth.f, dstheight.f, srcx.f, srcy.f, srcwidth.f, srcheight.f, srcUnit.l, *ImageAttributes, *callback, *callbackData) ;GdipSetImageAttributesColorMatrix(*In_ImageAttr, #ColorAdjustTypeDefault, #True, *In_colorMatrix, #Null, #ColorMatrixFlagsDefault) EndImport Declare checkCollision(bub0, bub1) Declare BlitWindow(*image) ExamineDesktops() dw=DesktopWidth(0) dh=DesktopHeight(0) input.GdiplusStartupInput input\GdiPlusVersion = 1.1 GdiplusStartup(@token, @input, #Null) GdipLoadImageFromFile("popobluebig.png", @image) ;GdipCloneImage(image,@imgc) For i=1 To #Count adx(i)=10:ady(i)=10 Next i RandomSeed(Date()) For i=1 To #Count Bx(i)=Random(dw,0) By(i)=Random(dh,0) Next i If ExamineDesktops() OpenWindow(0,0,0,DesktopWidth(0),DesktopHeight(0),"Gdiplus Drawing",#PB_Window_ScreenCentered|#PB_Window_BorderLess) SetWindowLongPtr_(WindowID(0), #GWL_EXSTYLE, #WS_EX_LAYERED|#WS_EX_TRANSPARENT|#WS_EX_TOPMOST) SetWindowPos_(WindowID(0),#HWND_TOPMOST,#Null,#Null,#Null,#Null,#SWP_NOMOVE|#SWP_NOSIZE) Repeat EventID = WindowEvent() Select EventID ;Case #WM_LBUTTONDOWN ; SendMessage_(WindowID(0), #WM_NCLBUTTONDOWN, #HTCAPTION, 0) ; Case #PB_Event_Menu ; If EventMenu()=9 : End : EndIf ; Case #WM_RBUTTONDOWN ; DisplayPopupMenu(0, WindowID(0)) Case #WM_KEYDOWN If EventwParam()=27 End EndIf EndSelect BlitWindow(image) Until EventID = #PB_Event_CloseWindow EndIf End Procedure checkCollision(bub0, bub1) Protected dx.w,dy.w,Dist.w,px1.w,px2.w,py1.w,py2.w,dx2.w,dy2.w,d2.w dx=Abs(Bx(bub1)-Bx(bub0)) dy=Abs(By(bub1)-By(bub0)) dist=Sqr(dx*dx+dy*dy); If dist = 0 dist= 1 EndIf D2=D-Dist If d2>=0 ; p的速度的交换分 py1=(dy* dy) *Ady(bub1)/dist / dist +(dy * dx)*Adx(Bub1)/dist /dist px1=(dx* dx) *Adx(Bub1)/dist / dist +(dx * dy)*Ady(Bub1)/dist /dist ; this的速度的交换分量 py2=(dy* dy) *Ady(Bub0)/dist /dist +(dy * dx)*Adx(Bub0)/dist /dist px2=(dx* dx) *Adx(Bub0)/dist /dist +(dx * dy)*Ady(Bub0)/dist /dist Adx(Bub0)=Adx(Bub0)+px1-px2 Ady(Bub0)=Ady(Bub0)+py1-py2 Adx(Bub1)=Adx(Bub1)-px1+px2 Ady(Bub1)=Ady(Bub1)-py1+py2 EndIf If d2>0 ;移动第二个圆到延长线上去。 dx2=D2 * (Bx(Bub1) -Bx(Bub0)) / Dist dy2=D2 * (By(Bub1) -By(Bub0)) / Dist Bx(Bub1)=Bx(Bub1)+dx2 By(Bub1)=By(Bub1)+dy2 ;//this._pCenter.X -= (float)(dx2 / 2.0); ;//this._pCenter.Y -= (float)(dy2 / 2.0); EndIf EndProcedure Procedure BlitWindow(image) hdcTemp = GetDC_(WindowID(0)) m_hdcMemory = CreateCompatibleDC_(hdcTemp) hBitMap = CreateCompatibleBitmap_(hdcTemp,Dw,Dh) SelectObject_(m_hdcMemory,hBitMap) hdcScreen = GetDC_(WindowID(0)) GdipCreateFromHDC(m_hdcMemory,@gfx) GdipCreateImageAttributes(@attr) For i=1 To #Count If Random(20)<2 nColors(i,0)= Random(20,0)/10-1 nColors(i,1)= Random(20,0)/10-1 nColors(i,2)= Random(20,0)/10-1 EndIf ncolor\M40=nColors(i,0) ncolor\M41=nColors(i,1) ncolor\M42=nColors(i,2) ;ncolor\M33=nColors(i,3);//透明度 0-1 GdipSetImageAttributesColorMatrix(attr,0,#True,ncolor,#NUL,0) For j=2 To #Count checkCollision(i,j) Next j ;GdipDrawImageRectI(gfx, image, Bx(i), By(i),D,D);//Can not work well GdipDrawImageRectRect(gfx,image,Bx(i),By(i),D,D,0,0,D,D,#UnitPixel,attr,#Null,#Null) Bx(i)=Bx(i)+Adx(i) : By(i)=By(i)+Ady(i) If Bx(i)>dw-D Bx(i)=Dw-d Adx(i)=-Adx(i) EndIf If Bx(i)<0 Bx(i)=0 Adx(i)=-Adx(i) EndIf If By(i)>Dh-D By(i)=Dh-D Ady(i)=-Ady(i) EndIf If By(i)<0 By(i)=0 Ady(i)=-Ady(i) EndIf If Adx(i)=0 Adx(i)=6 EndIf If Ady(i)=0 Ady(i)=6 EndIf Next i With sz.SIZE \cx =dw;ImageWidth(image) \cy =dh;ImageHeight(image) EndWith With ContextOffset.Point \x=0 \y=0 EndWith With BlendMode.BLENDFUNCTION \BlendOp=#AC_SRC_OVER ;On the top [0] \BlendFlags=0 \SourceConstantAlpha = 255 \AlphaFormat = #AC_SRC_ALPHA ;every one [1] EndWith UpdateLayeredWindow_(WindowID(0),hdcScreen,0,@sz,m_hdcMemory,@ContextOffset.POINT,$0,@BlendMode,#ULW_ALPHA) GdipDisposeImageAttributes(attr) GdipDeleteGraphics(gfx) GdipReleaseDC(gfx, m_hdcMemory) ReleaseDC_(WindowID(0),hdcTemp) ReleaseDC_(WindowID(0),hdcScreen) DeleteDC_(m_hdcMemory) DeleteObject_(hBitMap) ; Debug hdcScreen ; Debug hdcTemp EndProcedure
标签:Bub1,Bx,image,Plus,dx,dy,GDI,DrawImage,dist From: https://www.cnblogs.com/PBprogram/p/16925434.html