C# Winform 自定义Combobox的颜色、样式

比较复杂,需要绘制边框,主体,下拉项目,文字,下拉按钮。

根据MSDN:那些由 Windows 完成其所有绘图的控件(例如 Textbox)从不调用它们的 OnPaint 方法,因此不能重写OnPaint。请参见您要修改的特定控件的文档,
查看 OnPaint 方法是否可用。如果某个控件未将 OnPaint 作为成员方法列出, 则您无法通过重写此方法改变其外观。
MSDN:要了解可用的 Message.Msg、Message.LParam 和 Message.WParam 值,
请参考位于 MSDN Library 中的 Platform SDK 文档参考。可在 Platform SDK(“Core SDK”一节)
下载中包含的 windows.h 头文件中找到实际常数值,该文件也可在 MSDN 上找到。

使用下面的代码,DropDownStyle 设置为DropDownList,FlatStyle设置为Flat或Popup,DrawMode设置为OwnerDrawVariable,可以得到一个蓝色边框,淡蓝色的Combobox。 可参考.NET源码(https://referencesource.microsoft.com/#system.windows.forms/winforms/Managed/System/WinForms/ComboBox.cs)

 public class CustomComboBox : ComboBox
    {
        /// <summary> 
        /// 获得当前进程,以便重绘控件 
        /// </summary> 
        /// <param name="hWnd"></param> 
        /// <returns></returns> 
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        public CustomComboBox()
            : base()
        {
            SetStyle(
            ControlStyles.UserPaint |  //控件自行绘制,而不使用操作系统的绘制
            ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息,减少闪烁。
            ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制,不直接绘制到屏幕上,减少闪烁。
            ControlStyles.ResizeRedraw | //控件大小发生变化时,重绘。                  
            ControlStyles.SupportsTransparentBackColor, true);
        }
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0xf || m.Msg == 0x133)
            {
                IntPtr hDC = GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0)
                {
                    return;
                }
                Pen pen = new Pen(Color.FromArgb(0,163,235), 1);
                Graphics g = Graphics.FromHdc(hDC);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
                pen.Dispose();

                int dropDownButtonWidth = 30;
                var outerBorder = new Rectangle(ClientRectangle.Location, new Size(ClientRectangle.Width - 1, ClientRectangle.Height - 1));
                var innerBorder = new Rectangle(outerBorder.X + 1, outerBorder.Y + 1, outerBorder.Width - dropDownButtonWidth - 2, outerBorder.Height - 2);
                var innerInnerBorder = new Rectangle(innerBorder.X + 1, innerBorder.Y + 1, innerBorder.Width - 2, innerBorder.Height - 2);
                var dropDownRect = new Rectangle(innerBorder.Right + 1, innerBorder.Y, dropDownButtonWidth, innerBorder.Height + 1);


                g.FillRectangle(new SolidBrush(Color.FromArgb(241,251,255)), outerBorder);
                Brush brush = new SolidBrush(Color.FromArgb(0, 163, 235));


                Point middle = new Point(dropDownRect.Left + dropDownRect.Width / 2, dropDownRect.Top + dropDownRect.Height / 2);
                
                    middle.X += (dropDownRect.Width % 2);

                int Offset2Pixels = 2;
                g.FillPolygon(brush, new Point[] {
                     new Point(middle.X - Offset2Pixels, middle.Y - 1),
                     new Point(middle.X + Offset2Pixels + 1, middle.Y - 1),
                     new Point(middle.X, middle.Y + Offset2Pixels)
                 });
                if (!(SelectedIndex == -1))
                {
                    g.DrawString(Items[SelectedIndex].ToString(), new Font(Font.FontFamily, size, FontStyle.Regular), Brushes.Black, innerInnerBorder);

                }


                m.Result = IntPtr.Zero;
                //释放 
                ReleaseDC(m.HWnd, hDC);

            }
        }
        private float size = 9;
        private Color animalColor = Color.FromArgb(0, 163, 235);
        private Font myFont;

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            //base.OnDrawItem(e);
            // Draw the background of the item.
            e.DrawBackground();

            // Create a square filled with the animals color. Vary the size
            // of the rectangle based on the length of the animals name.
            Rectangle rectangle = new Rectangle(2, e.Bounds.Top + 2,
                    e.Bounds.Width, e.Bounds.Height - 4);

            //Console.WriteLine("e.State:{0}", e.State);
            if ((e.State & DrawItemState.Selected)!=0 || (e.State & DrawItemState.ComboBoxEdit) != 0)
            {      
                e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);
            }

            // Draw each string in the array, using a different size, color,
            // and font for each item.
            myFont = new Font(Font.FontFamily, size, FontStyle.Regular);
            
            if (e.Index == -1)
                return;
            if ((e.State & DrawItemState.Selected) != 0 || (e.State & DrawItemState.ComboBoxEdit) != 0)
            {
                e.Graphics.DrawString(Items[e.Index].ToString(), myFont, Brushes.White, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));

            }
            else
            {
                e.Graphics.DrawString(Items[e.Index].ToString(), myFont, Brushes.Black, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            }

        }
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            //base.Invalidate();
        }
    }

