How can i run hidef XNA from winforms? - winforms

I have winforms application and i have texture SimpleTexture.xnb compiled as HiDef. I need to run XNA 3D visualisation from my winforms application in separate window. I try this:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread thStartGame = new System.Threading.Thread(StartGame);
thStartGame.Start();
}
private void StartGame()
{
using (Game1 game = new Game1())
{
game.Run();
}
}
But i get error:
Error loading "SimpleTexture". This file was compiled for the HiDef profile, and cannot be loaded into a Reach GraphicsDevice.
What can i do to run this??

Change reach to hidef:
YourGraphicsDeviceManager.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs (graphics_PreparingDeviceSettings);
void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
e.GraphicsDeviceInformation.GraphicsProfile = GraphicsProfile.HiDef;
}

Related

How to make a file download progress bar in WinForms?

I'm making a web browser with CefSharp. I just implemented downloads 2 days ago but there is no progress bar to go along with the download. How do I make the progress bar show download progress?
Edit: Making things clearer
Add a ProgressBar Control to your Form and add a BackgroundWorker Component alongside it to your form. Figure out your file size first:
Int64 bytes_total= Convert.ToInt64(client.ResponseHeaders["Content-Length"])
This is code from Alex's amazing answer which can be found here:
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.WorkerReportsProgress = true;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
//Replace this code with a way to figure out how much of the file you have already downloaded and then use backgroundworker1.ReportProgress and send the percentage completion of the file download.
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
backgroundWorker1.ReportProgress(i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

Why does WPFMediaKit VideoCaptureElement created by code land ever in MediaFailed?

I add a VideoCaptureElement to a window in runtime but when I run this code it fires MediaFailed. But if I add the same element in XAML then it works fine, I can see the video from the laptop camera.
Am I doing anything wrong? Please help!
public partial class MainWindow : Window
{
WPFMediaKit.DirectShow.Controls.VideoCaptureElement VCE;
public MainWindow()
{
InitializeComponent();
VCE = new WPFMediaKit.DirectShow.Controls.VideoCaptureElement();
Content = VCE;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
VCE.MediaOpened += VCE_MediaOpened;
VCE.MediaFailed += VCE_MediaFailed;
VCE.VideoCaptureDevice = WPFMediaKit.DirectShow.Controls.MultimediaUtil.VideoInputDevices[0]; // This is my laptop webcam
}
void VCE_MediaOpened(Object sender, RoutedEventArgs e) { ... }
void VCE_MediaFailed(object sender, WPFMediaKit.DirectShow.MediaPlayers.MediaFailedEventArgs e) { ... }
}
I had a similar problem with a MediaUriElement working in XAML but not working when instantiated in code-behind.
The solution for me was to Init the control:
VCE.BeginInit();
VCE.EndInit();
This would fit between instantiating (VCE = new...) and assigning (Content = VCE). I haven't tested your particular scenario, but it sounds like the same cause - there must be some extra work done in Init that happens automatically when using XAML.

GMap.NET Windows Forms move the map with the mouse

How in GMapControl it is correct to handle pressing and moving Google maps using the mouse?
GMapControl gMap = new GMapControl();
gMap.MouseDown += GMap_MouseDown;
private void GMap_MouseDown(object sender, MouseEventArgs e)
{
gMap.MouseMove += GMap_MouseMove;
}
private void GMap_MouseMove(object sender, MouseEventArgs e)
{
base.OnMouseMove(e);
//gMap.Position = new PointLatLng(X, Y);
}
You don't need any code to move the map. The default is for the RIGHT mouse button to move the map, which I found rather awkward. To change to the LEFT mouse, use code like this in the Load() event of your Form:
gMap.DragButton = MouseButtons.Left;

WPF equivilent of unity 3D's Screen.lockCursor

I haven't used Unity 3D but I gather you can use Screen.lockCursor to take control of the mouse for FPS games. Is this possible in WPF/Win32?
Obviously you have to release it when exiting or in the event of a crash
Thanks
I found the answer spread across a whole bunch of links, so
(1) Set a captureMouse flag, press once to go into this mode, again to come out,
hide the cursor while you are in there
bool captureMouse = false;
private void viewport3D1_MouseDown(object sender, MouseButtonEventArgs e)
{
if (!captureMouse)
{
captureMouse = true;
Mouse.OverrideCursor = Cursors.None;
}
else
{
Mouse.OverrideCursor = null;
captureMouse = false;
}
}
(2) While you're in this mode constantly put the mouse back to the middle of the window
private void theWindow_MouseMove(object sender, MouseEventArgs e)
{
if (!captureMouse)
return;
Point windowPoint = WpfToRealPixels(theWindow, new Point(500, 500));
NativeMethods.SetCursorPos((int)windowPoint.X, (int)windowPoint.Y);
oldP = new Point(500, 500);
}
(3) Translate the co-ords
private Point WpfToRealPixels(Window w, Point p)
{
return theWindow.PointToScreen(p);
}
(4) To put the mouse back you'll need a native Win32 call
public partial class NativeMethods
{
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool SetCursorPos(int X, int Y);
}
Hope that helps someone.

Why is this getting random user IDs and not the current user?

Some developers in my company are having a problem with developing an in-house system. In the code snippet below, winID should be assigned the current system's user's ID. Instead, it's being assigned someone else's ID, apparently at random. Any suggestions?
public partial class _Default : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
Page.Theme = "MasterTheme1";
if (Session["UserName"] == null)
{
using (((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate())
{
WindowsIdentity winID = System.Security.Principal.WindowsIdentity.GetCurrent();
Session["UserName"] = winID.Name;
}
}
Response.Redirect("MainDB_Login.aspx");
}
protected void Page_Load(object sender, EventArgs e)
//Snip
}
Possibly cacheing in place (browser, or on server)?

Resources