Form.Location must be set twice - c#

I'm writing a C# app (XNA to be specific) and I'm having some very strange behavior when trying to set the window location. The first time I set the location, it places it in a weird place (always the same) but if I set it again, even on the next line, it works.
This is the code I'm using. The initial values in Location are 657, 286. After attempting to set it to 1920, 300, the position becomes 2257, 314. If I run the set again, it correctly updates the position to 1920, 300.
I've tried a full clean and rebuild and tried running in both Debug and Release. I've also tried putting some code before this line just to attempt to rule out a timing issue. I'm totally stumped here, so any help would be greatly appreciated.
var form = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle( this.Window.Handle );
form.Location = new System.Drawing.Point( 1920, 300 );

To set the location of the form try this:
this.Location = new System.Drawing.Point(1920, 300)
Note that this assumes the form you are trying to reposition is the parent object of the method you are calling.

Related

c# SharpDX ScaleEffect Interpolation question

i've been using this wonderful library for low impact screen recording software however im at a point where i need to use a DirectX Scale Effect to scale the image down with anisotropic filtering.
I can create the effect just fine and set up most of its parameters, however im not able to set the most important one for me scaleEffect.SetValue((int)ScaleProperties.InterpolationMode, (int)SharpDX.Direct2D1.ScaleInterpolationMode.Anisotropic);
As soon as the program runs it crashes at that line with an invalid parameter exception.
I've searched and couldnt find anything wrong with the code, but then again i have almost 0 experience with anything Direct2D.
Here is the full code for that specific effect for reference:
SharpDX.Direct2D1.Effect scaleEffect = new D2D.Effect(textureDc, D2D.Effect.Scale);
scaleEffect.Cached = true;
scaleEffect.SetInput(0, frameBitmap, false);
var centerPoint = new SharpDX.Vector2(0, 0);
var newSize = new SharpDX.Vector2(0.2f, 0.2f);
scaleEffect.SetValue((int)ScaleProperties.CenterPoint, centerPoint);
scaleEffect.SetValue((int)ScaleProperties.Scale, newSize);
//Crashes Here: scaleEffect.SetValue((int)ScaleProperties.InterpolationMode, (int)SharpDX.Direct2D1.ScaleInterpolationMode.Anisotropic);
Error Message: SharpDX.SharpDXException: 'HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect.'
textureDc.BeginDraw();
textureDc.DrawImage(scaleEffect, InterpolationMode.Anisotropic);
textureDc.EndDraw();
Thank you very much in advance for any help!
UPDATE:
As per #Simon Mourier comment, the solution to avoid this bug somewhere in the SharpDX API, is to use the SharpDX.Direct2D1.Effects.Scale instead.
This way, i can confirm the InterpolationMode does no longer crash and works as intended!
Here is the new, working code for anyone running into the same issue.
SharpDX.Direct2D1.Effects.Scale scaleClass = new D2D.Effects.Scale(textureDc);
scaleClass.SetInput(0, frameBitmap,false);
scaleClass.Cached = true;
var centerPoint = new SharpDX.Vector2(0, 0);
var newSize = new SharpDX.Vector2(0.2f, 0.2f);
scaleClass.CenterPoint = centerPoint;
scaleClass.ScaleAmount = newSize;
scaleClass.InterpolationMode = InterpolationMode.Anisotropic;
textureDc.BeginDraw();
textureDc.DrawImage(scaleClass);
textureDc.EndDraw();
PS: For anyone wondering about casting the value to uint instead using the first method, it returns the following error:
(Argument1 cannot convert from uint to int)
So it semms that there might be some underlying bug in this specific scenario.
One solution is to use the SharpDX.Direct2D1.Effects.Scale directly which is a wrapper over the Effect class and comes with an InterpolationMode property.

Word Statusbar gets reset when I use range.Information

I have the following code (simplified to show the problem):
var wdApp = new Application();
var wdDoc = wdApp.Documents.Open("C:\foo.docx");
wdApp.StatusBar = "Updating...";
var rng = wdDoc.Range(10, 10);
if ((bool)rng.Information(WdInformation.wdWithInTable))
{
}
//StatusBar value is gone...
What could be the reason?
How can I prevent it?
Do you know of other situations where this can happen?
Here screenshots of the problem
1 F10 (step over) later
Edit:
The provided code uses NetOffice and not the interop library from Microsoft directly, therefor the syntax is correct. You may notice in the provided screenshots that they are taken from a running application. Breakpoint, highlighting of current line of code executing, aswell as the actual result of the code in the word application on the right. Where at first there is the desired statusbar "Tabelle 8 von 17 wird neu erstellt." (Table 8 out of 17 is recreating) and at the next step my statusbar is gone and its the default stuff "165 von 8227 Wörtern" (165 out of 8227 words)
What could be the reason?
I believe this is to do with the library you are using. I tested your code but with the Word Interop library, and the only way I could get the status bar to reset was to manually click/type within the Word window.
How can I prevent it?
I would say take a look into the code base of library you are using. It is likely that it is doing something that is causing the behaviour. Unless there is a specific reason you are using NetOffice I would suggest switching to the either the standard Interop or VSTO.
Do you know of other situations where this can happen?
As above, I could only get the status bar to reset if I manually carried out some sort of input into the window.

The variable 'control' is either undeclared or was never assigned - Except it is, and the application compiles

