关于JavaScript中获取浏览器窗口大小的说明

在JavaScript交互时,我们常常需要获取当前浏览器窗口的大小,从而进行定位元素等操作。不过获取各个版本浏览器窗口大小的方法都略有差异,当然我们可以使用jQuery等兼容多种浏览器的JavaScript框架来获取,但是有时我们又不希望引入第三方的JavaScript库,所以我们只能通过使用原生的JavaScript方法来获取。
以下代码定义了如何通过原生的JavaScript方法来获取浏览器窗口的大小:

// 定义获取窗口大小的方法
(function (window, undefined) {
    // 定义获取窗口高度
    if (typeof (window.getWindowHeight) != "function") {
        window.getWindowHeight = function () {
            var height = false;
            if (window.innerHeight) {
                height = window.innerHeight;
            } else if (document.body && document.body.clientHeight) {
                height = document.body.clientHeight;
            }
            if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
                height = document.documentElement.clientHeight;
            }
            return height;
        };
    }
    // 定义获取窗口宽度
    if (typeof (window.getWindowWidth) != "function") {
        window.getWindowWidth = function () {
            var width = false;
            if (window.innerWidth) {
                width = window.innerWidth;
            } else if (document.body && document.body.clientWidth) {
                width = document.body.clientWidth;
            }
            if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
                width = document.documentElement.clientWidth;
            }
            return width;
        };
    }
})(window);

关于WordPress中去除Jetpack的Open Graph元标签的说明

在WordPress中如果使用了Jetpack的Sharing或者Publicize功能的话,Jetpack会自动地在网站页面上添加Open Graph的元标签。这些标签的作用是让Facebook能够获取页面的信息从而进行分享。但是对于国内而言,由于无法访问Facebook,该功能就失去了作用,那如何才能去掉这些无用的Open Graph元标签呢?

如果有其他WordPress插件处理了Open Graph元标签,那么Jetpack将自动禁用掉自身的Open Graph元标签。

我们可以在所用主题的functions.php文件中或者在所用的其他WordPress插件中添加以下代码来去除Jetpack的Open Graph元标签:

add_filter ( 'jetpack_enable_open_graph', '__return_false' );

更多信息请参阅:Open Graph — Jetpack for WordPress

关于C#中创建自定义形状控件的说明

在C#应用程序编程中有许多界面控件供我们使用,这些控件的形状都是规则的矩形,但是为了美观友好的UI体验,有时我们需要创建一些不规则的自定义形状的控件。
同样在C#中,类型Region是描述控件显示区域的类,类型GraphicsPath则表示一系列相互连接的直线和曲线,通过构造出闭合的不规则的GraphicsPath曲线来创建对应形状区域的Region对象,并设置控件的显示区域为此Region对象,就可以使控件按照GraphicsPath对象描述的形状来显示。
下面示例创建一个中间镂空的圆环控件(当然我们也可以添加其他任何形状的边界到GraphicsPath对象中,从而创建形状更加复杂的自定义控件):

一、创建一个原始的空白窗体Form:
csharp-region-control-1

二、在窗体Form的构造函数中添加如下代码:

// 创建用户控件
UserControl userControl = new UserControl();
// 设置用户控件大小
userControl.Width = 200;
userControl.Height = 200;
// 设置用户控件在父控件中居中显示
userControl.Location = new Point((this.ClientSize.Width - userControl.Width) / 2, (this.ClientSize.Height - userControl.Height) / 2);
// 设置用户控件背景色为红色
userControl.BackColor = Color.Red;
// 创建GraphicsPath对象
GraphicsPath graphicsPath = new GraphicsPath();
// 定义外部圆形区域
Rectangle outBounds = new Rectangle(0, 0, 200, 200);
// 定义内部圆形区域
Rectangle inBounds = new Rectangle(50, 50, 100, 100);
// 添加内外圆形区域到GraphicsPath对象中
graphicsPath.AddEllipse(outBounds);
graphicsPath.AddEllipse(inBounds);
// 设置用户控件Region对象
userControl.Region = new Region(graphicsPath);
// 将用户控件添加到窗体中
this.Controls.Add(userControl);

