利用Windows API實現螢幕取色器

2022-02-05 07:45:56 字數 2637 閱讀 4476

一  效果圖

為了實現這個取色器,在網上查了很多關於取色器的資料,起先是通過winform怎麼製作,後來發現大多數資料都呼叫了windows api,但自己以前從來沒有用過這方面的,又從網上下了windows api 來看,經過多次實踐終於做出了現在這個效果,先感謝下網上那些提供資料的朋友。

效果,如下圖:

二 原理

為了實現整個螢幕取色

1.需要獲取滑鼠當前的位置

2.獲取整個視窗的場景

3.獲取指定點的顏色值

4.判斷按鍵狀態來控制開始結束取色

需要實現這些功能,就必需要呼叫windows api來實現。

在c#中呼叫windows api的方式如下所示:

[dllimport(

"user32.dll")]

public

static

extern

uint

windowfrompoint(

uint

x_point,

uint

y_point);

三 詳細**

關鍵**都已經注釋,就不解釋了。介面**就不給出了很簡單的。

**

using

system;

using

system.collections.generic;

using

system.componentmodel;

using

system.data;

using

system.drawing;

using

system.linq;

using

system.text;

using

system.windows.forms;

using

system.runtime.interopservices;

namespace

getcolor

private

bool

flag

=false

;public

struct

pointapi

public

class

wininfo

//////

初始化色板顏色-白色

///private

void

initpanelcolor()

private

void

txtredvalue_textchanged(

object

sender, eventargs e)

private

void

txtgreenvalue_textchanged(

object

sender, eventargs e)

private

void

txtbluevalue_textchanged(

object

sender, eventargs e)

//////

設定面板顏色

///private

void

setpanelcolor()

private

intlimitnum(

intnum)

else

if(num

<0)

else

}private

void

panelcolor_mouseclick(

object

sender, mouseeventargs e)

private

void

colorform_mousemove(

object

sender, mouseeventargs e)

//////

按esc開始停止取色

///private

void

isgetcolor()

else

if(flag)

}private

void

txtredvalue_keypress(

object

sender, keypresseventargs e)

}private

void

txtgreenvalue_keypress(

object

sender, keypresseventargs e)

}private

void

txtbluevalue_keypress(

object

sender, keypresseventargs e)

}private

void

關於toolstripmenuitem_click(

object

sender, eventargs e)

private

void

介紹toolstripmenuitem_click(

object

sender, eventargs e)}}

利用windows api實現程序通訊(命名管道)

服務端 1.使用api函式createnamedpipe建立與命名管道的連線。2.使用api函式connectnamedpipe等待客戶端的連線。可用這個函式將乙個管道換成同另乙個客戶連線,也就是可以重複呼叫該函式,但必須用disconnectnamedpipe函式斷開之前程序的連線 3.使用api...

利用Windows API判斷檔案共享鎖定狀態

一 概述 鎖是作業系統為實現資料共享而提供的一種安全機制,它使得不同的應用程式,不同的計算機之間可以安全有效地共享和交換資料。要保證安全有效地操作共享資料,必須在相應的操作前判斷鎖的型別,然後才能確定資料是否可讀或可寫,從而為開發出健壯的程式提供切實依據。同樣,在windows中,檔案可以共享模式開...

Windows API實現視窗居中

如下 居中視窗 int scrwidth,scrheight rect rect scrwidth getsystemmetrics sm cxscreen scrheight getsystemmetrics sm cyscreen getwindowrect hwnd,rect setwindo...