I am using the following to Show/Hide window by its handle:
[DllImport("user32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
// Win32 API Constants for ShowWindowAsync()
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
ShowWindowAsync(_hWnd, SW_SHOW); //Show Window
ShowWindowAsync(_hWnd, SW_HIDE); //Hide Window
When I hide Sticky Notes and then Show them again it cause visual "holes" in the stickies and I have to close the Sticky Notes and reopen.
Here's a screenshot of the problem:
I believe the problem is specific to the Sticky Notes.
I dont know why it happens but I would like to solve it somehow..
I thought about checking if the window is Sticky Notes, and if it is then Open/Close it instead of Show/Hide will act the same but I dont really like it - feels hacky.
Changing from ShowWindowAsync() to ShowWindow() solved the problem.
I dont know why the ShowWindowAsync() cause this problem and I wish for an educating answer here but for now, as long as my problem is solved I am happy.
Related
I'm looking for some guidance here, if not an example of how to accomplish what I am looking to do in C-Sharp(C#).
There is a particular button on a MS Access Ribbon that I would like to click. The MS Access window has a custom UI and custom buttons in different groups. When the button in the first group is clicked, it then opens a childform window. I need to automate that particular step and have my C# application programmatically open that child form by clicking the button or any other means to get it open.
I attempted to do this with the SendKeys and FindWindow functions, but this prove to be rather faulty, because the ALT id's kept changing in MS Access and the SendKeys would stop working.
I've tried searching for articles that would help me with this issue, but all I came across are tutorials on how to make a custom add-in for MS ribbon (I do not want this).
There might be a better way to do this and any help with it is greatly appreciated!
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string strClassName, int nptWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
Public Void SendTest()
{
IntPtr WinHandle = FindWindow("OMain", 0); // OMain = Window Class --found window by class
if (WinHandle == IntPtr.Zero)
{
MessageBox.Show("Program is not running or could not be detected.");
return;
}
SetForegroundWindow(WinHandle);
SendKeys.SendWait("%"); // ALT
SendKeys.SendWait("Y1"); // Y1
SendKeys.SendWait("Y1"); //Y1
}
I want to paste text from my text box or rich text box in C# windows form application to out side of the form for example:
//on a button click event
textbox1.text=Clipboard.SetText(); // this will set text to clipboard
now I want when I click in address bar of Firefox or Google chrome to get the same text as I typed in my windows form application, as I can do it by CTRL+V but I want a C# program to do so for me and get text from clipboard when ever I click in address bar or rename a folder.
You could just turn on some windows disability settings, If dragging or pasting is too awkward.
If you really want to implement this, you need a global hook on the mouse so you can recieve mouse events from outside your application. See here or perhaps here.
Then you have a problem, do you want to paste anywhere or just in address bars. First you'll have to define what an address bar window is and work out if that is what has the focus.
Its a lot of effort and the behaviour is not especially desirable. If you really want this, please expand your question and improve its readability so that this post will be useful to future visitors.
This is completely untested, and I've never used these DLL calls in C#, but hopefully it will do the trick or at least come close...
public class Win32
{
public const uint WM_SETTEXT = 0x000c;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
}
Elsewhere in the code...
IntPtr theHandle = WindowFromPoint(Cursor.Position.X, Cursor.Position.Y);
if (theHandle != null)
{
long res = Win32.SendMessage(theHandle, WM_SETTEXT, IntPtr.Zero, Clipboard.GetText());
}
Note: I'm not entirely sure that WindowFromPiont will get the handle of the child control (i.e. the actual textbox) of another window, rather than the handle of the window itself. You might have to find the child by the cursor position. Been a long time since I've done something like this, unfortunately.
Also, if you want to get a fancier with the acquisition of the window handle, see this question: getting active window name based on mouse clicks in c#
I'm very new to C# and still trying to get my head round it (with help of some very patient friends).
I have an issue with setting a new windows form's TopMost property to true. I have two (almost) identical forms; 1 which works OK and one which doesn't.
Both of the forms have the TopMost property set to true.
Form1 shows the window and when I try to click behind it, the form flashes a few times and issues a windows beep.
Form2 also shows the form but when I click behind it, the form greys out (or loses focus) and I can click away on the main form.
I've searched for an answer to this issue and found an answer which suggested putting this.TopMost = true; in the form's load event but that didn't work.
The only thing I have changed which may or may not have had an effect is that Form1 was created with .NET 4.5 set in the properties and before creating Form2, I changed this to .NET 3.5 (client profile). I've tried changing it back but it hasn't helped. Before I delete and create Form2 again, does anyone have any ideas?
Many thanks in advance.
(If you need any more information, please just let me know)
TopMost is a property that is used to make sure one window is always shown above all others within an application. Microsofts example was a find and replace tool.
The difference you are finding is that Form1 was created as a modal dialog through the use of ShowDialog. Show dialog makes sure that your form must be closed before all other windows in the application can be used again. For example; using a form to gain user data to enter into a parent forms database.
Show is used when you don't mind if your user has finished with their dialog or not, such as allowing your user the chance to use some utility (e.g timer, stopwatch) that will assist within the main function of a program.
The only visual difference I can think of when using different .Net frameworks, is different windows dialogs such as the OpenFileDialog, that have been updated throughout the framework
It may help you;
frm.TopLevel = true;
frm.TopMost = true;
This link from Microsoft confirm that could be a Bug in Windows 7 and Windows Server 2008 R2 I've faced it in a Windows 7 Embedded system and the provided patch fix the problem so please consider to take a look :)
http://support.microsoft.com/kb/2587473/en-us
Hope it help!
The DIY-way to do it - works 100%!
public static class User32
{
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
public const int SW_SHOWNORMAL = 1;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_RESTORE = 9;
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(uint dwProcessId);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
User32.SetForegroundWindow(Handle);
User32.ShowWindow(Handle, User32.SW_SHOWNORMAL);
I hat a similar problem in my solution. After using the overloaded Show-function it worked:
frm.TopLevel = true;
frm.TopMost = true;
frm.Show(this)
Add the following code in the Shown event:
this.TopMost = true;
this.Focus();
this.TopMost = true;
I'm currently developing a form application which works as an overlay to another program (Skype). Right now, I'm using TopMost = true, but that's a pretty bad solution.
I have a handle to the Skype window, as well as a handle to my own window. How do I make my program fulfill the following three statements:
1. It has to disappear if Skype is minimized
2. It has to appear above Skype
3. It has to appear behind any other application which is above Skype
Above and behind relates to z-order.
I'm currently using the SetWindowLong function, but I cant get the desired results.
[DllImport("user32.dll")]
public static extern int SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong);
SetWindowLong(
new HandleRef(child, child.Handle),
-8, // GWL_HWNDPARENT
new HandleRef(owner, owner.Handle));
For #1, my application continually checks if the dimensions of Skype has changed, so I could simply also check if the window is no longer visible. However, I'm completely at loss with #2 and #3.
Thanks in advance.
Kloar
It's a small thing but I was just wondering...
Visual Studio 2008, C#.
I have a master-detail form with databound controls. When user selects the record in a listbox, all the details are updated in multiple databound controls on the form.
As it happens, they kind of 'flash', or blink, when repopulated with new data and it's sort of like an electrical wave going across the form in a fraction of second :) don't know how to explain it better
Not a big deal, but still, it looks "shaky" and ugly, so, for the sake of ellegance, I was just wondering whether there is some easy way to prevent it?
I thought about calling SuspendLayout and ResumeLayout (on the container control), but what events should I handle? listBox_SelectedValueChanged for suspending it, I guess ... but for resuming?
You could prevent the flicker by suspending painting when the control's data is refreshed.
From this stackoverflow question:
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 11;
public static void SuspendDrawing(Control parent)
{
SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
}
public static void ResumeDrawing(Control parent)
{
SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
parent.Refresh();
}
As far as which events to handle, I'm not sure.
I didn't notice that "SuspendLayout" did anything for me, but worth giving it a shot. I think you would want to latch onto the "CurrentChanged" event for when the selected item is changed wholesale.
Have you set the DoubleBuffered (under "behavior" in the props window) to true?
control.DoubleBuffered = true;
A bit 'o history: http://www.codeproject.com/KB/graphics/DoubleBuffering.aspx