三、运行窗体Form,其中红色的圆环控件就是我们创建的自定义形状的控件:
csharp-region-control-2

更多信息请参阅:GraphicsPath 类 (System.Drawing.Drawing2D)Region 类 (System.Drawing)

关于C#中获取屏幕图像的说明

获取显示器屏幕当前显示的画面在许多场景中都会用到,比例截图。在C#中我们可以通过Graphics对象获取屏幕图像:

// 获取主屏幕的显示范围
Rectangle bounds = Screen.PrimaryScreen.Bounds;
// 创建大小与主屏幕一直的空图像
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
// 通过图像创建Graphics对象
Graphics graphics = Graphics.FromImage(bitmap);
// 将屏幕图片完整绘制到图像上
graphics.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
// 释放Graphics对象
graphics.Dispose();
// 返回屏幕图像
return bitmap;

获取到屏幕图像后,我们就可以根据需求对该图像进行后续处理了。

关于JavaScript中获取和设置Url参数的说明

在Web编程时,一般我们都只是在服务端获取请求的Url参数,但是在前端交互过程中我们也有可能希望通过JavaScript获取Url中的参数。
我们可以通过以下JavaScript方式获取和设置Url中的参数信息:

一、定义Url参数操作方法:

// 定义获取和设置Url参数的方法
(function (window, undefined) {
    // 定义获取Url参数方法
    if (typeof (window.getUrlParam) != "function") {
        /**
        * url:要获取参数的Url
        * key:要获取参数的键
        * return:返回获取到的参数值
        */
        window.getUrlParam = function (url, key) {
            // 定义默认值
            var value = "";
            // 定位到Url参数部分
            var index = url.indexOf("?");
            if (index >= 0) {
                // 获取Url中参数集合
                var params = url.substr(index + 1);
                params = params.split("&");
                // 遍历查询对应键值的参数
                for (var param in params) {
                    param = params[param];
                    index = param.indexOf("=");
                    // 获取对应键值的参数值
                    if (index >= 0 && key == param.substring(0, index)) {
                        value = param.substr(index + 1);
                    }
                }
            }
            // 返回获取到的参数值
            return value;
        };
    }
    // 定义设置Url参数方法
    if (typeof (window.setUrlParam) != "function") {
        /**
        * url:要设置参数的Url
        * key:要设置参数的键
        * value:要设置参数值
        * return:返回设置了参数的新Url
        */
        window.setUrlParam = function (url, key, value) {
            // 定位到Url参数部分
            var index = url.indexOf("?");
            if (index >= 0) {
                // 获取Url中参数集合
                var baseUrl = url.substring(0, index);
                var hasKey = false;
                var newParams = new Array();
                var oldParams = url.substr(index + 1);
                oldParams = oldParams.split("&");
                // 遍历Url中所有的参数
                for (var oldParam in oldParams) {
                    oldParam = oldParams[oldParam];
                    index = oldParam.indexOf("=");
                    if (index >= 0 && key == oldParam.substring(0, index)) {
                        // 将需要设置的参数重新赋值
                        newParams.push(key + "=" + value);
                        hasKey = true;
                    } else {
                        // 记录不需要设置的参数信息
                        newParams.push(oldParam);
                    }
                }
                // 没有找到参数的话添加新的参数
                if (!hasKey) {
                    newParams.push(key + "=" + value);
                }
                // 重构设置了参数的Url
                newParams = newParams.join("&");
                url = baseUrl + "?" + newParams;
            } else {
                // 重构设置了参数的Url
                url += "?" + key + "=" + value;
            }
            // 返回设置了参数的新Url
            return url;
        };
    }
})(window);

二、调用实例:

getUrlParam("index.php?a=1", "a"); // 结果:1
setUrlParam("index.php?a=1", "a", 2); // 结果:index.php?a=2