Click here to Skip to main content
15,883,855 members
Articles / Desktop Programming / WPF

Creating OpenGL Windows in WPF

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
10 Mar 2009CPOL9 min read 161.1K   7.8K   63  
A guide to creating OpenGL applications with Windows Presentation Foundation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Interop; //HwndHost

namespace WPFOpenGLApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class OpenGLHWndWindow : Window
    {
        public OpenGLHWndWindow()
        {
            InitializeComponent();
        }

        private System.Windows.Threading.DispatcherTimer updateTimer = new System.Windows.Threading.DispatcherTimer();

        public override void BeginInit()
        {
            updateTimer.Interval = new TimeSpan(160000);
            updateTimer.Tick += new EventHandler(updateTimer_Tick);
            updateTimer.Start();
            base.BeginInit();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);

            if (!e.Cancel)
            {
                if (null != updateTimer)
                {
                    updateTimer.Stop();
                    updateTimer = null;
                }
            }
        }

        private void updateTimer_Tick(object sender, EventArgs e)
        {
            if (null != hwndPlaceholder &&
                null != hwndPlaceholder.Child)
            {
                hwndPlaceholder.Child.InvalidateVisual();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Create our OpenGL Hwnd 'control'...
            HwndHost host = new WPFOpenGLLib.OpenGLHwnd();

            // ... and attach it to the placeholder control:
            hwndPlaceholder.Child = host;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                e.Handled = true;
                this.Close();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer emphess
Germany Germany
Chris lives in Munich, Germany and tries to study Physics.

Alongside, he works as a self-employed software consultant on projects from various fields.

Unfortunately, he loved software ever since he started with C++, and his addiction has become a lot worse since the introduction of .NET 2.0.

In his spare time, he tries to go sailing, drink some nice single malt whisky and, in very rare cases, he also sleeps.

You can reach him at ChrisNOSPAM at emphess.net or via his blog emphess.net.

Comments and Discussions