每周源代码45-Windows 7和Windows XP上的踢屁股

2024-01-26 02:59

本文主要是介绍每周源代码45-Windows 7和Windows XP上的踢屁股,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

I really advocate folks reading as much source as they can because you become a better writer by reading as much as writing. That's the whole point of the Weekly Source Code - reading code to be a better developer.

我真的提倡人们阅读尽可能多的资料,因为您会通过阅读和写作而成为更好的作家。 这就是每周源代码的重点-阅读代码以成为一名更好的开发人员。

There's a very cool developer context going on right now called "Code7." If you code a Windows 7 application between now and October 7 you could win a giant bag of money and/or a trip to PDC.

现在有一个非常酷的开发人员上下文称为“ Code7” 。 如果您在现在到10月7日之间编写Windows 7应用程序代码,则可能会赢得一大笔钱和/或去PDC。

There's a pile of new APIs in Windows 7 (as well as existing and useful Vista APIs) like these:

Windows 7中有许多新的API (以及现有的和有用的Vista API),如下所示:

  • Windows 7 Taskbar Integration

    Windows 7任务栏集成
  • Transactional File System

    交易文件系统
  • I/O Optimization

    I / O优化
  • Event Tracing for Windows (ETW)

    Windows事件跟踪(ETW)
  • Windows 7 Libraries

    Windows 7库
  • Windows 7 Sensor and Location Platform

    Windows 7传感器和定位平台
  • Aero Glass

    航空玻璃

In some of these instances, there isn't hardware (yet) for things like Ambient Light Sensors. One dude has taken a Webcam and hooked it into the Windows 7 Sensors API and made a program to dim his monitors with the new Monitor Configuration API*. He might even make it turn off his machine when he walks away.

在某些情况下,还没有诸如环境光传感器之类的硬件。 一个家伙花了一个摄像头,将其连接到Windows 7 Sensors API中,并制作了一个程序,使用新的Monitor Configuration API *对其显示器进行调光。 他走开时甚至可能关闭机器。

XP2Win7 -Windows 7示例代码/应用程序 (XP2Win7 - Windows 7 Sample Code/Application)

I've been checking out what sample applications there are to start learning about Windows 7. The coolest so far as been the "PhotoView" application. Don't sweat the fact it's YAPA (Yet Another Photo Application) and consider it a loosely confederated collection of samples.

我一直在研究有哪些示例应用程序可以开始学习Windows7。到目前为止,最酷的是“ PhotoView”应用程序。 不要大惊小怪,它是YAPA(又是另一张照片应用程序),并认为它是松散联盟的样本集合。

MainWindow

What's cool about this application is that it works on Windows XP and Windows Vista and Windows 7. This may be obvious and even a silly statement to you, Dear Reader, but it's a nice reminder that and app can be awesome on all three platforms. 99% of the apps that I use work great on Windows 7. Sure, some drivers and wacky VPN things will need to be updated, but it's comforting to me to know I can write an app for Windows that will, um, work on Windows. ;)

该应用程序最酷的地方是它可以在Windows XP,Windows Vista和Windows 7上运行。 亲爱的读者,这对您来说可能很明显,甚至是一个愚蠢的声明,但很高兴地提醒您,在三个平台上,and app都很棒。 我使用的应用程序中的99%可以在Windows 7上很好地运行。当然,某些驱动程序和古怪的VPN东西需要进行更新,但是让我感到欣慰的是,我可以为Windows编写可以在Windows上运行的应用程序。 ;)

This PhotoView application, also called XP2Win7 is written managed code and uses plugins to "light up on up-level platforms." That's fancy Microsoft talk that means if your operating system has a feature the app will detect it and use it.

这个PhotoView应用程序也称为XP2Win7,是编写的托管代码,并使用插件“在高级平台上点亮”。 微软的话很花哨,这意味着如果您的操作系统具有某个功能,则该应用将检测到并使用它。

