惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
腾讯CDC
D
Docker
G
Google Developers Blog
D
DataBreaches.Net
雷峰网
雷峰网
Blog — PlanetScale
Blog — PlanetScale
S
SegmentFault 最新的问题
The Cloudflare Blog
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
量子位
美团技术团队
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Engineering at Meta
Engineering at Meta
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 清雷

湘江老厨聚餐 我的青春 今日感想 东莞一日游 HP MC/SG 双机安装心得 周末的狂想 汉字转拼音的例子 回忆大学 Asp.Net下的DataGrid的多层表头 VS无法调试智能设备程序的解决方案 VS.Net下的新的单元测试工具-TestDriven.NET 怀念我的大学(2) 显示主窗体前显示登陆窗口的完美解决方案 windows消息大全 在.NET 中使用WIN32 API的宝库 一个封装的异步Socket客户端 一个很好的C#题目 开发.NET CF 蓝牙的通信模块 怀念我的大学(1)
设置窗体背景图片,并且让图片随着窗体的大小的调整而调整大小
清雷 · 2005-09-14 · via 博客园 - 清雷

         在一个窗体中设置窗体的背景图片后,如果窗体大小改变时,图片并不会随之发生变化,下面就是我的解决方案:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication9
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private Image image;
        
private RectangleF rect;

        
public Form1()
        
{
            image 
= Image.FromFile(@"G:\我的文档\My Pictures\frl1.jpg");
            InitializeComponent();
            
this.BackgroundImage = image;
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
#region Windows 窗体设计器生成的代码
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
            
this.ClientSize = new System.Drawing.Size(616446);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.Resize += new System.EventHandler(this.Form1_Resize);
            
this.Load += new System.EventHandler(this.Form1_Load);
            
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

        }

        
#endregion


        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            
try
            
{
                rect 
= new RectangleF(0,0,this.ClientSize.Width,this.ClientSize.Height);
            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

        
        }

        
private void DrawBackgroudImage()
        
{
            
try
            
{
                Graphics g 
= this.CreateGraphics();
                g.DrawImage(image,rect);
                g.Dispose();
            }

            
catch(Exception e)
            
{
                MessageBox.Show(e.Message);
            }

        }


        
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        
{            
            DrawBackgroudImage();
        
        }


        
private void Form1_Resize(object sender, System.EventArgs e)
        
{
            rect 
= new RectangleF(0,0,this.ClientSize.Width,this.ClientSize.Height);
            DrawBackgroudImage();        
        }

        
    }

}