Results 1 to 10 of 10

Thread: Offscreen DCs

  1. #1
    Zaei
    Guest
    Just a quick question. Can you use GetPixel with an offscreen DC?

  2. #2
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Sure, why not?

  3. #3
    Guest
    Well, ive found that it will return -1 if i use a hidden picture box. I'm wondering if it will work if i use an offscreen DC

  4. #4
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    It will.

    If you want to use a picturebox set autoredraw to true..

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Same problem appears for any DC if there's no bitmap selected.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Zaei, just create a DC in memory.
    I do it in the source code I released/posted in the misc.bas module.

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Guest
    Ok, from my experiments, you CANNOT use GetPixel
    with an offscreen DC. You can Blt from one, which I
    tested, but using GetPixel returns 0 (and when the pixel
    referenced is invalid, it returns -1).

  8. #8
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    I made a memory DC and did a BitBlt of the desktop to it.
    Using a For..Next loop with GetPixel() resulted in the colours and not 0 or -1!

  9. #9
    Guest
    Hrmmm.. Post code, please?

  10. #10
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553

    Talking

    Well, here it is my friend:

    Code:
    Option Explicit
      'This project needs 1 picturebox
      Private 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
      Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
      Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
      Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
      Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
      Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
      Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
      Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
      Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    
      Dim W      As Long
      Dim H      As Long
      Dim memDC  As Long
      Dim memBMP As Long
    
    Private Sub Form_Activate()
      Me.Refresh
      BitBlt Me.hdc, 0, 0, W, H, memDC, 0, 0, vbSrcCopy
    End Sub
    
    Private Sub Form_DblClick()
      Unload Me
    End Sub
    
    Private Sub Form_Load()
      W = Screen.Width
      H = Screen.Height
      Picture1.ScaleMode = vbPixels
      Picture1.BackColor = 0
      Me.ScaleMode = vbPixels
      Me.Move 0, 0, W, H
      W = W / Screen.TwipsPerPixelX
      H = H / Screen.TwipsPerPixelY
      Picture1.Move W - Picture1.Width, H - Picture1.Height
      memDC = CreateCompatibleDC(Me.hdc)
      memBMP = CreateCompatibleBitmap(Me.hdc, W, H)
      SelectObject memDC, memBMP
      BitBlt memDC, 0, 0, W, H, GetDC(0), 0, 0, vbSrcCopy 'Capture desktop...
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
      Dim C As Long
      Dim i As Integer
      Static BUSY As Boolean
      
      If (BUSY) Then Exit Sub
      BUSY = True
      
      If (x < Picture1.Width) And (y < Picture1.Height) Then
        For i = 0 To 9
          'Get the colour in MEMORY!!! not from the Form!!!
          C = GetPixel(memDC, x + i, y)
          SetPixelV Picture1.hdc, x + i, y, C
          C = GetPixel(memDC, x + i, y + 1)
          SetPixelV Picture1.hdc, x + i, y + 1, C
        Next i
      End If
      BUSY = False
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      DeleteDC memDC
      DeleteObject memBMP
      Set Form1 = Nothing
      End
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width