I am trying to make a form move (using the titlebar) from a button click.
I thought this would be simple using SendMessage:
Const WM_LBUTTONDOWN As Integer = &H201
Button1.Capture = False
Cursor.Position = Me.Location + New Size(50, 8)
SendMessage(Me.Handle, WM_LBUTTONDOWN, CType(1, IntPtr), IntPtr.Zero)
However, although this sends the message if the cursor is in the forms client area, it does not seem to send it to the forms titlebar (the form captures the event somehow, despite the cursor being on the titlebar not in the client area).
I have tried the above code in both mousedown and click events on the button, moving the cursor and then pressing on the button1.
Any suggestions?
You would need WM_NCLBUTTONDOWN (and pass HTCAPTION as wParam). I'm still not entirely sure this would accomplish what you're trying to do, though.
Typically, the way to allow the user to move your form when clicking somewhere other than the title bar is to process the WM_NCHITTEST message and return HTCAPTION when the cursor is over the area from which you'd like to initiate moving. However, if this area is occupied by a child control, you also have to process WM_NCHITTEST from the child control and return HTTRANSPARENT.
Incidentally, an easier—if slightly less correct—way to accomplish this is to do as Mehrdad Afshari suggested, and just set the form's Location property. You commented to him that "it needs to move on the mouse move", and that's exactly what you can and should do.
class MyForm : Form{
Point downAt;
MyForm(){
Label lbl = new Label();
lbl.AutoSize = true;
lbl.BackColor = Color.Blue;
lbl.ForeColor = Color.White;
lbl.Location = new Point(50, 50);
lbl.Text = "Drag me to move this form.";
lbl.Parent = this;
lbl.MouseDown += (s, e)=>downAt = e.Location;
lbl.MouseMove += (s, e)=>{if(lbl.Capture) Location += (Size)e.Location - (Size)downAt;};
}
}
The problem with this approach is that it bypasses Windows' code for moving a top-level window. This means that if the user has not selected the "Show window contents while dragging" option in the Display Properties dialog, this will effectively ignore that setting (it won't show a drag outline). There may be other drawbacks that I haven't thought of as well.
On the whole, though, this is a simple, easy way to accomplish this that is a fully .NET solution which doesn't rely on any platform invoke (so it should be portable to Mono on Unix).
Oops. I just realized that I gave you C# example code, but your code seems to be VB.NET. I guess what you would need would be:
Sub New()
Dim lbl As New Label
lbl.AutoSize = True
lbl.BackColor = Color.Blue
lbl.ForeColor = Color.White
lbl.Location = New Point(50, 50)
lbl.Text = "Drag me to move this form."
lbl.Parent = Me
AddHandler lbl.MouseDown, Function(ByVal s As Object, ByVal e As MouseEventArgs)
Me.downAt = e.Location
End Function
AddHandler lbl.MouseMove, Function(ByVal s As Object, ByVal e As MouseEventArgs)
If lbl.Capture Then
Me.Location = Me.Location + DirectCast(e.Location, Size) - DirectCast(Me.downAt, Size)
End If
End Function
End Sub
This may not be the most succinct way to express this in VB.NET. I used Reflector to help me translate it.
The LParam value for the wm_LButtonDown message receives the mouse position in client coordinates. The title bar is in the non-client area, so use the wm_NCLButtonDown message. I've seen that message given as an answer to this question before, but there's a more direct route that I would have expected to work: Send a wm_SysCommand message to the window, and specify sc_Move flag.
Mehrdad is right, no need to do this. The mouse is captured so you can never move it too quickly. Sample code:
Point mLastPos;
private void button1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
this.Location = new Point(this.Location.X + e.X - mLastPos.X,
this.Location.Y + e.Y - mLastPos.Y);
}
// NOTE: else is intentional!
else mLastPos = e.Location;
}
Related
I have an VSTO Outlook Add-in and I would like to popup a notification (floating window) with a custom text message for a specific time (5 seconds for example) and then disappear automatically. I want to show this notification from within the compose window. How can I achieve this? some example will highly appreciated.
UPDATED: I would like the notification window to be a child of the compose window.
UPDATE 29.06.2022:
I have done the following:
Outlook.Inspector currentInspector = this.Window as Outlook.Inspector;
IOleWindow ioleWnd = (IOleWindow)currentInspector;
ioleWnd.GetWindow(out IntPtr phwnd);
NativeWindow nativeWnd = new System.Windows.Forms.NativeWindow();
nativeWnd.AssignHandle(phwnd);
// frm is my notification window, borderless and without maximize, minimize and close buttons and without title bar.
Form frm = new Form();
frm.ControlBox = false;
frm.FormBorderStyle = FormBorderStyle.None;
NativeMethods.Rect rect = new NativeMethods.Rect();
NativeMethods.GetWindowRect(phwnd, ref rect);
frm.Left = rect.Right - 85;
frm.Top = rect.Bottom - 55;
frm.Width = 80;
frm.Height = 50;
frm.Location = new System.Drawing.Point(rect.Right - 85, rect.Bottom - 55);
frm.BackColor = System.Drawing.Color.Red;
TextBox txtBox = new TextBox();
txtBox.BackColor = System.Drawing.Color.Red;
txtBox.ForeColor = System.Drawing.Color.White;
txtBox.Location = new System.Drawing.Point(5, 5);
txtBox.Visible = true;
txtBox.Text = "This is a notification message";
frm.Controls.Add(txtBox);
frm.Show(nativeWnd);
What happens with above code is below:
Notification window is not positioned on the bottom right hand
corner of the compose window.
If I move compose window, notification window keeps in the same position and it is not moving while I move the compose window.
In windows task bar it appears as the notification window is a different process/program, not being part of the same compose window, maybe I need to do something like frm.Owner = nativeWnd but it is not working.
Any ideas?
Use a timer which fires the Tick event on the main thread (UI) where you can call the Close method of your form. The System.Windows.Forms.Timer's event is raised from the UI thread by the message loop when it receives a WM_TIMER message. So, you are good to calling the Close method.
Note, the timer can be run and the form is closed from your code outside of the form or inside the form (built-in to the form).
You can create a custom Windows form that closes itself automatically after a timeout.
To make your form a child of an Outlook inspector, Q/cast the Inspector object (e.g. from Application.ActiveInspector) to the IOleWindow interface, call IOleWindow.GetWindow to get the HWND. Create an instance of the System.Windows.Forms.NativeWindow class and call NativeWindow.AssignHandle. You can then pass the NativeWindow object to Form.Show or Form.ShowModal (NativeWindow object implements the IWin32Window interface).
I have a problem that I couldn't be able to fix since one week, I hope someone could have ever experienced this.
I'm using SharpDX with a windows form project, basically there is a form with a picturebox and some panel over it.
The typical Swapchain.Present(1,PresentFlags.None) works good when I zoom or translate the image. However I get this strange situation where when I minimize my screen and reopen it back, all the swapchain content is hidden like if it has been erased. However, I know it hasn't and this is probably a race condition between GDI refresh and SharpDX.
So, I tried to overrides WndProc(Message m) in every control and handle it's paint and eraseBackground event. This is not working. When I debug, windows message are always different from time to time so it is quite hard to figure out what could be wrong.
If anyone has ever experience this behavior while mixing SharpDX(or directX) on Windows form, I would really like an answer.
Here is how I handle the resize event of my SharpDX DeviceContext
Public Overrides Sub Resize(Width As Integer, Height As Integer)
If m_SwapChain IsNot Nothing Then
If m_BackBuffer IsNot Nothing Then
m_BackBuffer.Dispose()
End If
If m_2DDeviceContext IsNot Nothing Then
m_2DDeviceContext.Dispose()
End If
If m_2DTarget IsNot Nothing Then
m_2DTarget.Dispose()
End If
m_SwapChain.ResizeBuffers(2, Width, Height, Format.B8G8R8A8_UNorm, SwapChainFlags.GdiCompatible)
m_BackBuffer = m_SwapChain.GetBackBuffer(Of Surface)(0)
m_2DDeviceContext = New SharpDX.Direct2D1.DeviceContext(m_2DDevice, SharpDX.Direct2D1.DeviceContextOptions.EnableMultithreadedOptimizations)
m_2DTarget = New SharpDX.Direct2D1.Bitmap(m_2DDeviceContext, m_BackBuffer, m_Properties)
m_2DDeviceContext.AntialiasMode = AntialiasMode.PerPrimitive
m_2DDeviceContext.Target = m_2DTarget
CType(m_Context, GPUDrawingContext).DeviceContext = m_2DDeviceContext
End If
End Sub
EDIT :
After trying many stuff, I realize it was happening right when I call base.Wndproc(m) where m = WM_PAINT.
I can't ignore the paint message because if I do, my control is still invalidated and it will add a new WM_PAINT to the event queue and send me into an infinite loop. I really cannot do base.Wndproc(m) because this is where my swapchain gets hide.
Is their any way to handle(ignore) this event message without having it back in the loop because SharpDX doesn't require this function since it's a paint layer over the control.
When you just want to resize the swapchain you don't have to recreate the DeviceContext. Maybe here is the first cause of your problem, because the swapchain was created with a different DeviceContext than the backbuffer.
For me this is working in C#:
first add eventhandlers to the resize events of the control. No need to override the WM_PAINT:
form.ResizeBegin += (o, e) => {
formHeight = ((Form)o).Height;
formWidth = ((Form)o).Width;
};
form.ResizeBegin += (o, e) => {
isResizing = true;
};
form.ResizeEnd += (o, e) => {
isResizing = false;
HandleResize(o, e);
};
form.SizeChanged += HandleResize;
with this I can save the old size of the control for comparison. And I'm just resizing the swapchain after the resize is finished and not for every event (like when resizing a window). Also I will only resize if the control is not minimized:
private void HandleResize(object sender, System.EventArgs e) {
Form f = (Form)sender;
if ((f.ClientSize.Width != formWidth || f.ClientSize.Height != formHeight)
&& !isResizing
&& !(f.WindowState == FormWindowState.Minimized)) {
formWidth = f.ClientSize.Width;
formHeight = f.ClientSize.Height;
DoResize(formWidth, formHeight);
}
}
Finally DoResize is following (renderTarget is my custom class):
private void DoResize(int width, int height) {
renderTarget.Dispose();
swapChain.ResizeBuffers(1, width, height, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0)) {
//recreate the rendertarget with the new backbuffer
renderTarget.Resize(width, height, resource);
}
}
I would like to 'shake' my winforms form to provide user feedback, much like the effect used on a lot of mobile OS's.
I can obviously set the location of the window and go back and forth with Form1.Location.X etc. but the effect from this method is terrible. I'd like something a little more fluent - or alternatively is there a way to shake the entire screen?
I'll only be targeting Windows 7 using .net 4.5.
Update
Using both Hans and Vidstige suggestions I've come up with the following, which also works when the window is maximized - I wish I could pick two answers, I've up-voted your answer though Vidstige and hope others will too. Hans' answer hits all the salient points though.
Two forms MainForm and ShakeForm
MainForm Code
Private Sub shakeScreenFeedback()
Dim f As New Shakefrm
Dim b As New Bitmap(Me.Width, Me.Height, PixelFormat.Format32bppArgb)
Me.DrawToBitmap(b, Me.DisplayRectangle)
f.FormBorderStyle = Windows.Forms.FormBorderStyle.None
f.Width = Me.Width
f.Height = Me.Height
f.ShowInTaskbar = False
f.BackgroundImage = b
f.BackgroundImageLayout = ImageLayout.Center
f.Show(Me)
f.Location = New Drawing.Point(Me.Location.X, Me.Location.Y)
'I found putting the shake code in the formLoad event didn't work
f.shake()
f.Close()
b.Dispose()
End Sub
ShakeForm Code
Public Sub shake()
Dim original = Location
Dim rnd = New Random(1337)
Const shake_amplitude As Integer = 10
For i As Integer = 0 To 9
Location = New Point(original.X + rnd.[Next](-shake_amplitude, shake_amplitude), original.Y + rnd.[Next](-shake_amplitude, shake_amplitude))
System.Threading.Thread.Sleep(20)
Next
Location = original
End Sub
Have you tried something like this?
private void shakeButton_Click(object sender, EventArgs e)
{
Shake(this);
}
private static void Shake(Form form)
{
var original = form.Location;
var rnd = new Random(1337);
const int shake_amplitude = 10;
for (int i = 0; i < 10; i++)
{
form.Location = new Point(original.X + rnd.Next(-shake_amplitude, shake_amplitude), original.Y + rnd.Next(-shake_amplitude, shake_amplitude));
System.Threading.Thread.Sleep(20);
}
form.Location = original;
}
The typical problem is having way too many controls on the form, making the painting too slow. So just fake it, create a borderless window that displays a bitmap of the form and shake that one. Create the bitmap with the form's DrawToBitmap() method. Use 32bppPArgb for the pixel format, it draws ten times faster than all the other ones.
You can use the Aero Shake feature of Windows 7 to achieve this.
Better you can have a look in the below link for more details:
http://www.codeproject.com/Articles/36294/Aero-Shake
Here is a small work around, you may try it.
private void button1_Click(object sender, EventArgs e)
{
this.Width = this.Width - 10;
this.Height = this.Height - 10;
Thread.Sleep(15);
this.Width = this.Width + 10;
this.Height = this.Height + 10;
}
I'm developing a Windows Forms application in C# with an embedded WebBrowser control to "dummy-proof" (i.e. disable context menus, back button, free navigation, etc.) access to a third-party web application.
Right now I'm trying to add the Zoom feature to my custom browser. I have the keyboard combos working for it (CTRL + and CTRL - make the correct OLE calls to the underlying ActiveX WebBrowser object), but among the other frustrating things about WebBrowser I've had to deal with, I can't seem to figure out how to capture CTRL-Mouse wheel to simulate the Zoom function like IE does. I've looked everywhere to find a solution to this but to no avail.
To try to figure it out, I created an empty form with just the WebBrowser control in it, and found the following:
CTRL-MouseWheel always fires the MouseWheel event when the parent form has focus and the mouse cursor is hovering over the top of the window (e.g. over the title of the application), or when the mouse cursor is hovering over the WebBrowser control when it does not appear to have focus even though the parent form has focus.
CTRL-MouseWheel never fires the MouseWheel event when the mouse cursor is hovering over the WebBrowser control and WebBrowser has focus, and there seems to be no effect.
Using the mouse wheel without CTRL scrolls the window contents of WebBrowser but does not fire the MouseWheel event until the vertical scroll bar has fully reached either the top or the bottom.
Intercepting the Message for WM_MOUSEWHEEL by overriding WndProc and DefWndProc both for a sample class inherited from WebBrowser and for the parent form applies only for the above conditions (with wParam properly denoting MK_CONTROL).
The PreviewKeyDown event fires when CTRL is pressed, as expected, but still does nothing in unison with the mouse wheel.
So I guess the Message is being processed below the parent form and the managed control level and does not bubble up to where I can intercept or even handle it. Is there a way to do this, or some other way to simulate zooming in and out using CTRL-MouseWheel?
Thanks for reading!
First cast the WebBrowser.Document.DomDocument to the right interface in the mshtml namespace, like mshtml.HTMLDocumentEvents2_Event, then you can handle (and cancel) mousewheel events. I'm not sure, but I think you need to wire up the event handler anytime the document is changed, so I do it on the WebBrowser.DocumentCompleted event. I'm also not sure if you need to release any COM objects.
This was frustrating enough that I got it to work and stopped caring...
Here is at least one document explaining the basics: How to handle document events in a Visual C# .NET application
For your specific case, just conditionally squash the onmousewheel event, based on whether or not the CTRL key is pressed.
private void webBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser.Url.ToString() == "about:blank")
return;
var docEvents = (mshtml.HTMLDocumentEvents2_Event)webBrowser.Document.DomDocument;
docEvents.onmousewheel -= docEvents_onmousewheel; //may not be necessary?
docEvents.onmousewheel += docEvents_onmousewheel;
}
bool docEvents_onmousewheel(mshtml.IHTMLEventObj pEvtObj)
{
if (pEvtObj.ctrlKey)
{
pEvtObj.cancelBubble = true; //not sure what this does really
pEvtObj.returnValue = false; //this cancels the event
return false; //not sure what this does really
}
else
return true; //again not sure what this does
}
Now if you need to know the Wheel Delta (scroll amount), you'll want to cast the events object to yet another interface.
bool docEvents_onmousewheel(mshtml.IHTMLEventObj pEvtObj)
{
var wheelEventObj = (mshtml.IHTMLEventObj4)pEvtObj;
var delta = wheelEventObj.wheelDelta;
[...]
}
Perhaps using SetWindowsHookEx to look for these events may work for you. This is what I've used to get scroll wheel events on top of the ActiveX MapPoint control.
Be aware there are some quirks with this on Windows 7 that may require some tinkering. For more details do a search for SetWindowsHookEx on Windows 7 on the MSDN forums.
To solve this problem you have to listen for and handle these messages:
OLECMDID_GETZOOMRANGE
OLECMDID_OPTICAL_GETZOOMRANGE
OLECMDID_OPTICAL_ZOOM
OLECMDID_ZOOM
They're dispatched by Internet Explorer. See the remarks on MSDN.
This is the code I used to disable ctrl+shift: You need to change the behavior of WndProc in the deepest control "Internet Explorer_Server",
Do this after your web browser is ready:
IntPtr wbHandle = Win32.FindWindowEx(this.wbMain.Handle, IntPtr.Zero, "Shell Embedding", String.Empty);
wbHandle = Win32.FindWindowEx(wbHandle, IntPtr.Zero, "Shell DocObject View", String.Empty);
wbHandle = Win32.FindWindowEx(wbHandle, IntPtr.Zero, "Internet Explorer_Server", String.Empty);
WbInternal wb = new WbInternal(wbHandle);
class WbInternal : NativeWindow
{
public WbInternal(IntPtr handle)
{
this.AssignHandle(handle);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEWHEEL)
{
if (((int)m.WParam & 0x00FF) == MK_SHIFT)
{
return;
}
}
base.WndProc(ref m);
}
}
You can find more message about WM_MOUSEWHEEL from MSDN.
I find this from MSDN. But I forgot the link, Once find, will append it here.
I couldn't get any of these to work reliably so after some (frustrating) experimentation, I came up with a derivative of the answer posted by TCC. My webbrowser control is hosted in a usercontrol. The main differences are I use a class-level variable for the HTMLDocumentEvents2_Event so I can unsubscribe successfully, and I set the mshtml.IHTMLEventObj pEvtObj.Returnvalue to true.. seems to work well now..
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_wbData = (WebBrowser)FindElement("DataBrowser");
_horzScroll = (ScrollBar)FindElement("HorizontalScroll");
_vertScroll = (ScrollBar)FindElement("VerticalScroll");
_wbData.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(OnLoadCompleted);
_horzScroll.Scroll += new ScrollEventHandler(OnHorizontalScroll);
_vertScroll.Scroll += new ScrollEventHandler(OnVerticalScroll);
LoadDefault();
EnableSoundEffects(SoundEffects);
}
private void OnHorizontalScroll(object sender, ScrollEventArgs e)
{
// _wbData.Handle
mshtml.HTMLDocument htmlDoc = _wbData.Document as mshtml.HTMLDocument;
_horzPosition = (int)e.NewValue;
if (htmlDoc != null && htmlDoc.body != null)
htmlDoc.parentWindow.scroll(_horzPosition, _vertPosition);
}
private void OnVerticalScroll(object sender, ScrollEventArgs e)
{
mshtml.HTMLDocument htmlDoc = _wbData.Document as mshtml.HTMLDocument;
_vertPosition = (int)e.NewValue;
if (htmlDoc != null && htmlDoc.body != null)
htmlDoc.parentWindow.scroll(_horzPosition, _vertPosition);
}
private void OnLoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
mshtml.HTMLDocument htmlDoc = _wbData.Document as mshtml.HTMLDocument;
if (htmlDoc != null && htmlDoc.body != null)
{
mshtml.IHTMLElement2 body = (mshtml.IHTMLElement2)htmlDoc.body;
_horzScroll.Visibility = Visibility.Collapsed;
if (body.scrollHeight > _wbData.ActualHeight)
_vertScroll.Visibility = Visibility.Visible;
else
_vertScroll.Visibility = Visibility.Collapsed;
_vertScroll.ViewportSize = _wbData.ActualHeight;
_vertScroll.Maximum = body.scrollHeight - (_wbData.ActualHeight - 8);
_eventHelper = (HTMLDocumentEvents2_Event)_wbData.Document;
_eventHelper.onmousewheel -= OnMouseWheel;
_eventHelper.onmousewheel += new HTMLDocumentEvents2_onmousewheelEventHandler(OnMouseWheel);
}
}
private bool OnMouseWheel(mshtml.IHTMLEventObj pEvtObj)
{
mshtml.HTMLDocument htmlDoc = _wbData.Document as mshtml.HTMLDocument;
var wheelEventObj = (mshtml.IHTMLEventObj4)pEvtObj;
var delta = wheelEventObj.wheelDelta;
if (htmlDoc != null && htmlDoc.body != null && wheelEventObj != null)
{
_vertPosition += (int)wheelEventObj.wheelDelta;
htmlDoc.parentWindow.scroll(_horzPosition, _vertPosition);
}
pEvtObj.returnValue = true;
return true;
}
For any custom dialog (form) in a WinForm application I can set its size and position before I display it with:
form.StartPosition = FormStartPosition.Manual;
form.DesktopBounds = MyWindowPosition;
This is particularly important when dealing with multiple monitors. Without such code, when you open a dialog from an application that you have dragged to a second monitor, the dialog appears on the primary monitor. This presents a poor user experience.
I am wondering if there are any hooks to set the position for the standard .NET OpenFileDialog and SaveFileDialog (which do not have a StartPosition property).
I suspect that the best you can do is make sure you use the overload of ShowDialog that accepts an IWin32Window to use as the parent. This might help it choose an appropriate location; most commonly:
using(var dlg = new OpenFileDialog()) {
.... setup
if(dlg.ShowDialog(this) == DialogResult.OK) {
.... use
}
}
Check out this article on CodeProject. Excerpt:
Here is when the handy .NET
NativeWindow comes into the picture, a
NativeWindow is a window wrapper where
it processes the messages sent by the
handle associated to it. It creates a
NativeWindow and associates the
OpenFileWindow handle to it. From this
point, every message sent to
OpenFileWindow will be redirected to
our own WndProc method in the
NativeWindow instead, and we can
cancel, modify, or let them pass
through.
In our WndProc, we process the message
WM_WINDOWPOSCHANGING. If the open
dialog is opening, then we will change
the original horizontal or vertical
size depending of the StartLocation
set by the user. It will increment the
size of the window to be created. This
happens only once when the control is
opened.
Also, we will process the message
WM_SHOWWINDOW. Here, all controls
inside the original OpenFileDialog are
created, and we are going to append
our control to the open file dialog.
This is done by calling a Win32 API
SetParent. This API lets you change
the parent window. Then, basically
what it does is attach our control
to the original OpenFileDialog in the
location it set, depending on the
value of the StartLocation property.
The advantage of it is that we still
have complete control over the
controls attached to the
OpenFileDialog window. This means we
can receive events, call methods, and
do whatever we want with those
controls.
OpenFileDialog and SaveFileDialog position themselves in the upper-left corner of
the client area of the most recently displayed window. So just create a new invisible window positioned where you want the the dialog to appear before creating and showing that dialog.
Window dialogPositioningWindow = new Window();
dialogPositioningWindow.Left = MainWindow.Left + <left position within main window>;
dialogPositioningWindow.Top = MainWindow.Top + <top position within main window>;
dialogPositioningWindow.Width = 0;
dialogPositioningWindow.Height = 0;
dialogPositioningWindow.WindowStyle = WindowStyle.None;
dialogPositioningWindow.ResizeMode = ResizeMode.NoResize;
dialogPositioningWindow.Show();// OpenFileDialog is positioned in the upper-left corner
// of the last shown window (dialogPositioningWindow)
Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
...
if ((bool)dialog.ShowDialog()){
...
}
dialogPositioningWindow.Close();
I had this problem for most of yesterday. BobB's answer was the one that helped me out the most (Thanks BobB).
You can even go as far as to make a private method that creates a window and closes it before the dialog.ShowDialog() method call and it will still centre the OpenFileDialog.
private void openFileDialogWindow()
{
Window openFileDialogWindow = new Window();
openFileDialogWindow.Left = this.Left;
openFileDialogWindow.Top = this.Top;
openFileDialogWindow.Width = 0;
openFileDialogWindow.Height = 0;
openFileDialogWindow.WindowStyle = WindowStyle.None;
openFileDialogWindow.ResizeMode = ResizeMode.NoResize;
openFileDialogWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
openFileDialogWindow.Show();
openFileDialogWindow.Close();
openFileDialogWindow = null;
}
Then call it in any method before the ShowDialog() method.
public string SelectWebFolder()
{
string WebFoldersDestPath = null;
CommonOpenFileDialog filePickerDialog = new CommonOpenFileDialog();
// OpenFileDialog Parameters..
openFileDialogWindow();
if (filePickerDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
WebFoldersDestPath = filePickerDialog.FileName + "\\";
}
filePickerDialog = null;
return WebFoldersDestPath;
}
Here's how I did it:
The point where I want to display the OpenFileDialog:
Thread posThread = new Thread(positionOpenDialog);
posThread.Start();
DialogResult dr = ofd.ShowDialog();
The repositioning code:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
/// <summary>
/// Find the OpenFileDialog window when it appears, and position it so
/// that we can see both dialogs at once. There is no easier way to
/// do this (&^%$! Microsoft!).
/// </summary>
private void positionOpenDialog ()
{
int count = 0;
IntPtr zero = (IntPtr)0;
const int SWP_NOSIZE = 0x0001;
IntPtr wind;
while ((wind = FindWindowByCaption(zero, "Open")) == (IntPtr)0)
if (++count > 100)
return; // Find window failed.
else
Thread.Sleep(5);
SetWindowPos(wind, 0, Right, Top, 0, 0, SWP_NOSIZE);
}
I start a thread that looks for a window with the "Open" title. (Typically found in 3 iterations or 15 milliseconds.) Then I set its position with the obtained handle. (See SetWindowPos documentation for the position/size parameters.)
Kludgy.
There is quite an old example of one approach on MSDN.
http://msdn.microsoft.com/en-us/library/ms996463.aspx
It includes all the code needed to implement your own OpenFileDialog class that allows extensibility.
Very grateful for BobB's reply on this one. There are a few more "gotchas". You have to pass the handle of PositionForm when calling OpenFileDialog1.ShowDialog(PositionForm) otherwise BobB's technique is not reliable in all cases. Also, now that W8.1 launches a new fileopen control with SkyDrive in it, the Documents folder location in the W8.1 fileopen control is now screwed. So I frig fileopen to use the old W7 control by setting ShowHelp = True.
Here is the VB.NET code I ended up using, my contribution to the community in case it helps.
Private Function Get_FileName() As String
' Gets an Input File Name from the user, works with multi-monitors
Dim OpenFileDialog1 As New OpenFileDialog
Dim PositionForm As New Form
Dim MyInputFile As String
' The FileDialog() opens in the last Form that was created. It's buggy! To ensure it appears in the
' area of the current Form, we create a new hidden PositionForm and then delete it afterwards.
PositionForm.StartPosition = FormStartPosition.Manual
PositionForm.Left = Me.Left + CInt(Me.Width / 2)
PositionForm.Top = Me.Top + CInt(Me.Height / 2)
PositionForm.Width = 0
PositionForm.Height = 0
PositionForm.FormBorderStyle = Forms.FormBorderStyle.None
PositionForm.Visible = False
PositionForm.Show()
' Added the statement "ShowHelp = True" to workaround a problem on W8.1 machines with SkyDrive installed.
' It causes the "old" W7 control to be used that does not point to SkyDrive in error.
OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
OpenFileDialog1.Filter = "Excel files (*.xls*)|*.xls*|CSV Files (*.csv)|*.csv"
OpenFileDialog1.FilterIndex = 1
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.AutoUpgradeEnabled = False
OpenFileDialog1.ShowHelp = True
OpenFileDialog1.FileName = ""
OpenFileDialog1.SupportMultiDottedExtensions = False
OpenFileDialog1.Title = "Select an Excel or .csv file containing patent data or list of Publication Numbers for your project."
If OpenFileDialog1.ShowDialog(PositionForm) <> System.Windows.Forms.DialogResult.OK Then
Console.WriteLine("No file was selected. Please try again!")
PositionForm.Close()
PositionForm.Dispose()
OpenFileDialog1.Dispose()
Return ""
End If
PositionForm.Close()
PositionForm.Dispose()
MyInputFile = OpenFileDialog1.FileName
OpenFileDialog1.Dispose()
Return MyInputFile
End Function
Using Rob Sherrit's response on Jan 22 '14 as inspiration, I created a new module and called it CKRFileDialog (call it what you want) which contains the following code:
Public Function Show(fd As Object, CoveredForm As Form, Optional bShowHelp As Boolean = False) As DialogResult
Dim oDR As DialogResult
'The .Net FileDialogs open in the last Form that was created.
'To ensure they appear in the area of the current Form, we create a new HIDDEN PositionForm and then
'delete it afterwards.
Dim PositionForm As New Form With {
.StartPosition = FormStartPosition.Manual,
.Left = CoveredForm.Left + CInt(CoveredForm.Width / 8), 'adjust as required
.Top = CoveredForm.Top + CInt(CoveredForm.Height / 8), 'adjust as required
.Width = 0,
.Height = 0,
.FormBorderStyle = Windows.Forms.FormBorderStyle.None,
.Visible = False
}
PositionForm.Show()
'If you use SkyDrive you need to ensure that "bShowHelp" is set to True in the passed parameters.
'This is a workaround for a problem on W8.1 machines with SkyDrive installed.
'Setting it to "true" causes the "old" W7 control to be used which avoids a pointing to SkyDrive error.
'If you do not use SkyDrive then simply do not pass the last parameter (defaults to "False")
fd.ShowHelp = bShowHelp
'store whether the form calling this routine is set as "topmost"
Dim oldTopMost As Integer = CoveredForm.TopMost
'set the calling form's topmost setting to "False" (else the dialogue will be "buried"
CoveredForm.TopMost = False
oDR = fd.ShowDialog(PositionForm)
'set the "topmost" setting of the calling form back to what it was.
CoveredForm.TopMost = oldTopMost
PositionForm.Close()
PositionForm.Dispose()
Return oDR
End Function
I then call this code in my various modules as follows:
If performing a "FileOpen" ensure that there is a FileOpenDialog component added to your form or code and adjust the properties of the component if you wish
(e.g. InitDirectory,Multiselect,etc.)
Do the same when using FileSaveDialog components (Different properties to the FileOpenDialog component may apply).
To "show" the dialog component use a line of code as follows, passing two parameters, the first the FileDialog you are using ("Open" or "Save") and the second parameter the Form upon which you wish to overlay the dialogue.
CKRFileDialog.Show(saveFileDialog1, CoveredForm)
or
CKRFileDialog.Show(openFileDialog1, CoveredForm)
Remember, if you are using SkyDrive you must pass "True" as a third parameter:
CKRFileDialog.Show(saveFileDialog1, CoveredForm, True)
or
CKRFileDialog.Show(openFileDialog1, CoveredForm, True)
I set the "offset" of the dialogue to be 1/8 the way across and down on the form
"CoveredForm", but you can set that back to 1/2 (as in Rob Sherret's code) or whatever value you wish.
This seemed the easiest approach
Thanks Rob! :-)