C# Winform 自定义彩色CheckBox控件

CheckBox默认有一个黑色框,黑色对号,白色背景。 使用自定义控件,继承Checkbox类,重写OnPaint,可以改变这几个颜色,和对号的样式。

class ColoredCheckbox: System.Windows.Forms.CheckBox
{
        public ColoredCheckbox()
        {
            
        }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            //base.OnPaint(pevent);

            pevent.Graphics.Clear(Color.White);

            using (SolidBrush brush = new SolidBrush(ForeColor))
                pevent.Graphics.DrawString(Text, Font, brush, 27, 4);

            Point pt = new Point(4, 4);
            Rectangle rect = new Rectangle(pt, new Size(16, 16));


            pevent.Graphics.FillRectangle(Brushes.White, rect);

            if (Checked)
            {
                using (SolidBrush brush = new SolidBrush(Color.FromArgb(0, 163, 235)))
                using (Font wing = new Font("Wingdings", 12f))
                    pevent.Graphics.DrawString("ü", wing, brush, 1, 2);
            }

            pevent.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(0, 163, 235))), rect);

            Rectangle fRect = ClientRectangle;

            
        }
        

}

C# Winform 更改radioButton颜色

自定义一个控件,继承RadioButton类,重写OnPaint方法,参考代码:

 public class ColoredRadioButton : RadioButton
{ 

    public ColoredRadioButton()
    {
            // Allows for Overlaying
        SetStyle(
         ControlStyles.UserPaint |  //控件自行绘制,而不使用操作系统的绘制
         ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息,减少闪烁。
         ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制,不直接绘制到屏幕上,减少闪烁。
         ControlStyles.ResizeRedraw | //控件大小发生变化时,重绘。                  
         ControlStyles.SupportsTransparentBackColor, true);
        BackColor = Color.Transparent;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        m_circle = new Rectangle(7, 9, 8, 8 /*Magic Numbers*/);
        m_outlinerect = new Rectangle(4, 6, 14, 14);
        m_outline = new Pen(new SolidBrush(Color.FromArgb(0,163,235)), 1.5F /*Magic Numbers*/);

            // Init
            //base.OnPaint(e);
        Graphics g = e.Graphics;

        g.Clear(Color.White);
        using (SolidBrush brush = new SolidBrush(ForeColor))
            g.DrawString(Text, Font, brush, 27, 4);

        g.SmoothingMode = SmoothingMode.AntiAlias;

            // Overlay Graphic
        if (Checked)
        {
            g.FillEllipse(new SolidBrush(Color.FromArgb(0, 163, 235)), m_circle);
            g.DrawEllipse(m_outline, m_outlinerect);
        }
        else
        {
            g.FillEllipse(new SolidBrush(Color.White), m_circle);
            g.DrawEllipse(m_outline, m_outlinerect);
        }
    }
}

Windows系统下 VSCode 配置C++编译

方法一:比较简单