There's a great overview Word Document that explains the app and how it is written. The MSI will install the app, then optionally the source in ~\MyDocuments\Xp2Win7 if you have trouble finding it. You'll need Visual C++ if you want to build a few parts...just read the readme. It's a pretty extraordinarily broad sample with examples on how to make MMC ReportViewer snapins, delayed services, register scheduled tasks, piles.

有一个很好的概述Word Document,它解释了该应用程序及其编写方式。 MSI将安装该应用程序,如果找不到它,则可以选择安装〜\ MyDocuments \ Xp2Win7中的源。 如果要构建一些部分,则需要Visual C ++。只需阅读自述文件。 这是一个非常广泛的示例,其中包含有关如何制作MMC ReportViewer管理单元,延迟服务,注册计划任务,堆的示例。

(Unfortunately the guys that wrote this didn't use MEF for their plugin model, but I'll talk to them. It would allow them to remove a lot of boilerplate plugin monkey code.)

(不幸的是,编写此代码的人并未在他们的插件模型中使用MEF ,但我会与他们交谈。这将使他们删除许多样板插件猴子代码。)

It uses the Windows API Code Pack (I talk about this below) to do a lot of its work. Here's a few fun parts.

它使用Windows API代码包(我将在下面讨论)完成许多工作。 这里有一些有趣的部分。

任务栏跳转列表 (TaskBar JumpLists)

When the app is run under Windows 7 it includes "jumplists" when you right-click (or swipe-up) on the taskbar button:

在Windows 7下运行该应用程序时,在任务栏按钮上单击鼠标右键(或向上滑动)时,它会包含“ jumplists”:

This is easily added with the Taskbar API. Notice the multiple categories, user tasks, recent items, and custom categories.

可通过任务栏API轻松添加。 请注意多个类别,用户任务,近期项目和自定义类别。

Taskbar.JumpList.CustomCategories.Clear();
Taskbar.JumpList.UserTasks.Clear();
Taskbar.JumpList.KnownCategoryToDisplay = KnownCategoryType.Recent;

CustomCategory allAlbumsCategory = new CustomCategory("All Albums");
//...snip out enumerating of the filesystem to get photo albums
Taskbar.JumpList.CustomCategories.Add(allAlbumsCategory);

Taskbar.JumpList.UserTasks.Add(
new JumpListLink()
{
Title = "Reset configuration",
Path = typeof(XP2Win7.VistaPlugins.ConfigurationResetter.Program).Assembly.Location,
Arguments = XP2Win7.VistaPlugins.ConfigurationResetter.Program.ResetCommand
});
Taskbar.JumpList.UserTasks.Add(
new JumpListLink
{
Title = "Launch indexing task",
Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Constants.ServiceCommandLine),
Arguments = Constants.ServiceAsTaskCommandLine
});
Taskbar.JumpList.UserTasks.Add(
new JumpListLink { Title = "Open albums directory", Path = Host.UserConfiguration.AlbumRepositoryPath });

Taskbar.JumpList.RefreshTaskbarList();
Windows 7库 (Windows 7 Libraries)

You can also add Windows 7 Libraries for your application:

您还可以为应用程序添加Windows 7库:

//Create new shell library under the default Libraries
using (ShellLibrary library = new ShellLibrary("XP2Win7", true))
{
library.LibraryType = LibraryFolderType.Pictures;
library.IconResourceId = GetPictureLibraryIcon(); //Set the same Icon as the Picture library
library.IsPinnedToNavigationPane = true;


foreach (string folderPath in GetPicturesFolders())
{
library.Add(folderPath);
}

library.ShowManageLibraryUI(Application.Current.MainWindow,
"Manage the XP2Win7 library", "You can manualy add or remove folders",
true);
}
用户访问控制(UAC) (User Access Control (UAC))

Windows Vista and Windows 7 both include User Access Control (UAC). You'll recognize the little shield icon next to a button that will require a prompt from the user in the dialog below.

Windows Vista和Windows 7均包含用户访问控制(UAC)。 您会在按钮旁边看到一个小的盾牌图标,该图标要求用户在下面的对话框中进行提示。

Application reconfiguration

Their application uses this in a few places. First, can we even show the little shield? We only want to do that if UAC is enabled:

