I need to be able to run a Unity game inside a Visual Studio form for my school project.
I'm not that good yet with VS and this kind of stuff is still complicated for me and I don't even know if it is possible.
This is what I have:
And this is the result that I need:
Is this even possible? If so, how or point me in the right direction please.
EDIT:
Found how to do it:
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private void button1_Click(object sender, EventArgs e)
{
var clientApplication = Process.Start("C:/Users/ricardo.coelho/Desktop/Unity_VS_Test.exe");
Thread.Sleep(500);
SetParent(clientApplication.MainWindowHandle, panel1.Handle);
}
Yes you can do that, via the command line parameters.
The parameter you are looking for is -parentHWND, as stated in the documentation. The documentation also contains an example that could help you out.
From there (in a C# application) it is a matter of
process = new Process();
process.StartInfo.FileName = "Child.exe";
process.StartInfo.Arguments = "-parentHWND " + panel1.Handle.ToInt32() + " " + Environment.CommandLine;
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForInputIdle();
EnumChildWindows(panel1.Handle, WindowEnum, IntPtr.Zero);
Related
I have made an simple application which uses few sound effects and .txt document for storing some info(those files are stored into my projects bin/Debug directory).The app works fine on my PC, but when I run the .exe file on other computer, it works until the sound or .txt file is necessary(then is gives me a warning message telling that sound or .txt file isn't reachable).
How can I solve this problem, i mean, what should I do to make my app work properly on other PC's as well.Thanks!
Edit:
Here it the code which uses only sound effects(i'm changing them, so there are more than one sound)
{
public partial class GameScreen : Form
{
bool sound;
string sound1;
string sound2;
string sound3;
SoundPlayer sp1;
public GameScreen(string PlayerName)
{
InitializeComponent();
sound1 = "Air_Horn.wav";
sound2 = "TaDam.wav";
sound3 = "Bonus.wav";
sp1 = new SoundPlayer(sound1);
sound = true;
}
private void GameScreen_Load(object sender, EventArgs e)
{
if (sound == true)sp1.Play();
}
}
}
and here is the code using sound effects and .txt document:
{
public partial class VictoryForm : Form
{
SoundPlayer sp2;
string results;
bool sound_eff;
public VictoryForm(string statistics, bool _sound)
{
InitializeComponent();
sp2 = new SoundPlayer("Cheering3.wav");
results = statistics;
sound_eff = _sound;
}
private void VictoryForm_Load(object sender, EventArgs e)
{
if (sound_eff == true) sp2.Play();
}
private void VictoryForm_FormClosing(object sender, FormClosingEventArgs e)
{
StreamWriter sw = new StreamWriter("Results.txt", true);
sw.WriteLine(results);
sw.Close();
}
}
}
Sounds like you a) have not copied your texts or sound to the other computer or b) use wrong path to them. To be sure, print out the full path to the text or sound files and check if the intended files are really there. I bet they are not. ...
Btw, the contructor of StreamWriter says about its arguments: path = "The complete file path to write to", but your "Result.txt" is not a full path.
I have read some answers and some tutorials but they either didn't work or were too complex for me to understand.
I am trying to get a different class to change A labels text but It did not work. Here is my code:
public static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Mainprog mp = new Mainprog();
Mainprog.count += 25;
mp.Counter.Text = Convert.ToString(Mainprog.count);
}
I need to be able to read lines of a file with a StreamReader processed by a FileSystemWatcher in a Windows service.
I've read and tried everything that made sense online, but it still doesn't work. When I'm attahced to my Windows service process (local machine using Visual Studio 2010), the whole thing works flawlessly!
When I try to run it (on my local machine) without attaching to it and debugging it, the second file never makes it through and I get the following msg:
"The process cannot access the file 'C:\Projects\Data\VendingStats\20121213_AZM_Journey_MIS.txt' because it is being used by another process." I do not have this file open anywhere else on my machine. It is just sitting in a directory. I then copy it in a directory and the FSW takes over (and the code below).
Can someone please tell me what I need to do to get this to work? I don't know why it works fine when I'm attached to and debugging it, but it doesn't work when I send the files through without being attached and debugging it. I feel it's defeintiely something on my local box that I need to disable, etc --- I don't know.....
I noticed that the error occurs even before it gets into the "using" statement, because the second file is never copied to the temp directory for it to be processed.
I noticed in my StackTrace, I'm getting the following error:
system.io.__error.winioerror(int32 errorcode string maybefullpath)
Here is my code:
protected override void OnStart(string[] args)
{
FileSystemWatcher Watcher = new FileSystemWatcher(#"C:\Projects\Data\VendingStats");
Watcher.EnableRaisingEvents = true;
Watcher.Created += new FileSystemEventHandler(Watcher_Created);
Watcher.Filter = "*.txt";
Watcher.IncludeSubdirectories = false;
}
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
try
{
string targetPath = #"C:\Temp\VendorStats";
// Use Path class to manipulate file and directory paths.
FileInfo fi = new FileInfo(e.FullPath); // full name of path & file in the FSW directory
string destFile = Path.Combine(targetPath, fi.Name);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
// To copy a file to another location and
File.Copy(e.FullPath, destFile, true);
// Set attribute to READONLY
if (fi.IsReadOnly == false)
fi.Attributes = FileAttributes.ReadOnly;
GetCruiseLineShipName(destFile, ref cruiseLine, ref shipName);
using (StreamReader sr = new StreamReader(File.Open(destFile, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
filename = e.FullPath;
//How many lines should be loaded?
int NumberOfLines = 39;
//Read the number of lines and put them in the array
for (int i = 1; i < NumberOfLines; i++)
{
ListLines[i] = sr.ReadLine();
switch (i)
{
case 3:
int idx = ListLines[i].IndexOf(":");
string timeLine = ListLines[i].Substring(idx + 1);
dt = GetDate(Convert.ToDateTime(timeLine.Substring(1)));
break;
//more code here of the same
}
}
//InsertData into database }
}
catch (Exception ex)
{
EventLog.WriteEntry("VendorStats", "Error in the Main:" + "\r\n\r\n" + ex.Message + "\r\n\r\n" + ex.InnerException);
return;
}
}
The bottom line to solving this was to put the method (that was spawned by the FileSystemWatcher) to sleep for "X" amount of seconds until Windows completely releases the resources to the previous and present files as well as the folder.
It was the FileSystemWatcher that actaully had a hold on the resources.
Here is some sample code:
private static void Watcher_Created(object sender, FileSystemEventArgs e)
{
try
{
Thread.Sleep(10000);
GetCruiseLineShipName(e.FullPath, ref cruiseLine, ref shipName);
using (StreamReader sr = new StreamReader(File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
i am showing images from server. In server image is changing in every second. I want that in my application image should be change automatically after one second.M new in windows 7 programming. Kindly suggest me where i am lacking in concept. M using this code.
This process will start when i will tab on my image.
private void image1_Tap(object sender, GestureEventArgs e)
{
System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 500 Milliseconds
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
}
This is calling this method .
void dt_Tick(object sender, EventArgs e)
{
status.Text = "chking" + counter++;
// Do Stuff here.
image1.Source = null;
Uri imgUri = new Uri(base_url,UriKind.Absolute);
BitmapImage BI = new BitmapImage(imgUri);
int H = BI.PixelHeight;
int w = BI.PixelWidth;
image1.Source = BI;
}
In this code my Counter is working fine and status.Text is sucessfully change in every second. But image is changing once after that its not changing.
Kinldy suggest me where i am commiting mistake.
Thanks in advance
Gaurav Gupta
I think you should declare System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer(); as a member variable instead of declaring it in the images tap event.
I do the same thing when grabbing images from a camera in my wp8 app. I hold the URL including the current datetime-value as a url-param in my viewmodel. When i want to refresh, i just reset my URL-property.
Here's my sample:
this.MyUrlProperty = string.Format("{0}?timestamp={1:yyyy-MM-dd HH:mm:ss:fff}", _originalCameraUrl, DateTime.Now);
Works great for me...
I am trying out the Historical Debugger in VS2010 Beta 1.
I have it turned on in the settings (at least I think I do), but when I try to examine objects the value is:
[Historical Data Has Not Been Collected]
Any Ideas how to get this to show the actual value?
Here is the code in question:
public partial class Form1 : Form
{
public Form1()
{InitializeComponent();}
private void button1_Click(object sender, EventArgs e)
{
int numer = Int32.Parse(txtNumerator.Text) ;
int denom = Int32.Parse(txtDemoninator.Text);
float answer = DivideValues(numer, denom);
txtAnswer.Text = answer.ToString();
}
private static float DivideValues(int numer, int denom)
{
float answer = numer / denom;
return answer;
}
}
To get it to work right you need to use the tree view in the Debug History Window. The navigation arrows in the gutter seem to be more of a distraction than a help as it lets you get to areas that are not recorded.
Also, local variables are not recorded. This is by design.