I'm actually tryin to place SharpDX Window in Winforms Window like in the following video:
http://www.youtube.com/watch?v=g-JupOxwB-k
In SharpDX this method doesn't work.Can anyone tell me how to EASILY do this ?
don't think of it as putting a sharpDX window into a winforms window.
instead think of it as how to output SharpDX to the windows handle (sorta)
the key is in the SharpDX.DXGI.SwapChain. when creating this you will need a SwapChainDescription
I like to create mine like
SwapChainDescription scd = new SwapChainDescription()
{
//set other fields
OutputHandle = yourform.Handle,
//set other fields
};
Handle
SwapChainDescription
so when you call SwapChain.Present() it will render to the form.
this is the basic way to do it with straight SharpDX and not the toolkit stuff
EDIT 04222019 LINKS DO NOT WORK --- 01052022 Link fixed
if you want to use the toolkit's GraphicsDevice you will have to set the Presenter property. in almost the same way you set the window handle in the presentation parameters.
https://github.com/sharpdx/Toolkit/tree/master/Documentation
also the toolkit has the RenderForm which plays nice with the Game class
04222019
EDIT (DirectX Example)
here is an example using straight SharpDX (No Toolkit). for complete examples you should refer to the github examples HERE
As stated above all you need to do to render to a WindowsForm window is pass the Handle to the SwapChain
visual studio 2012
add the references: (all other references are default winforms project references)
some using statements to make things easier:
namespace YourNameSpaceHere
{
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
...the rest of the application
}
the Form class: here we make the device, swap chain, render target , and render target view a variable of the Form class we are declaring
public partial class Form1 : Form //default vs2012 declaration
{
Device d; //Direct311
SwapChain sc; //DXGI
Texture2D target; //Direct3D11
RenderTargetView targetveiw;//DIrect3D11
...the rest of the form
}
Initializing the Device and SwapChain: this is what works for me on my system. if you have problems than you need to research your specific implementation and hardware. DirectX (and by extension SharpDX) has methods by which you can detect what the hardware will support.
the main code Example:
using System;
using System.ComponentModel;//needed to overide OnClosing
//I removed useless usings
using System.Windows.Forms;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX;
namespace WindowsFormsApplication2
{
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
public partial class Form1 : Form
{
Device d;
SwapChain sc;
Texture2D target;
RenderTargetView targetveiw;
public Form1()
{
InitializeComponent();
SwapChainDescription scd = new SwapChainDescription()
{
BufferCount = 1, //how many buffers are used for writing. it's recommended to have at least 2 buffers but this is an example
Flags = SwapChainFlags.None,
IsWindowed = true, //it's windowed
ModeDescription = new ModeDescription(
this.ClientSize.Width, //windows veiwable width
this.ClientSize.Height, //windows veiwable height
new Rational(60,1), //refresh rate
Format.R8G8B8A8_UNorm), //pixel format, you should resreach this for your specific implementation
OutputHandle = this.Handle, //the magic
SampleDescription = new SampleDescription(1, 0), //the first number is how many samples to take, anything above one is multisampling.
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain(
SharpDX.Direct3D.DriverType.Hardware,//hardware if you have a graphics card otherwise you can use software
DeviceCreationFlags.Debug, //helps debuging don't use this for release verion
scd, //the swapchain description made above
out d, out sc //our directx objects
);
target = Texture2D.FromSwapChain<Texture2D>(sc, 0);
targetveiw = new RenderTargetView(d, target);
d.ImmediateContext.OutputMerger.SetRenderTargets(targetveiw);
}
protected override void OnClosing(CancelEventArgs e)
{
//dipose of all objects
d.Dispose();
sc.Dispose();
target.Dispose();
targetveiw.Dispose();
base.OnClosing(e);
}
protected override void OnPaint(PaintEventArgs e)
{
//I am rendering here for this example
//normally I use a seperate thread to call Draw() and Present() in a loop
d.ImmediateContext.ClearRenderTargetView(targetveiw, Color.CornflowerBlue);//Color to make it look like default XNA project output.
sc.Present(0, PresentFlags.None);
base.OnPaint(e);
}
}
}
this is meant to get you started with using DirectX using ShaprDX in a managed environment, specifically C# on Windows. there is much much more you will need to get something real off the ground. and this is meant as the gateway to rendering on a Winforms window using SharpDX. I don't explain things like vertex/index buffers or rendering Textures/Sprites. because it is out of scope for the question.
Related
I'm actually trying to make program that is displaying some picture on the Window
Here is part of code
public Texture2D tulTexture;
//...
protected override void LoadContent()
{
// Instantiate a SpriteBatch
spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));
// Loads the balls texture (32 textures (32x32) stored vertically => 32 x 1024 ).
// The [Balls.dds] file is defined with the build action [ToolkitTexture] in the project
tulTexture = this.Content.Load<Texture2D>("T.jpg");
// Loads a sprite font
// The [Arial16.xml] file is defined with the build action [ToolkitFont] in the project
base.LoadContent();
}
When I'm running the program,I'm getting the AssetNotFoundException but this is lie.I've got this asset!
AssetNotFoundException but this is lie.I've got this asset!
It is most likely that you didn't configure your texture to be part of the build. You need to set the action "ToolkitTexture" on the texture in VS, and read the texture Content.Load<Texture2D>("T") without the ".jpg", as it is done in SharpDX samples. This is explained in the comment of the code above you pasted.
When I use reflection in ViewDidLoad like this:
foreach (var m in this.GetType().GetMethods()) {
Console.WriteLine (m.Name);
}
without any other code, methods like
Add
set_View
are not there; when I add;
public void PreventOptimizing() {
var x = this.View;
this.View = x;
this.Add (null);
}
to the class and without calling that method, they are there. So I assume the AOT compilation optimizes these methods away as they are not called. I don't know which methods it adds away so I would like the compiler, for my experiment, not to optimize anything away. How can that be done? Or is there another trick preventing automated removal of method?
Edit: so full code, if this isn't clear enough;
result output will not contain 'Add' and 'set_View';
public class TestController : UIViewController
{
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
foreach (var m in this.GetType().GetMethods()) {
Console.WriteLine (m.Name);
}
}
}
Output does contain Add and set_View;
public class TestController : UIViewController
{
public void PreventOptimizing() {
var x = this.View;
this.View = x;
this.Add (null);
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
foreach (var m in this.GetType().GetMethods()) {
Console.WriteLine (m.Name);
}
}
}
You either set the linker option to "Link SDK assemblies only" or use the PreserveAttribute on the class.
Details are in Xamarin website:
http://developer.xamarin.com/Guides/ios/Advanced_Topics/Linker/
Linker Behavior
The linking process can be customized in different ways. The primary
mechanism for controlling the linker in Xamarin Studio is the Linker
Behavior drop-down in the iOS Build Xamarin Studio's Project Options
dialog box. In Visual Studio, this is located in project Properties,
under iOS Build.
Preserving Code
When you use the linker it can sometimes remove code
that you might have called dynamically either using
System.Reflection.MemberInfo.Invoke, or by exporting your methods to
Objective-C using the [Export] attribute and then invoking the
selector manually.
In those cases, you can instruct the linker to consider either entire
classes to be used or individual members to be preserved by applying
the [Xamarin.iOS.Foundation.Preserve] attribute either at the
class-level or the member-level. Every member that is not statically
linked by the application is subject to be removed. This attribute is
hence used to mark members that are not statically referenced, but
that are still needed by your application.
For instance, if you instantiate types dynamically, you may want to
preserve the default constructor of your types. If you use XML
serialization, you may want to preserve the properties of your types.
You can apply this attribute on every member of a type, or on the type
itself. If you want to preserve the whole type, you can use the syntax
[Preserve (AllMembers = true)] on the type.
Sometimes you want to preserve certain members, but only if the
containing type was preserved. In those cases, use [Preserve
(Conditional=true)]
I have bmp (or jpg) file.
I need to convert it to dds file programmatically (I can use C++, C# with or without .NET; I can try any other language if I would see some clues in it)
I know that there are software that do this, but I need it to integrate into my programm, this should be a part of a longer manipulations on my application.
BTW, my question is:
1) Are there any opensource program that does this, so I can look into the code of it?
2) Are there any tutorials found somewhere in web that can help me to write this code?
I could not found any helpfull information.
Thank you!
I'm sure there are standalone converters you can use calling them via command line, if you need a programmatically solution the easiest way I may think about is to use built-in XNA classes to do the job. Because XNA handles all these file formats you can open source .bmp and then save it back to .dds (using the Texture class):
public static void ConvertToDds(
GraphicsDevice graphicsDevice, string sourcePath, string targetPath)
{
Texture.FromFile(graphicsDevice, sourcePath)
.Save(targetPath, ImageFileFormat.Dds);
}
Things changed with XNA 4.0 (these methods have been removed), try the DDSLib to write and Texture2D to read:
public static void ConvertToDds(
GraphicsDevice graphicsDevice, string sourcePath, string targetPath)
{
using (var stream = File.Open(sourcePath))
{
var texture = Texture2D.FromStream(graphicsDevice, stream);
DDSLib.DDSToFile(targetPath, true, texture, false);
}
}
See linked pages for more details and examples. By the way you can't have C# without .NET!
Say I want to create a source code editor for ocaml programming language, where do I start? I am looking to create an editor for the Windows platform as a hobby project. My primary skill is in web development. I have developed windows apps long time ago. I have no clue how it is done with todays available tools. I have visual studio 2008 and C# is my language of choice.
You need to know:
OCAML Syntax, Features, Keywords, Functions etc...
C# as this is your native language I guess
You need to know what features you wanna implement
...if it's using a GUI or just from a terminal like nano/vim
how syntax highlighting works
how to open and save files
how autocompletion works
etc..
You might want to take look at some open source editors like dev-c++ or gedit
Also, as you in person are more web-devvy, you might want to start creating one which runs in a web browser. This is often easier and helps you understand the basics of creating a code editor. Later you can always write one for desktops.
If you are most comfortable in Visual Studio, then you can use the Visual Studio Shell to create your own IDE based on that foundation.
Here is a podcast that gives a good overview:
http://www.code-magazine.com/codecast/index.aspx?messageid=32b9401a-140d-4acb-95bb-6accd3a3dafc
Also, as a reference, the IronPython Studio was created using the Visual Studio 2008 Shell:
http://ironpythonstudio.codeplex.com/
Browsing that source code should give you a good starting point.
a lighter-weight alternative is to use the RichEdit control
example:
http://www.codeproject.com/Messages/3401956/NET-Richedit-Control.aspx
// http://www.codeproject.com/Messages/3401956/NET-Richedit-Control.aspx
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace RichEditor
{
public class RichTextBoxEx : RichTextBox
{
IntPtr mHandle = IntPtr.Zero;
protected override CreateParams CreateParams
{
get
{
// Prevent module being loaded multiple times.
if (this.mHandle == IntPtr.Zero)
{
// load the library to obtain an instance of the RichEdit50 class.
this.mHandle = LoadLibrary("msftedit.dll");
}
// If module loaded, reset ClassName.
if (this.mHandle != IntPtr.Zero)
{
CreateParams cParams = base.CreateParams;
// Check Unicode or ANSI system and set appropriate ClassName.
if (Marshal.SystemDefaultCharSize == 1)
{
cParams.ClassName = "RichEdit50A";
}
else
{
cParams.ClassName = "RichEdit50W";
}
return cParams;
}
else // Module wasnt loaded, return default .NET RichEdit20 CreateParams.
{
return base.CreateParams;
}
}
}
~RichTextBoxEx()
{
//Free loaded Library.
if (mHandle != IntPtr.Zero)
{
FreeLibrary(mHandle);
}
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary(String lpFileName);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
}
}
You could use Scintilla. It has syntax highlighting and some other features. Also, it has a .NET Version available here.
Another good tool is Alsing Syntax Box:
Powerful Syntax Highlight Windows
Forms Control for the Microsoft.NET
Platform. Written in 100% managed C#.
Supports syntax highlighting and code
folding for just about any programming
language.
With Alsing Syntax Box, you can define a syntax file (just like this one for C#) and later have a intellisense like feature.
You can start with one of them for your editor.
I'm using Visual Studio 2008, and have downloaded VSeWSS.exe 1.2, to enable Web Part development. I am new to SP development, and am already bewildered by the number of different versions of SP, and the VS add-ons. This particular problem has come up, which highlights my confusion.
I selected Add -> New Project -> Visual C# -> SharePoint-> Web Part, accepted the defaults, and VS created a project, with the main file WebPart1.cs
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace WebPart1
{
[Guid("9bd7e74c-280b-44d4-baa1-9271679657a0")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
public WebPart1()
{
}
protected override void CreateChildControls() // <-- This line
{
base.CreateChildControls();
// TODO: add custom rendering code here.
// Label label = new Label();
// label.Text = "Hello World";
// this.Controls.Add(label);
}
}
}
The book I'm following, Essential SharePoint 2007, by Jeff Webb, has the following for the default project -
using System;
<...as previously>
namespace WebPart1
{
[Guid("9bd7e74c-280b-44d4-baa1-9271679657a0")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
// ^ this is a new style (ASP.NET) web part! [author's comment]
protected override void Render(HtmlTextWriter writer) // <-- This line
{
// This method draws the web part
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
}
}
The real reason for my concern is that in this chapter of the book the author frequently mentions the distinction between "old style" web parts, and "new style" web parts, as noted in his comment on the Render method.
What's happened? Why has my default Web Part got a different signature to the authors?
The distinction the author is making with "new style" web parts is that they are the ASP.NET 2.0 web parts (released in 2005), which can be used in both SharePoint and ASP.NET. The old style web parts were specific to SharePoint
New style System.Web.UI.WebControls.WebParts.WebPart, available in ASP.Net 2.0 (2005) and WSS 3.0 (2006)
Old style Microsoft.SharePoint.WebPartPages.WebPart (still supported)
In the code samples in the question the two web parts are both new style, ie. they are ASP.NET Web Parts. The only difference is that visual studio has overidden a different method than the book has. However, both methods, and many others, eg. OnLoad, OnInit are available, and will be customised to get the web part to work.
My recommendation, after several months of web part development, is to use the first one as the basis for a "hello world" web part, ie.
protected override void CreateChildControls()
{
base.CreateChildControls();
Label label = new Label();
label.Text = "Hello World";
this.Controls.Add(label);
}
And then start adding code to this method, or add other methods (eg. OnLoad, OnPrerender) to add functionality.
The Rendermethod would not overriden in most web parts.