他们的应用程序在一些地方使用了它。 首先,我们什至可以显示小盾牌吗? 我们只想在启用UAC的情况下这样做:

protected override BitmapSource BitmapSource
{
get
{
if (UacHelpers.UserAccountControl.IsUacEnabled)
{
return Microsoft.SDK.Samples.VistaBridge.Library.StockIcons.StockIcons.Shield;
}
else
{
return ImageFromResource(Assembly.GetExecutingAssembly(),
"UserAccountControl.StartService-128x128.png");
}
}
}

Then, if they do click the button, and we want to launch some process as Administrator, we'll need to call a special API to do that. This is mean easy by helper APIs.

然后,如果他们确实单击了按钮,并且我们想以管理员身份启动某些过程,则需要调用特殊的API来执行此操作。 通过助手API可以轻松实现。

if (UacHelpers.UserAccountControl.IsUacEnabled || !UacHelpers.UserAccountControl.IsUserAdmin)
{
UacHelpers.UserAccountControl.CreateProcessAsAdmin(
typeof(ServiceStarter.Program).Assembly.Location, "XP2Win7ImageDataService");
}
else
{
Process.Start(typeof(ServiceStarter.Program).Assembly.Location, "XP2Win7ImageDataService");
}

Pretty slick and easy to code. The Windows API Code Pack makes all these APIs and dozens more easy for managed code developers (C#, VB, and everyone else.)

非常光滑,易于编码。 Windows API代码包使所有这些API以及托管代码开发人员(C#,VB和其他所有人)都更容易使用。

Windows API代码包 (Windows API Code Pack)

Another great pile of Windows sample code is the Windows API Code Pack. This thing is a gold mine of samples and they are all in C# and VB. There's like 20+ samples. Here's a few:

Windows示例代码的另一堆是Windows API代码包。 这个东西是样本的金矿它们都在C#和VB中。 大约有20多个样本。 这里有一些:

能源管理 (Power Management )

It's nice if your app knows the power status of the machine it's on and avoid doing crazy stuff if it's on batteries.

如果您的应用知道正在运行的机器的电源状态,并且如果用电池供电,请避免做疯狂的事情,这很好。

Power Management

You can get all sorts of great power-related info:

您可以获得各种与功率相关的信息:

private void GetPowerSettings()
{
settings.PowerPersonality = PowerManager.PowerPersonality.ToString();
settings.PowerSource = PowerManager.PowerSource.ToString();
settings.BatteryPresent = PowerManager.IsBatteryPresent;
settings.UpsPresent = PowerManager.IsUpsPresent;
settings.MonitorOn = PowerManager.IsMonitorOn;
settings.MonitorRequired = PowerManager.MonitorRequired;

if (PowerManager.IsBatteryPresent)
{
settings.BatteryShortTerm = PowerManager.IsBatteryShortTerm;
settings.BatteryLifePercent = PowerManager.BatteryLifePercent;

BatteryState batteryState = PowerManager.GetCurrentBatteryState();

string batteryStateStr = string.Format(
"ACOnline: {1}{0}Max Charge: {2} mWh{0}Current Charge: {3} mWh{0}Discharge Rate: {4} {0}Estimated Time Remaining: {5}{0}Suggested Critical Battery Charge: {6} mWh{0}Suggested Battery Warning Charge: {7} mWh{0}",
Environment.NewLine,
batteryState.ACOnline,
batteryState.MaxCharge,
batteryState.CurrentCharge,
batteryState.ACOnline == true ? "N/A" : batteryState.DischargeRate.ToString() + " mWh",
batteryState.ACOnline == true ? "N/A" : batteryState.EstimatedTimeRemaining.ToString(),
batteryState.SuggestedCriticalBatteryCharge,
batteryState.SuggestedBatteryWarningCharge
);

settings.BatteryState = batteryStateStr;
}
}

There's also lots of power-related events you can be notified of:

您还会收到许多与电源有关的事件的通知:

PowerManager.IsMonitorOnChanged += new EventHandler(MonitorOnChanged);
PowerManager.PowerPersonalityChanged += new EventHandler(
PowerPersonalityChanged);
PowerManager.PowerSourceChanged += new EventHandler(PowerSourceChanged);
if (PowerManager.IsBatteryPresent)
{
PowerManager.BatteryLifePercentChanged += new EventHandler(BatteryLifePercentChanged);

// Set the label for the battery life
SetLabelButtonStatus(batteryLifePercentLabel, string.Format("{0}%", PowerManager.BatteryLifePercent.ToString()));
}

PowerManager.SystemBusyChanged += new EventHandler(SystemBusyChanged);
股票图标 (Stock Icons)

A lot of folks don't realize that there's a pile of stock icons that are available in Windows and you can access them programmatically.

许多人没有意识到Windows中有大量可用的库存图标,您可以通过编程方式访问它们。

 

That means if you need the stock icon for a BluRayRom or a ZipFile, you can just ask for it.

这意味着,如果您需要用于BluRayRom或ZipFile的股票图标,则可以要求它。

任务栏进度 (Task Bar Progress)

One of the nicest subtle features of Win7 is that if you've got a Progress Bar doing something in your application you can make its progress known in the Taskbar icon itself. This is fantastic for long-running processes like file copies, etc.

Win7最好的微妙功能之一就是,如果您在应用程序中有一个进度条,您可以在任务栏图标本身中显示其进度。 这对于长时间运行的进程(如文件副本等)来说非常理想。

Notice the progress bar in this application and the taskbar button in the bottom right reflects it.

注意此应用程序中的进度条,右下角的任务栏按钮将其反映出来。

There's also icon/image overlays and other nice touches. Even better, this is epic-easy:

还有图标/图像覆盖和其他漂亮的感觉。 更好的是,这很容易:

// When the user changes the trackBar value,
// update the progress bar in our UI as well as Taskbar
progressBar1.Value = trackBar1.Value;

TaskbarManager.Instance.SetProgressValue(trackBar1.Value, 100);

Since your app can have multiple progress bars, you have to manually decide what you want the taskbar progress to look like.

由于您的应用程序可以具有多个进度条,因此您必须手动确定任务栏进度的外观。

核心帮手 (Core Helpers)

Finally there's some nice "CoreHelpers" to make your applications easy to read and run on XP, Vista and Win7 at the same time:

最后,有一些不错的“ CoreHelpers”使您的应用程序易于阅读,并且可以同时在XP,Vista和Win7上运行:

//example
if (CoreHelpers.RunningOnXP()) { ... }
//example
if (CoreHelpers.ThrowifNotWin7() { ... }
// and all the others you'd expect for XP, Vista, Win7

I've just touched the surface of these samples. If you're doing Windowe Client development be sure to check out http://windowsclient.net/ and http://www.msdn.com/windows for more and start writing your application for the https://www.code7contest.com.

我刚刚摸过这些样品的表面。 如果您正在进行Windowe Client开发,请确保查看http://windowsclient.net/http://www.msdn.com/windows了解更多信息,然后开始为https://www.code7contest编写应用程序。 com

1. Get Windows 7 and the SDK

1.获取Windows 7和SDK

  • Windows SDK

    Windows SDK

2. Develop and Test Your Application

2.开发和测试您的应用程序

  • Get the Windows API Code Pack

    获取Windows API代码包

  • Learn about Application Compatibility

    了解有关应用程序兼容性的信息

  • Read the Windows 7 Application Quality Cookbook

    阅读Windows 7应用程序质量手册

  • Download the Windows 7 Training Kit for Developers

    下载针对开发人员的Windows 7培训工具包

3. Get the Windows 7 Logo

3.获取Windows 7徽标

  • Learn about the Pledge Program

    了解有关承诺计划的信息

  • Get the Windows 7 Logo

    获取Windows 7徽标

4. Light Up Your Application with Windows 7

4.使用Windows 7点亮您的应用程序

  • Read the Windows 7 Developer Guide

    阅读Windows 7开发人员指南

  • Learn how to Develop for Windows 7

    了解如何针对Windows 7开发

Related Links

相关链接

  • Windows 7 Developer Guide

    Windows 7开发人员指南

  • Windows API Code Pack

    Windows API代码包

  • Less Virtual, More Machine - Windows 7 and the magic of Boot to VHD

    虚拟机更少,机器更多-Windows 7和启动VHD的魔力

  • Top 10 Tips Working Developers Should Know about Windows 7

    开发人员应该了解的Windows 10十大技巧

  • Windows 7 Easy Upgrade Path Truth Table/Chart

    Windows 7简易升级路径真值表/图表

  • Windows 7 - Seamless Apps in Windows Virtual PC (Virtual XP) and Application Compatibility

    Windows 7-Windows Virtual PC(Virtual XP)中的无缝应用程序和应用程序兼容性

  • Step-By-Step: Turning a Windows 7 DVD or ISO into a Bootable VHD Virtual Machine

    循序渐进:将Windows 7 DVD或ISO转换为可引导的VHD虚拟机

* Note the (lightweight) parameter I passed into this MSDN URL. Check out the new "Lightweight" MSDN Library and give the team feedback on the site!

*请注意我传递给此MSDN URL的(lightweight)参数。 查看新的“轻量级” MSDN库,并在网站上为团队提供反馈

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-45-kicking-butt-on-windows-7-and-windows-xp

这篇关于每周源代码45-Windows 7和Windows XP上的踢屁股的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/645444

相关文章

Python+PyQt5开发一个Windows电脑启动项管理神器

《Python+PyQt5开发一个Windows电脑启动项管理神器》:本文主要介绍如何使用PyQt5开发一款颜值与功能并存的Windows启动项管理工具,不仅能查看/删除现有启动项,还能智能添加新... 目录开篇:为什么我们需要启动项管理工具功能全景图核心技术解析1. Windows注册表操作2. 启动文件

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Windows系统宽带限制如何解除?

《Windows系统宽带限制如何解除?》有不少用户反映电脑网速慢得情况,可能是宽带速度被限制的原因,只需解除限制即可,具体该如何操作呢?本文就跟大家一起来看看Windows系统解除网络限制的操作方法吧... 有不少用户反映电脑网速慢得情况,可能是宽带速度被限制的原因,只需解除限制即可,具体该如何操作呢?本文

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

Windows 上如果忘记了 MySQL 密码 重置密码的两种方法

《Windows上如果忘记了MySQL密码重置密码的两种方法》:本文主要介绍Windows上如果忘记了MySQL密码重置密码的两种方法,本文通过两种方法结合实例代码给大家介绍的非常详细,感... 目录方法 1:以跳过权限验证模式启动 mysql 并重置密码方法 2:使用 my.ini 文件的临时配置在 Wi

Windows Docker端口占用错误及解决方案总结

《WindowsDocker端口占用错误及解决方案总结》在Windows环境下使用Docker容器时,端口占用错误是开发和运维中常见且棘手的问题,本文将深入剖析该问题的成因,介绍如何通过查看端口分配... 目录引言Windows docker 端口占用错误及解决方案汇总端口冲突形成原因解析诊断当前端口情况解

Redis在windows环境下如何启动

《Redis在windows环境下如何启动》:本文主要介绍Redis在windows环境下如何启动的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis在Windows环境下启动1.在redis的安装目录下2.输入·redis-server.exe

Windows Server服务器上配置FileZilla后,FTP连接不上?

《WindowsServer服务器上配置FileZilla后,FTP连接不上?》WindowsServer服务器上配置FileZilla后,FTP连接错误和操作超时的问题,应该如何解决?首先,通过... 目录在Windohttp://www.chinasem.cnws防火墙开启的情况下,遇到的错误如下:无法与

Python解析器安装指南分享(Mac/Windows/Linux)

《Python解析器安装指南分享(Mac/Windows/Linux)》:本文主要介绍Python解析器安装指南(Mac/Windows/Linux),具有很好的参考价值,希望对大家有所帮助,如有... 目NMNkN录1js. 安装包下载1.1 python 下载官网2.核心安装方式3. MACOS 系统安

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想