1.安装MinGW-x64

可在https://sourceforge.net/projects/mingw-w64/files/?source=navbar下载,推荐下载离线安装包,x86_64-win32-sjlj或者x86_64-win32-seh。

解压到C:\mingw64\,并将C:\mingw64\bin添加到PATH环境变量。

2.VSCode中安装以下插件

a.C/C++

b.C/C++ Compile Run

c.C++ Intellisense

3.把默认的Terminal改成cmd

—————————玄学部分———————————

4.在项目文件夹中新建.vscode文件夹,

添加

.vscode\tasks.json
.vscode\launch.json
.vscode\c_cpp_properties.json

config_vscode

ngrok搭建(Windows服务端+Windows客户端)

1.go环境搭建(需要Linux系统)

1)下载源码,可以在http://www.golangtc.com/download

2)将其解压到/usr/local目录下:
tar -C /usr/local -xzf go1.9.2.linux-amd64.tar.gz

3).在root环境下执行如下命令:
mkdir $HOME/go
echo ‘export GOROOT=/usr/local/go’>> ~/.bashrc
echo ‘export GOPATH=$HOME/go’>> ~/.bashrc
echo ‘export PATH=$PATH:$GOROOT/bin’>> ~/.bashrc
source $HOME/.bashrc

4). 安装go get工具
yum install mercurial git bzr subversion

2.获取源码

git clone https://github.com/inconshreveable/ngrok.git

 

3. 编译
1). 配置环境变量

export NGROK_DOMAIN=”ngrok.example.com”

ngrok.example.com替换成你自己的域名。

 

2). 生成自签名ssl证书

cd ngrok

openssl genrsa -out rootCA.key 2048

openssl req -x509 -new -nodes -key rootCA.key -subj “/CN=$NGROK_DOMAIN” -days 5000 -out rootCA.pem

openssl genrsa -out device.key 2048

openssl req -new -key device.key -subj “/CN=$NGROK_DOMAIN” -out device.csr

openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000

cp rootCA.pem assets/client/tls/ngrokroot.crt

cp device.crt assets/server/tls/snakeoil.crt

cp device.key assets/server/tls/snakeoil.key

4. 交叉编译生成windows客户端
上述编译过程生成的服务端和客户端都是linux下的,不能在windows下用。如果想编译生成windows客户端,需要重新配置环境并编译。 交叉编译过程如下:

进入go目录,进行环境配置
cd /usr/local/go/src/

GOOS=windows GOARCH=amd64 CGO_ENABLED=0 ./make.bash
进入ngrok目录重新编译
cd /usr/local/src/ngrok/

GOOS=windows GOARCH=amd64 make release-server release-client
编译后,就会在bin目录下生成windows_amd64目录,其中就包含着windows下运行的服务器和客户端程序。

 

此时会遇到错误

Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.

解决办法是,下载golang1.4版本,然后执行下面命令。

 

tar zxvf [go1.4.2.darwin-amd64-osx10.8.tar.gz]
cp go/ $home/go-bootstrap/
GOROOT_BOOTSTRAP=$home/go-bootstrap/
export GOROOT_BOOTSTRAP

 

5.客户端及服务端配置。

以3389远程桌面为例。

客户端新建一个ngrok.cfg文件,内容如下:

server_addr: “ngrok.example.org:1180”
trust_host_root_certs: false

tunnels:
mstsc:
subdomain: “mstsc”
remote_port: 4443
proto:
tcp: “127.0.0.1:3389″

然后写一个批处理,内容如下:

ngrok -config=ngrok.cfg start mstsc

服务端也写一个批处理,内容如下:

ngrokd.exe -domain=”ngrok.example.org” -httpAddr=”:801″ -httpsAddr=”:802″ -tunnelAddr=”:1180″

 

6.参考

1.http://www.360doc.com/content/17/0524/18/29401987_656833431.shtml

2.https://www.jianshu.com/p/0146801c1178

3.https://www.jianshu.com/p/4f79ae4f081c

Linux 网站目录和MySQL备份并上传FTP

