Cocoa: Autosave window not working - c#

I'm trying to save and restore the window bounds using Cocoa with C#. I tried the following according to docs, but it does not work. What I'm missing?
public class MyWindow: NSWindow
{
internal MyWindow() : base()
{
SetSavedFrame();
//Attach window will close event to MyWindow_WillClose
[...]
}
[...]
void SetSavedFrame()
{
FrameAutosaveName = AUTOSAVE_NAME;
if (SetFrameUsingName(FrameAutosaveName, true))
return;
SetFrame(new RectangleF (0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT), true);
Center();
}
void MyWindow_WillClose(object sender, EventArgs args)
{
SaveFrameUsingName(FrameAutosaveName);
}
}
If I cat the content of my .plist file, it is always the same, so I think the problem is, at least, when saving the preferences.
Thank you.

Related

Showing the camera image with Datalogic C# SDK for Visual Studio's

I was wondering if anyone has any experience with Datalogic's SDK for Visual Studio.
I'm trying to get the image out of VPM (Vision Program Manager). You have to be a bit familiar with Datalogic's 'Impact' to know where I am talking about. I'm using the function GetData(). The following condition must be made:
Device.GetImagePortValue(VisionPort.CreateFromPath("Inspection.Image In Task:Image"),
Afterward.IfSuccess<VisionImage>((ImageInspection) =>
But That's not the case. Here is the complete Program:
using VisionSDK;
using VisionSDK.Drawing;
using VisionSDK.Events;
namespace SDKCameraImage
{
public partial class MainWindow : Window
{
VisionDevice Device = VisionDevice.Create();
public MainWindow()
{
InitializeComponent();
Device.SetupConnection(VisionDeviceConnectionDetails.CreateForNetworkConnection("127.0.0.1",10001,2000));
GetData();
Device.EventListener.DidAcquiredAnImage += (_, __) =>
{
MessageBox.Show("Image Acquired");
GetData();
};
Device.DeviceConnection.DidConnectHandler += (object sender, VisionEventArgs e) =>
{
//MessageBox.Show("Connection");
};
Device.DeviceConnection.DidDisconnectHandler += (object sender, VisionEventArgs e) =>
{
MessageBox.Show("No Connection");
};
Device.Connect();
}
private void GetData()
{
Device.GetImagePortValue(VisionPort.CreateFromPath("Inspection.Image In Task:Image"),
Afterward.IfSuccess<VisionImage>((ImageInspection) =>
{
ImageViewer.Background = new ImageBrush(ImageInspection.AsImageSource());
}));
}
}
}
I have a connection with the camera. And the camera is in online state.
Hopefuly it's enough info. Thanks in advance!
Dirk
I figured it out!
The solution is to use an image made by a Datalogic camera. If you use any other image it won't show in your application.
I'm not sure yet why this is but now I know how to get it working!

Kinect New Window Wpf

So I'm making a Kinect application using buttons, and to navigate the app, I'm making new windows for each button. I'm come across an issue I haven't been able to find any help at all on, and would appreciate any help.
So to open the new window, I'm using this:
private void button1_Click(object sender, RoutedEventArgs e)
{
//SUPPOSED to uninitialize the Kinect
UninitializeKinectSensor(this.Kinect;
//To open the new window
Window1 newwindow = new Window1();
newwindow.Show();
//To close the first window...
Close();
{
SO that one line is supposed to uninitialize the Kinect so it'll be free for the new window to use, but when it goes to the new window, the Kinect freezes. If I use the mouse to go back to the first window, it works on the first window again, which it shouldn't.
I also added in this line in the initialization phase
public Window1()
{
//Other init code is here, but this is the line I added. It doesn't seem to do anything.
InitializeKinectSensor(this.Kinect);
}
Any help is greatly appreciated!! I'm sure it's something simple and I just failed miserably haha XD
Do you really have to create a new window instead of using pages?
In your MainWindow you create a frame that takes all the window and use this frame to navigate between pages. This way, you'll keep the focus of the kinect in your whole application.
Depends alot on what UninitializeKinectSensor is actually doing. Just as a quick fix, though, you can try calling uninitialize on a background worker and see if that helps at all.
Instead of using the "Show()" use "ShowDialog()".It's better if you can create a static class or method to initialize and uninitialized kinect.
public static void start()
{
KinectSensor.KinectSensors.StatusChanged += kinectSensorsStatusChanged;
DiscoverSensor();
}
private static void kinectSensorsStatusChanged(object sender, StatusChangedEventArgs e)
{
KinectSensor oldSensor = Kinect;
if (oldSensor != null)
{
UninitializeKinect();
}
var status = e.Status;
if (Kinect == null)
{
//updateStatus(status);
if (e.Status == KinectStatus.Connected)
{
Kinect = e.Sensor;
DiscoverSensor();
}
}
else
{
if (Kinect == e.Sensor)
{
//updateStatus(status);
if (e.Status == KinectStatus.Disconnected ||
e.Status == KinectStatus.NotPowered)
{
Kinect = null;
sensorConflict = false;
DiscoverSensor();
}
}
}
}
private static DispatcherTimer readyTimer;
private static void UninitializeKinect()
{
if (speechRecognizer != null && Kinect != null)
{
Kinect.AudioSource.Stop();
Kinect.SkeletonFrameReady -= kinect_SkeletonFrameReady;
Kinect.SkeletonStream.Disable();
Kinect.Stop();
//this.FrameSkeletons = null;
speechRecognizer.RecognizeAsyncCancel();
speechRecognizer.RecognizeAsyncStop();
}
if (readyTimer != null)
{
readyTimer.Stop();
readyTimer = null;
}
}

Gtk# - Proper way to clear ActiveText on a ComboBoxEntry?

I am learning C#/Gtk# by jumping in and creating a monstrosity of an app for my own personal enjoyment/torture. My latest quandary is how to clear the items from a ComboBoxEntry. I did find a way to do it, but it seems kludgy to me.
Here is my test app, I create ComboBoxEntry with text and then click a button to clear it:
Then when I click the button to clear it, the items are removed but "foo" still remains as the active text:
Anyhow, I figured out a way to clear it with the following code. It seems that there should be a better way to do this but I can't find one so I'm coming here for a sanity check:
using System;
using Gtk;
public partial class MainWindow: Gtk.Window
{
ListStore comboModel1 = new ListStore (typeof(string));
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
ComboBoxEntry1.Model = comboModel1;
comboModel1.AppendValues ("foo");
// Set "foo" as selected item
Gtk.TreeIter iter;
ComboBoxEntry1.Model.IterNthChild (out iter, 0);
ComboBoxEntry1.SetActiveIter (iter);
}
protected void Button1OnClicked (object sender, System.EventArgs e)
{
// Just doing this .Clear () still leaves "foo" as the ActiveText
comboModel1.Clear ();
// My kludge to clear ActiveText
comboModel1.AppendValues ("");
Gtk.TreeIter iter;
ComboBoxEntry1.Model.IterNthChild (out iter, 0);
ComboBoxEntry1.SetActiveIter (iter);
comboModel1.Clear ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
}
Thanks!
Jason
I just tested it on MacOs 10.7 and a simple comboModel1.Clear() does the trick. There is not ActiveText and furthermore the Combobox isnt event accessible anymore cause there are no values present in the ListStore.
So maybe this is a bug on Windows. But i doubt it and will test in on Windows too.
Just as a hint instead of using IterNthChild you can use GetIterFirst.

Command Pattern and OnPaint Event issue

I am trying to adapt Command Pattern to simple paint application with undo functionality. And I've stuck with OnPaint Event on Undo operations. This is the code:
[SOLVED] details at the end of the post
interface ICommand {
void Execute();
void UnExecute();
}
class DrawLineCommand : ICommand {
private SimpleImage simpleImage;
private Image prevImage;
public DrawLineCommand(SimpleImage simpleImage) {
this.simpleImage = simpleImage;
this.prevImage = simpleImage.Image;
}
public void Execute() {
simpleImage.DrawLine();
}
public void UnExecute() {
simpleImage.Image = prevImage;
}
}
class CommandManager {
private Stack undoStack = new Stack();
public void ExecuteCommand(ICommand command) {
command.Execute();
undoStack.Push(command);
}
public void UnExecuteCommand() {
if (undoStack.Count > 0) {
ICommand command = (ICommand)undoStack.Pop();
command.UnExecute();
}
}
}
class SimpleImage {
private Point startPoint;
private Point endPoint;
private PictureBox pictureBox;
public SimpleImage(PictureBox pictureBox) {
this.pictureBox = pictureBox;
pictureBox.Paint += new PaintEventHandler(pictureBox_Paint);
}
void pictureBox_Paint(object sender, PaintEventArgs e) {
// this code shows the line during drawing
// this code is under "if operation == drawLine" block
Graphics graphics = e.Graphics;
graphics.DrawLine(Pens.Red, startPoint, endPoint);
// how can i refresh picturebox after undo operation?
// "if operation == undo" then ??
}
public void DrawLine() {
// this code actually saves finally drawn line
Image img = Image;
Graphics graphics = Graphics.FromImage(img);
graphics.DrawLine(Pens.Red, startPoint, endPoint);
Image = img;
}
public void Invalidate() {
pictureBox.Invalidate();
}
public Image Image {
get { return pictureBox.Image; }
set { pictureBox.Image = value; }
}
public Point StartPoint {
get { return startPoint; }
set { startPoint = value; }
}
public Point EndPoint {
get { return endPoint; }
set { endPoint = value; }
}
}
public partial class FormMain : Form {
private PictureBox pictureBox;
private SimpleImage simpleImage;
private CommandManager commandManager;
public FormMain() {
InitializeComponent();
simpleImage = new SimpleImage(this.pictureBox);
commandManager = new CommandManager();
}
void pictureBox_MouseDown(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left)
return;
simpleImage.StartPoint = e.Location;
}
void pictureBox_MouseMove(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left)
return;
simpleImage.EndPoint = e.Location;
simpleImage.Invalidate();
}
void pictureBox_MouseUp(object sender, MouseEventArgs e) {
simpleImage.Invalidate();
commandManager.ExecuteCommand(new DrawLineCommand(simpleImage));
}
}
It actually draws a line, it executes command and push it on the stack. I cannot achieve working UNDO. I mean. Step-by-step debugging I see the object pops from the stack, and then OnPaint executes. But no 'previous' image is actually shown.
I have read many sites, and I have also sample app from one of codeproject site's / articles. It presents the same approach with TextBox and Bold / Italicize operations. It works like hell. The only difference is this cruel OnPaint method..
Thanks in advance for any advices!
[EDIT] in a rush i forgot that assigning one reference type to another is not copying it (creating independent object), changing this:
this.prevImage = simpleImage.Image;
in few places solved the problem. Everything works now..
The point here would be not to paint directly on the canvas, but rather have a data structure that represents your painting. You would then add a line to this painting object, and the main loop of the canvas would draw the appropriate graphic from the data structure. Then your do/undo methods would simply need to manipulate the data structure, not do the painting.
You would need something like this:
interface IPaintable // intarface for Lines, Text, Circles, ...
{
void OnPaint(Image i); // does the painting
}
interface IPaintableCommand // interface for commands
{
void Do(ICollection<IPaintable> painting); // adds line/text/circle to painting
void Undo(ICollection<IPaintable> painting); // removes line/text/circle from painting
}
Your main application would simply keep a List, and repaint the canvas when a command changes the painting collection.
It looks like you have an aliasing problem. In your DrawLineCommand you pull in a reference to the image before the operation and store it as such:
this.prevImage = simpleImage.Image;
You now have two references to the same object. The draw line operation happens you operate on that same image:
Image img = Image; // Now a third reference to the same image object
Graphics graphics = Graphics.FromImage(img);
graphics.DrawLine(Pens.Red, startPoint, endPoint);
Image = img; // and you set the Image reference back to the same object
The above makes img an unnecessary reference to Image. But, you still have another reference to Image in your command. After the garbage collector runs, you're back down to to references to the same image object. Undo then executes the following:
simpleImage.Image = prevImage;
Here you haven't changed Image, only made Image reference the same object it was alreafy referencing.
Although I strongly agree with m0sa, the fix, in this case, is to make prevImage a COPY of the original image at the time you create your command. For the following I assume that Image.Clone() is implemented, though I've never tried it myself:
this.prevImage = simpleImage.Image.Clone();
NOTE: you may quickly run out of memory with large images or many commands if you use this approach.

.NET 2.0 WinForm print screen

I would like to print an image of a dialog, as if [alt][Print Scrn] were used. Does the framework allow for this to be done programmatically?
The Graphics.CopyFromScreen(..) method should do what you need.
Here's a good sample I found on the web:
http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html
EDIT: Code sample: (I created it as an extension method)
public static class FormExtensions
{
public static void SaveAsImage(this Form form, string fileName, ImageFormat format)
{
var image = new Bitmap(form.Width, form.Height);
using (Graphics g = Graphics.FromImage(image))
{
g.CopyFromScreen(form.Location, new Point(0, 0), form.Size);
}
image.Save(fileName, format);
}
}
can be used:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.SaveAsImage("foo.bmp", ImageFormat.Bmp);
}
}
What you could probably do is use the existing DLL that has that functionality for windows. It looks like you need to grab either some key commands or do it with a form button, and use the User32.dll. Since interop can sometimes be a big pain, I found a resource here that might help you do what you want:
http://www.cornetdesign.com/2005/04/screen-print-capture-in-c-using_08.html
If you really want just the dialog, use Control.DrawToBitmap to get a BMP image from it.

Categories

Resources