Question

I'm using SlimDX to render to a Windows Panel within a form using Visual C# Express. SlimDX derives a class from System.Windows.Forms.Form called SlimDX.Windows.RenderForm. I need to use this class in order to handle the windows message loop. I in turn must derive a class from SlimDX.Windows.RenderForm in order to add controls. My derived class looks like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX;
using SlimDX.Windows;

namespace SceneGenerator
{
    public partial class ApplicationForm : RenderForm
    {
        public ApplicationForm() { }
        public ApplicationForm( string text )
        {
            InitializeComponent();
            this.Text = text;
        }
        public Panel GetViewport() { return this.ViewportPanel; }

    }
}

Now though my background image does not show up in Visual C# environment( nor during runtime ). The background image is set in the usual InitializeComponent() function of the base class...Here are my designer and entry point codes:

namespace SceneGenerator
{
    partial class ApplicationForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ApplicationForm));
            this.ViewportPanel = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // ViewportPanel
            // 
            this.ViewportPanel.Location = new System.Drawing.Point(410, 12);
            this.ViewportPanel.Name = "ViewportPanel";
            this.ViewportPanel.Size = new System.Drawing.Size(475, 345);
            this.ViewportPanel.TabIndex = 0;
            // 
            // ApplicationForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(897, 788);
            this.Controls.Add(this.ViewportPanel);
            this.DoubleBuffered = true;
            this.Name = "ApplicationForm";
            this.Text = "ApplicationForm";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel ViewportPanel;
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using SlimDX.Windows;
using SlimDX.Direct3D9;
using Device = SlimDX.Direct3D9.Device;
using Resource = SlimDX.Direct3D9.Resource;

namespace SceneGenerator
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            // form
            var form = new ApplicationForm("Scene Generator");
            Panel viewportPanel = form.GetViewport();
            // device
            var device = new Device(new Direct3D(), 0, DeviceType.Hardware, viewportPanel.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters()
            {                
                BackBufferWidth = viewportPanel.Width,
                BackBufferHeight = viewportPanel.Height
            });

            MessagePump.Run(form, () => 
            {
                device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Green, 1.0f, 0);
                device.BeginScene();
                device.EndScene();
                device.Present();
            });
        }
    }
}

Why might this occur? Seems related to Visual C# Express and my lack of instructions to use the background image...

Was it helpful?

Solution

This is a problem caused by this RenderForm class. It overrides the OnPaintBackground() method and does nothing. So you don't see the BackgroundImage, you don't see BackColor either. You do get interesting LSD-style psychedelic effects in the designer. You can fix thix by writing your own:

    protected override void OnPaintBackground(PaintEventArgs e) {
        if (this.BackgroundImage != null) {
            e.Graphics.DrawImage(this.BackgroundImage, this.DisplayRectangle);
        }
    }

But you'd better work from the assumption that this was done intentionally to solve some kind of drawing interference problem. Which you'll probably get back by using a Panel, you'd have to derive your own and in turn override its OnPaintBackground() method:

using System;
using System.Windows.Forms;

class RenderPanel : Panel {
    protected override void OnPaintBackground(PaintEventArgs e) {
        // Do nothing, like RenderForm
    }
}

Might work, but this is pretty iffy stuff, not entirely uncommon for open source. This just doesn't look like it was made to do what you like to do. Best to avoid trouble and pass the form's Handle to the Device constructor.

OTHER TIPS

Try referencing it as an external resource as in via a direct path or something. The way I'd try it is:

Environment.CurrentDirectory + @"\backgroundimage.png"

And if it still doesn't work like that one of two things might be happening.

  1. Your background image is corrupt somehow
  2. SlimDX might not accept BackgroundImages properly

Now I've never used SlimDX before so I'm not entirely sure on this but you could also try referencing it via the designer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top