My designer view started to throw up this error today. However, the application compiles without error and the controls are useable.
The custom control is defined in the designer correctly. If i click "ignore and continue" the designer loads up fine, but the control mentioned is missing.
So far I have tried:
Cleaning, rebuilding then restarting visual studio hasn't worked.
I haven't made any changes since using it yesterday, then walking into the office today.
My constructors are
public CalendarWindow(MainWindow owner)
{
InitializeComponent();
this._owner = owner;
}
public CalendarWindow(int job, int visit, DataTable customer, DataTable address)
{
InitializeComponent();
this.addressDetails = address;
this.customerDetails = customer;
this.visitID = visit;
jobID = job;
}
and inside InitializeComponent() in designer.cs the controls are declared
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.label5);
this.panel1.Controls.Add(this.label4);
this.panel1.Controls.Add(this.calendar4); \\designer "error" points to here
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.calendar3); \\designer "error" points to here
this.panel1.Controls.Add(this.calendar2); \\designer "error" points to here
this.panel1.Controls.Add(this.calendar1); \\designer "error" points to here
this.panel1.Controls.Add(this.label2);
this.panel1.Location = new System.Drawing.Point(218, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(873, 628);
this.panel1.TabIndex = 10;
this.panel1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.calendar1_MouseDown);
Looks like I've managed to solve this problem.
While the compiler didn't have any problems compiling the code and running the application, it didn't like some code within the control itself.
I only managed to catch this because i attempted to add the same control to the form after hitting "ignore and continue" at which point it refused to add it and provided me with direction to what was causing the problem, which happened to be in a class that wasn't part of the control but was used by it.
Correcting that error solved the problem.
I'm no wiser as to why it would compile and run without problems, yet the errors fed back made no reference to the offending artifact.
I have had the same problem and I fixed it. Actually Visual Studio only works with X86 controls and you can't create a user control in X64 mode and use it.
You should add a new class library in Any CPU mode and build the library. then you can add its DLL in your project and it works.
If it doesn't you must go to the Configuration manager and set the "Active solution platform" to X64 also do that for all subprojects.
remember that build option has to be checked.
and go to the properties of the class library and click on the build tab. then set the "platform target" to Any CPU.

Windows 8.1 WPF application mixerSetControlDetails returns MMSYSERR_ERROR

Edit
After modifying the NAudio source code and a little debugging, I found that the mixerSetControlDetails function, which is called when I set the Value property, returns MMSYSERR_ERROR.
Is there a way to get further information about why mixerSetControlDetails failed?
I'm also open to other ways to accomplish same thing on C#.
Original
I have some code that computes energy in voice and sets the microphone boost level according to this computed value using NAudio. I have no problem reading audio samples but when I try to set microphone boost, the program gets stuck.
I have checked the issue on Windows 7, 8, and 8.1. The problem occurs only on Windows 8.1. Interestingly when I run the program on a virtual machine with Windows 8.1 it works as expected. Can this be a permission problem?
Here is the code for setting the boost value
foreach (MixerControl mixerControl in MixerLine.Controls)
{
if (mixerControl.ControlType == MixerControlType.Volume)
{
UnsignedMixerControl volumeControl = (UnsignedMixerControl)mixerControl;
volumeControl.Value = (uint) value;
}
}
Where MixerLine is created beforehand with this code.
MixerLine = new MixerLine((IntPtr) deviceID, 0, MixerFlags.WaveIn);
It seems the mixerSetControlDetails function returns MMSYSERR_ERROR randomly (at least I couldn't find a pattern). Actually it does set the new value, just ignore the exception, or if it does not set try setting the value again. The stuck behavior was about my program, irrelevant to this error.

Problems with PrimaryScreen.Size

I've been fine with Screen.PrimaryScreen.Bounds.Size for some time, but on my Windows7 computer attached to my big-screen TV it is giving me incorrect values.
I read elsewhere to try SystemInformation.PrimaryMonitorSize but that gives the same values.
When I right click the desktop to get Screen Resolution, it says 1920x1080. The above two are giving me 1280x720.
I've also tried the WPF versions:
var w = System.Windows.SystemParameters.PrimaryScreenWidth;
var h = System.Windows.SystemParameters.PrimaryScreenHeight;
MessageBox.Show(new Size((int)w, (int)h).ToString());
The display size has been changed via the (right click the desktop) Personalize > Desktop options to be 150% (since the screen is 60" and you sit kind of far away).
How to detect this so the value's returned from the above can be adjusted?
Note: I've discovered how to get around this with a right-click executable and adjust the compatability to disable DPI virtualization, but I still need a programatic solution so I don't have to have user's adjust this themselves: See - http://msdn.microsoft.com/en-us/library/dd464660(VS.85).aspx#dpi_virtualization
It could be your Dpi setting in windows set above 100%
Try using this method, this will scale the resolution to the current system Dpi settings
Winforms:
private Size GetDpiSafeResolution()
{
using (Graphics graphics = this.CreateGraphics())
{
return new Size((Screen.PrimaryScreen.Bounds.Width * (int)graphics.DpiX) / 96
, (Screen.PrimaryScreen.Bounds.Height * (int)graphics.DpiY) / 96);
}
}
WPF:
private Size GetDpiSafeResolution()
{
PresentationSource _presentationSource = PresentationSource.FromVisual(Application.Current.MainWindow);
Matrix matix = _presentationSource.CompositionTarget.TransformToDevice;
return new System.Windows.Size(
System.Windows.SystemParameters.PrimaryScreenWidth * matix.M22,
System.Windows.SystemParameters.PrimaryScreenHeight * matix.M11);
}
Note: Make sure your MainWindow is loaded before running this code
I don't feel this is a duplicate question, but the answer is the same as on another thread: https://stackoverflow.com/a/13228495/353147 As the question isn't about blurry fonts but why Screen.PrimaryScreen.Bounds.Size returns faulty information. It could help others.
I did run into an error message, that mscorlib threw an null error. From this thread http://forums.asp.net/t/1653876.aspx/1 I was able to discover that unchecking "Enable ClickOnce security settings" fixed it. This seems like a hack, but it works.

Categories

Resources