SH文件下载:backup-template

 

#!/bin/bash
MYSQL_USER=root
MYSQL_PASS=
FTP_USER=
FTP_PASS=
FTP_IP=
FTP_backup=backup
WEB_DATA=/home/wwwroot

DataBakName=Data_$(date +”%Y%m%d”).tar.gz
WebBakName=Web_$(date +”%Y%m%d”).tar.gz
OldData=Data_$(date -d -3day +”%Y%m%d”).tar.gz
OldWeb=Web_$(date -d -3day +”%Y%m%d”).tar.gz

rm -rf /home/backup/Data_$(date -d -3day +”%Y%m%d”).tar.gz /home/backup/Web_$(date -d -3day +”%Y%m%d”).tar.gz
cd /home/backup

for db in `/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -B -N -e ‘SHOW DATABASES’ | xargs`; do
(/usr/local/mysql/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS ${db} | gzip -9 – > ${db}.sql.gz)
done

tar zcPf /home/backup/$DataBakName /home/backup/*.sql.gz
rm -rf /home/backup/*.sql.gz

tar zcf /home/backup/$WebBakName $WEB_DATA
ftp -v -n $FTP_IP << END
user $FTP_USER $FTP_PASS
type binary
cd $FTP_backup
delete $OldData
delete $OldWeb
put $DataBakName
put $WebBakName
bye
END

L2TP+IPSec一键安装脚本

检测是否支持TUN模块
执行命令:

cat /dev/net/tun

如果返回信息为:cat: /dev/net/tun: File descriptor in bad state 说明正常
检测是否支持ppp模块
执行命令:

cat /dev/ppp

如果返回信息为:cat: /dev/ppp: No such device or address 说明正常

终端里运行以下命令(CentOS):

cd /root
wget --no-check-certificate https://raw.githubusercontent.com/teddysun/across/master/l2tp.sh
chmod +x l2tp.sh
./l2tp.sh

终端里运行以下命令(Ubuntu):

cd /root
wget --no-check-certificate https://raw.githubusercontent.com/teddysun/across/master/l2tp_ubuntu.sh
chmod +x l2tp_ubuntu.sh
./l2tp_ubuntu.sh

执行后会要求输入一些信息,如「Please input IP-Range:」意为输入本地IP段范围(本地电脑连接到VPS后给分配的一个本地IP地址),直接回车意味着输入默认值10.1.2;「Please input PSK:」PSK意为预共享密钥,即指定一个密钥将来在连接时需要用到,直接回车意味着输入默认值vpn。
输入了IP段范围和PSK之后,程序会显示你的VPS当前的IP(IPV4)、L2TP的本地IP(默认的话是10.1.2.1)、分配给客户端的IP段(默认的话是10.1.2.2-10.1.2.254)以及你所设置的PSK(默认的话是vpn),确认无误后,按任意键,程序便会开始自动配置。
安装完之后,会显示VPS当前的IP「ServerIP:VPS当前公网IP」、默认用户名「username:vpn」、默认用户名的密码「password:随机生成的6位字符串」、预共享密钥「PSK:你所设置的PSK,如果你之前没有设置则为默认值vpn」。

 

转载自网络

WordPress:为博客添加欢迎页

新建一个splash.html,编辑你个性的欢迎页面

更改index.php,

可通过检测HTTP_REFERER判断是否从页面内跳转或是从外部链接进入

决定是否显示欢迎页面,避免死循环

同时,我们只需要在访问首页时显示欢迎页面,所以 判断REQUEST_URI

<?php

$referrer = $_SERVER[‘HTTP_REFERER’];
$a=$_SERVER[‘REQUEST_URI’];

if (preg_match(“/blog.luobotou.org/”,$referrer)) {
define(‘WP_USE_THEMES’, true);
require(‘./wp-blog-header.php’);
} else {
if($a==”/”){
header(‘Location: http://blog.luobotou.org/splash.html’);}
else
{
define(‘WP_USE_THEMES’, true);
require(‘./wp-blog-header.php’);
}
}; ?>