用VB編寫一個螢幕顏色拾取器

來源:互聯網
上載者:User
設計狀態下視窗中添加兩個Frame控制項做為容器,加入二個PictureBox控制項,一個PictureClip控制項(其中裝入一個設計好的滑鼠指標Mask圖片),兩個文字框控制項,幾個Label控制項,兩個Command控制項,一個CheckBox控制項。



代碼如下:

Option Explicit

Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
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 StretchBlt Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal Height As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight 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 Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Const SRCCOPY = &HCC0020
Private Const SRCAND = &H8800C6
Private Const SRCPAINT = &HEE0086

Dim MousePos As POINTAPI
Dim oldMousePos As POINTAPI

Private Sub Check1_Click()
'設定頂層視窗
If Check1.Value = 1 Then
SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
Else
SetWindowPos Me.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End If
End Sub

Private Sub Command1_Click()
'開始停止捕捉螢幕
If Command1.Caption = "停止" Then
Command1.Caption = "開始"
Timer1.Enabled = False
Else
Command1.Caption = "停止"
Timer1.Enabled = True
End If
End Sub

Private Sub Command2_Click()
'退出程式
Unload Me
End Sub

Private Sub Form_Activate()
'程式啟動後自動化佈建頂層視窗
Check1.Value = 1
End Sub

Private Sub Timer1_Timer()
Dim WindowDC As Long
Dim Color As Long
Dim r As Integer, b As Integer, g As Integer
GetCursorPos MousePos '擷取滑鼠當前座標
'If MousePos.X = oldMousePos.X And MousePos.Y = oldMousePos.Y Then Exit Sub '若未移動則返回
Frame1.Caption = "座標(" & MousePos.X & "," & MousePos.Y & ")"
oldMousePos = MousePos
WindowDC = GetWindowDC(0) '擷取螢幕的裝置情境
Color = GetPixel(WindowDC, MousePos.X, MousePos.Y) '擷取滑鼠所指點的顏色
'分解RGB顏色值
r = (Color Mod 256)
b = (Int(Color \ 65536))
g = ((Color - (b * 65536) - r) \ 256)
Label1.BackColor = RGB(r, g, b)
Text1.Text = r & "," & g & "," & b
Text2.Text = WebColor(r, g, b)
'將以滑鼠位置為中心的9*9的螢幕映像放大
StretchBlt Picture1.hDC, 0, 0, 73, 73, WindowDC, MousePos.X - 4, MousePos.Y - 4, 9, 9, SRCCOPY
'將一個滑鼠指標用Mask的方法透明的畫到放大的映像中
Picture2.Picture = PictureClip1.GraphicCell(1)
BitBlt Picture1.hDC, 37, 37, 12, 21, Picture2.hDC, 0, 0, SRCAND
Picture2.Picture = PictureClip1.GraphicCell(0)
BitBlt Picture1.hDC, 37, 37, 12, 21, Picture2.hDC, 0, 0, SRCPAINT
'獲得是否按了熱鍵F12
If GetAsyncKeyState(vbKeyF12) <> 0 Then
Timer1.Enabled = False
Command1.Caption = "開始"
End If
End Sub

Private Function WebColor(r As Integer, g As Integer, b As Integer) As String
'將10進位RGB值轉為Web顏色值
WebColor = "#" & INT2HEX(r) & INT2HEX(g) & INT2HEX(b)
End Function

Private Function INT2HEX(Value As Integer) As String
'10進位轉16進位
INT2HEX = Hex(Value)
If Len(INT2HEX) = 1 Then
INT2HEX = "0" & INT2HEX
End If
End Function



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.