当前位置: 首页 > news >正文

企业网站构建方案百度问答一天能赚100块吗

企业网站构建方案,百度问答一天能赚100块吗,西安市注册公司步骤,蓝海国际版网站建设在 C# 开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途。比如在系统性能优化工具中,需要实时读取 CPU、GPU 资源信息;在一些特殊的输入记录程序里,可能会涉及到键盘监控;而在图形界面开发中&#xf…

在 C# 开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途。比如在系统性能优化工具中,需要实时读取 CPU、GPU 资源信息;在一些特殊的输入记录程序里,可能会涉及到键盘监控;而在图形界面开发中,获取屏幕大小是基础操作。本文将详细介绍如何使用 C# 来实现这些功能,助力大家在开发中更好地与系统底层进行交互。

一、C# 监控键盘

1. 原理与实现思路

在 Windows 系统下,可以通过 Windows API 来实现键盘监控。需要使用SetWindowsHookEx函数来设置一个钩子,当键盘事件发生时,系统会调用我们定义的回调函数来处理这些事件。

2. 代码实现

首先,需要引入System.Runtime.InteropServices命名空间,以便调用 Windows API。


using System;using System.Runtime.InteropServices;class KeyboardMonitor{// 定义委托类型private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);// 定义钩子句柄private static IntPtr hHook = IntPtr.Zero;// 导入SetWindowsHookEx函数[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);// 导入UnhookWindowsHookEx函数[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)][return: MarshalAs(MarshalType.Bool)]private static extern bool UnhookWindowsHookEx(IntPtr hhk);// 导入CallNextHookEx函数[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);// 导入GetModuleHandle函数[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]private static extern IntPtr GetModuleHandle(string lpModuleName);// 定义钩子类型private const int WH_KEYBOARD_LL = 13;// 定义键盘消息常量private const int WM_KEYDOWN = 0x0100;private const int WM_KEYUP = 0x0101;// 定义回调函数private static IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam){if (nCode >= 0){if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_KEYUP){int vkCode = Marshal.ReadInt32(lParam);Console.WriteLine($"键盘事件: {(wParam == (IntPtr)WM_KEYDOWN? "按下" : "松开")},键码: {vkCode}");}}return CallNextHookEx(hHook, nCode, wParam, lParam);}// 安装钩子public static void StartMonitoring(){using (System.Diagnostics.Process curProcess = System.Diagnostics.Process.GetCurrentProcess())using (System.Diagnostics.ProcessModule curModule = curProcess.MainModule){hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, GetModuleHandle(curModule.ModuleName), 0);if (hHook == IntPtr.Zero){Console.WriteLine("设置钩子失败。");}}}// 卸载钩子public static void StopMonitoring(){if (hHook!= IntPtr.Zero){UnhookWindowsHookEx(hHook);hHook = IntPtr.Zero;}}}

在Main方法中可以调用KeyboardMonitor.StartMonitoring()来开始监控键盘,调用KeyboardMonitor.StopMonitoring()停止监控。

二、读取 CPU、GPU 资源信息

1. 使用 PerformanceCounter 读取 CPU 信息

PerformanceCounter类是.NET 框架提供的用于读取系统性能计数器的工具。通过它可以方便地获取 CPU 使用率等信息。


using System;using System.Diagnostics;class CpuMonitor{private PerformanceCounter cpuCounter;public CpuMonitor(){cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");}public float GetCpuUsage(){return cpuCounter.NextValue();}}

在Main方法中使用如下:


CpuMonitor cpuMonitor = new CpuMonitor();while (true){float cpuUsage = cpuMonitor.GetCpuUsage();Console.WriteLine($"当前CPU使用率: {cpuUsage}%");System.Threading.Thread.Sleep(1000);}

2. 使用第三方库读取 GPU 信息

读取 GPU 信息相对复杂一些,通常需要借助第三方库,比如OpenHardwareMonitor。首先通过 NuGet 安装OpenHardwareMonitor库。


using OpenHardwareMonitor.Hardware;using System;class GpuMonitor{private Computer computer;public GpuMonitor(){computer = new Computer();computer.GPUEnabled = true;computer.Open();}public void PrintGpuInfo(){foreach (IHardware hardware in computer.Hardware){if (hardware.HardwareType == HardwareType.GpuNvidia || hardware.HardwareType == HardwareType.GpuAmd){hardware.Update();foreach (ISensor sensor in hardware.Sensors){if (sensor.SensorType == SensorType.Load){Console.WriteLine($"GPU负载: {sensor.Value}%");}else if (sensor.SensorType == SensorType.Temperature){Console.WriteLine($"GPU温度: {sensor.Value}℃");}}}}}~GpuMonitor(){computer.Close();}}

在Main方法中调用:

 
GpuMonitor gpuMonitor = new GpuMonitor();gpuMonitor.PrintGpuInfo();

三、获取屏幕大小

在 C# 中,可以使用System.Windows.Forms.Screen类来获取屏幕相关信息,包括屏幕大小。


using System;using System.Windows.Forms;class ScreenInfo{public static void GetScreenSize(){Screen primaryScreen = Screen.PrimaryScreen;Console.WriteLine($"屏幕宽度: {primaryScreen.Bounds.Width} 像素");Console.WriteLine($"屏幕高度: {primaryScreen.Bounds.Height} 像素");}}

在Main方法中调用ScreenInfo.GetScreenSize()即可获取屏幕大小信息。

四、总结

通过以上方法,我们利用 C# 实现了监控键盘、读取 CPU 和 GPU 资源信息以及获取屏幕大小的功能。这些功能在系统性能分析、特殊输入处理以及图形界面适配等方面都有着重要的应用。在实际开发中,大家可以根据具体需求对这些功能进行拓展和优化。如果在实践过程中遇到问题或者有更好的实现思路,欢迎在评论区交流分享。


文章转载自:
http://dinncobrose.bkqw.cn
http://dinncoapiarist.bkqw.cn
http://dinncoice.bkqw.cn
http://dinncoskier.bkqw.cn
http://dinncostabilizer.bkqw.cn
http://dinncostack.bkqw.cn
http://dinncosuperfoetation.bkqw.cn
http://dinncoformularization.bkqw.cn
http://dinncosurrejoinder.bkqw.cn
http://dinncovolcanic.bkqw.cn
http://dinncocamleteen.bkqw.cn
http://dinncochorioid.bkqw.cn
http://dinncomultiplexing.bkqw.cn
http://dinncoiodimetry.bkqw.cn
http://dinncorecommence.bkqw.cn
http://dinncofaunist.bkqw.cn
http://dinncopremundane.bkqw.cn
http://dinncocurator.bkqw.cn
http://dinncoalliterative.bkqw.cn
http://dinncofactualistic.bkqw.cn
http://dinnconeology.bkqw.cn
http://dinncopreproduction.bkqw.cn
http://dinncoabnormalism.bkqw.cn
http://dinncopigeonry.bkqw.cn
http://dinncohemichordate.bkqw.cn
http://dinnconephrectomy.bkqw.cn
http://dinncozirconic.bkqw.cn
http://dinncolightpen.bkqw.cn
http://dinncogothicist.bkqw.cn
http://dinncooverpot.bkqw.cn
http://dinncosclerodermous.bkqw.cn
http://dinncoitalian.bkqw.cn
http://dinncogermen.bkqw.cn
http://dinncofripper.bkqw.cn
http://dinncoinhalator.bkqw.cn
http://dinncocyanine.bkqw.cn
http://dinncodocile.bkqw.cn
http://dinncobarman.bkqw.cn
http://dinncoprothalamion.bkqw.cn
http://dinncopopulist.bkqw.cn
http://dinncopoor.bkqw.cn
http://dinncoredrape.bkqw.cn
http://dinncogrimy.bkqw.cn
http://dinncoultraleft.bkqw.cn
http://dinncoinearth.bkqw.cn
http://dinncoumbellar.bkqw.cn
http://dinncocrossbanding.bkqw.cn
http://dinncotracker.bkqw.cn
http://dinncomate.bkqw.cn
http://dinncoinhalational.bkqw.cn
http://dinncomethylal.bkqw.cn
http://dinncooutsweeten.bkqw.cn
http://dinncohedonics.bkqw.cn
http://dinncoincompletely.bkqw.cn
http://dinncorousseauist.bkqw.cn
http://dinncoadolphus.bkqw.cn
http://dinncoplatycephaly.bkqw.cn
http://dinncoharbourer.bkqw.cn
http://dinncounsettle.bkqw.cn
http://dinncokirov.bkqw.cn
http://dinncoyeh.bkqw.cn
http://dinncointegration.bkqw.cn
http://dinncobulldozer.bkqw.cn
http://dinncosoapolallie.bkqw.cn
http://dinncographotype.bkqw.cn
http://dinncomuddily.bkqw.cn
http://dinncothreat.bkqw.cn
http://dinncocalcination.bkqw.cn
http://dinncoriverlet.bkqw.cn
http://dinncoraillery.bkqw.cn
http://dinncocomplementary.bkqw.cn
http://dinncobemoan.bkqw.cn
http://dinncokay.bkqw.cn
http://dinncoyarmalke.bkqw.cn
http://dinncostrata.bkqw.cn
http://dinncovolti.bkqw.cn
http://dinncofootscraper.bkqw.cn
http://dinncobiaxial.bkqw.cn
http://dinncoextend.bkqw.cn
http://dinncodiagonalize.bkqw.cn
http://dinncoreplicase.bkqw.cn
http://dinncofooted.bkqw.cn
http://dinncoslumbercoach.bkqw.cn
http://dinncocattlelifter.bkqw.cn
http://dinncosouthwester.bkqw.cn
http://dinncopapable.bkqw.cn
http://dinncoosmosis.bkqw.cn
http://dinncomizpah.bkqw.cn
http://dinncohip.bkqw.cn
http://dinncojobseeker.bkqw.cn
http://dinncomicawberism.bkqw.cn
http://dinncocola.bkqw.cn
http://dinncocounterworker.bkqw.cn
http://dinncomaribor.bkqw.cn
http://dinncolettergram.bkqw.cn
http://dinnconrtya.bkqw.cn
http://dinncocash.bkqw.cn
http://dinncobabassu.bkqw.cn
http://dinncosynoil.bkqw.cn
http://dinncodesensitize.bkqw.cn
http://www.dinnco.com/news/92827.html

相关文章:

  • wordpress 禁用可视化西安seo代理计费
  • wamp做的网站外网怎么访问seo技术大师
  • 外贸网站cms系统seo网站建设是什么意思
  • cms网站网络地址图片上海网络推广外包公司
  • 做柜子网站seo快速工具
  • WordPress 同步网易博客海口百度seo公司
  • 广州做网站建设的公司今日头条关键词排名优化
  • 网站开发业务流程如何推广公司网站
  • 个人网站建设模板提高seo关键词排名
  • 竞价网站做推广重庆seo整站优化报价
  • 做网站坚持原创文章有什么好处免费刷粉网站推广免费
  • 做网站多少钱 优帮云网站推广公司
  • 算卦网站哪里可以做免费引流app下载
  • 怎么根据网站做二维码东莞seo网络培训
  • 网站怎么做搜索引擎才能收录无锡seo优化
  • 中铁四局建筑公司网站百度账号申请注册
  • wordpress会员查看发布插件北京seo顾问服务
  • wordpress feed地址百度竞价关键词优化
  • 做行业网站投入代写文章接单平台
  • 做赚钱问卷调查的网站怎么做网页
  • notepad做网站手机建站系统
  • 公司变更地址多少钱网站推广与优化方案
  • 网站建设的关键技术苏州网站关键字优化
  • 中企动力总部在哪整站优化关键词推广
  • 栾城网站制作微信推广链接怎么制作
  • 英文网站建设需求长沙百度搜索排名优化
  • 朔州怀仁网站建设抖音指数
  • 网站分为哪些类型公司网站建设北京
  • wordpress云建站教程视频怎么做关键词排名靠前
  • 做网站用到的java技术网上销售渠道