怎么让C#winfrmqq会话窗口自动关闭闭

关于C#windows窗体应用程序窗口的问题_百度知道
关于C#windows窗体应用程序窗口的问题
具体怎么实现。在C#windows窗体应用程序中怎么实现从菜单项打开的子窗口都在一个主窗口上显示。,主窗口显示新的子窗口时!需要用什么控件,之前显示的一个子窗口就会关闭。谢谢做毕业设计急用
提问者采纳
&&&&&&&nbsp,而不是作为弹出窗体;FrmAddUser(),将父窗体作为容器;&FrmAddUser&nbsp.Windows,但弹出窗体看起来效果都不是很好.TopLevel&new&frmAddUser&&&nbsp.TopL/*这样一来;&&&&nbsp.Add(frmAddUser).FormBorderS&&&//移除所有控件&&nbsp.C&&nbsp.FormBorderS&nbsp.Controls.Show();&nbsp,然后将要弹出的窗体作为Panel的控件显示到Panel上:*/&nbsp.Forms.将弹出窗体显示在父窗体中;&nbsp.Clear(),获取或设置窗体的边框样式;frmAddUser.具体代码可参考如下.W&=&&&&&&&nbsp,可在父窗体中加入一个Panel(或其他容器控件);S&&nbsp,获取或设置一个值,后者设为S&nbsp.FormBorderStyle&&nbsp,把From显示在Panel中/*当我们在做WinForm的程序时;&&&nbsp.但此处要设置子窗体的两个属性;&&&pnlOPC&&false.FfrmAddUser.FormBorderSfrmAddU&nbsp。将前者设为&&&&&=&&&nbsp,该值指示是否将窗体显示为顶级窗口。2;&nbsp,特别是弹出的窗体很多时;&&&&&nbsp可以用Panel作容器.N&nbsp:1;&&=&nbsp.我们可以用这样的方法来解决此问题;&&&&&pnlOPC&&nbsp,总是要弹出一些窗体.N&nbsp,这时候就会影响整个软件的用户体验;&nbsp,将字窗体显示到父窗体中;&&nbsp.所以还是要尽量的避免弹出窗体,就避免了弹出窗体带来不好的用户体验了
怎么用。。。这段代码加在哪?
上面代码是菜单项点击事件你原来点击菜单后的弹出窗体的应该是:Form1&frm=new&Form1();frm.Show();现在改成:panel1.Controls.Clear();//移除所有控件Form1&frm=new&Form1();frm.TopLevel&=&frm.FormBorderStyle&=&System.Windows.Forms.FormBorderStyle.Npanel1.Controls.Add(frm);frm.Show();
提问者评价
其他类似问题
tabControl控件作为应用程序内嵌窗口,以页的形式管理不同的菜单项打开的窗口
为您推荐:
应用程序的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁如何在winfrm程序中打开pdf文件,要求嵌入到form中。_c#吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:87,502贴子:
如何在winfrm程序中打开pdf文件,要求嵌入到form中。收藏
如何在winfrm程序中打开pdf文件,要求嵌入到form中。可以用第三方控件。
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或领导让搞这个,我没用过c#,表示特么的一点不会,问领导什么也得不到答案,只好边研究变做了。
有什么不对就指出来。
动态字段做之前怎么也做不出来,愁死。
后来发现其实挺简单,似乎只要是多个重复字段的数据,就可以自动排出来,只要把他们放在section3(详细资料):
c0到c14是将要重复输出的字段(数据不重复,如List&CCC&,CCC含有c0到c14这些属性)。然后将& section3(详细资料)& 的高度压紧,因为重复的时候,行高就是section3的高度,不压紧将会留白。
这样,就可以动态字段了,虽然还是有点局限的感觉
然后我就发现,我有一张图片(已转byte[])没有办法放进去了,直接放进CCC会不会出现重复什么什么的。结果实践后发现,其实不放在section3(详细资料)就不会重复,于是我把这个字段放在 section1(报表头)中。并且发现,不用每个CCC都为图片字段赋值,只要list的第一个CCC的图片字段有值就可以了。
经过测试发现,在 section5(页脚) 也是一样的,但他要在list的最后一个CCC里。
我在实际使用时,并不是用的list,而是DataTable,在上面的每个CCC都是DataTable的里的行,每个字段就是DataTable里的列。
我根本不知道用List行不行,list就是一个坑。。。
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区winforms - C# Force Form Focus - Stack Overflow
to customize your list.
Announcing Stack Overflow Documentation
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
So, I did search google and SO prior to asking this question. Basically I have a DLL that has a form compiled into it. The form will be used to display information to the screen. Eventually it will be asynchronous and expose a lot of customization in the dll. For now I just want it to display properly. The problem that I am having is that I use the dll by loading it in a Powershell session. So when I try to display the form and get it to come to the top and have focus, It has no problem with displaying over all the other apps, but I can't for the life of me get it to display over the Powershell window. Here is the code that I am currently using to try and get it to display. I am sure that the majority of it won't be required once I figure it out, this just represents all the things that I found via google.
CLass Blah
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll", EntryPoint = "ShowWindowAsync")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
private const int WS_SHOWNORMAL = 1;
public void ShowMessage(string msg)
MessageForm msgFrm = new MessageForm();
msgFrm.lblMessage.Text = "FOO";
msgFrm.ShowDialog();
msgFrm.BringToFront();
msgFrm.TopMost =
msgFrm.Activate();
SystemParametersInfo((uint)0x, 0x0002 | 0x0001);
ShowWindowAsync(msgFrm.Handle, WS_SHOWNORMAL);
SetForegroundWindow(msgFrm.Handle);
SystemParametersInfo((uint)0x, x0002 | 0x0001);
As I say I'm sure that most of that is either not needed or even flat out wrong, I just wanted to show the things that I had tried. Also, as I mentioned, I plan to have this be asynchronously displayed at some point which I suspect will wind up requiring a separate thread. Would splitting the form out into it's own thread make it easier to cause it to get focus over the Powershell session?
@Joel, thanks for the info. Here is what I tried based on your suggestion:
msgFrm.ShowDialog();
msgFrm.BringToFront();
msgFrm.Focus();
Application.DoEvents();
The form still comes up under the Powershell session. I'll proceed with working out the threading. I've spawned threads before but never where the parent thread needed to talk to the child thread, so we'll see how it goes.
Thnks for all the ideas so far folks.
Ok, threading it took care of the problem. @Quarrelsome, I did try both of those. Neither (nor both together) worked. I am curious as to what is evil about using threading? I am not using Application.Run and I have yet to have a problem. I am using a mediator class that both the parent thread and the child thread have access to. In that object I am using a ReaderWriterLock to lock one property that represents the message that I want displayed on the form that the child thread creates. The parent locks the property then writes what should be displayed. The child thread locks the property and reads what it should change the label on the form to. The child has to do this on a polling interval (I default it to 500ms) which I'm not real happy about, but I could not find an event driven way to let the child thread know that the proerty had changed, so I'm stuck with polling.
15.1k72047
18.8k64768
I also had trouble activating and bringing a window to the foreground. Here is the code that eventually worked for me. I'm not sure if it will solve your problem.
Basically, call ShowWindow() then SetForegroundWindow().
using System.D
using System.Runtime.InteropS
// Sets the window to be foreground
[DllImport("User32")]
private static extern int SetForegroundWindow(IntPtr hwnd);
// Activate or minimize a window
[DllImportAttribute("User32.DLL")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_RESTORE = 9;
private void ActivateApplication(string briefAppName)
Process[] procList = Process.GetProcessesByName(briefAppName);
if (procList.Length & 0)
ShowWindow(procList[0].MainWindowHandle, SW_RESTORE);
SetForegroundWindow(procList[0].MainWindowHandle);
2,25632242
2,80652033
Here is some code that I've used on one form or another for a few years. There are a few gotchas to making a window in another app pop up. Once you have the window handle do this:
if (IsIconic(hWnd))
ShowWindowAsync(hWnd, SW_RESTORE);
ShowWindowAsync(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
// Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
// Converted to Delphi by Ray Lischner
// Published in The Delphi Magazine 55, page 16
// Converted to C# by Kevin Gale
IntPtr foregroundWindow = GetForegroundWindow();
IntPtr Dummy = IntPtr.Z
uint foregroundThreadId = GetWindowThreadProcessId(foregroundWindow, Dummy);
uint thisThreadId
= GetWindowThreadProcessId(hWnd, Dummy);
if (AttachThreadInput(thisThreadId, foregroundThreadId, true))
BringWindowToTop(hWnd); // IE 5.5 related hack
SetForegroundWindow(hWnd);
AttachThreadInput(thisThreadId, foregroundThreadId, false);
if (GetForegroundWindow() != hWnd)
// Code by Daniel P. Stasinski
// Converted to C# by Kevin Gale
IntPtr Timeout = IntPtr.Z
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, Timeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Dummy, SPIF_SENDCHANGE);
BringWindowToTop(hWnd); // IE 5.5 related hack
SetForegroundWindow(hWnd);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Timeout, SPIF_SENDCHANGE);
I won't post the whole unit since since it does other things that aren't relevant
but here are the constants and imports for the above code.
//Win32 API calls necesary to raise an unowned processs main window
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("user32.dll")]
static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, ref Int32 lpdwProcessId);
[DllImport("User32.dll")]
public static extern IntPtr GetParent(IntPtr hWnd);
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_NORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_MAXIMIZE = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_SHOWMINNOACTIVE = 7;
private const int SW_SHOWNA = 8;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
private const int SW_MAX = 10;
private const uint SPI_GETFOREGROUNDLOCKTIMEOUT = 0x2000;
private const uint SPI_SETFOREGROUNDLOCKTIMEOUT = 0x2001;
private const int
SPIF_SENDCHANGE = 0x2;
2,66111329
2,25742130
Doesn't ShowDialog() have different window behavior than just Show()?
What if you tried:
msgFrm.Show();
msgFrm.BringToFront();
msgFrm.Focus();
1,13631428
.Activate() ?
Either of those any good?
Splitting it out into its own thread is a bit evil as it wont work properly if you don't call it with Application.Run and that will swallow up the thread. In the worst case scenario I guess you could separate it out into a different process and communicate via the disk or WCF.
17.4k84588
The following solution should meet your requirements:
Assembly can be loaded into PowerShell and main class instantiated
When ShowMessage method on this instance is called, a new window is shown and activated
If you call ShowMessage multiple times, this same window updates its title text and is activated
To stop using the window, call Dispose method
Step 1: Let's create a temporary working directory (you can naturally use your own dir)
(powershell.exe)
mkdir C:\TEMP\PshWindow
cd C:\TEMP\PshWindow
Step 2: Now let's define class that we will be interacting with in PowerShell:
// file 'InfoProvider.cs' in C:\TEMP\PshWindow
using System.T
using System.Windows.F
namespace PshWindow
public sealed class InfoProvider : IDisposable
public void Dispose()
GC.SuppressFinalize(this);
lock (this._sync)
if (!this._disposed)
this._disposed =
if (null != this._worker)
if (null != this._form)
this._form.Invoke(new Action(() =& this._form.Close()));
this._worker.Join();
this._form =
this._worker =
public void ShowMessage(string msg)
lock (this._sync)
// make sure worker is up and running
if (this._disposed) { throw new ObjectDisposedException("InfoProvider"); }
if (null == this._worker)
this._worker = new Thread(() =& (this._form = new MyForm(this._sync)).ShowDialog()) { IsBackground = true };
this._worker.Start();
while (this._form == null || !this._form.Created)
Monitor.Wait(this._sync);
// update the text
this._form.Invoke(new Action(delegate
this._form.Text =
this._form.Activate();
private bool _
private Form _
private Thread _
private readonly object _sync = new object();
As well as the Form that will be shown:
// file 'MyForm.cs' in C:\TEMP\PshWindow
using System.D
using System.T
using System.Windows.F
namespace PshWindow
internal sealed class MyForm : Form
public MyForm(object sync)
this._sync =
this.BackColor = Color.LightG
this.Width = 200;
this.Height = 80;
this.FormBorderStyle = FormBorderStyle.SizableToolW
protected override void OnShown(EventArgs e)
base.OnShown(e);
this.TopMost =
lock (this._sync)
Monitor.PulseAll(this._sync);
private readonly object _
Step 3: Let's compile the assembly...
(powershell.exe)
csc /out:PshWindow.dll /target:library InfoProvider.cs MyForm.cs
Step 4: ... and load the assembly in PowerShell to have fun with it:
(powershell.exe)
[System.Reflection.Assembly]::LoadFile('C:\TEMP\PshWindow\PshWindow.dll')
$a = New-Provider
$a.ShowMessage('Hello, world')
A green-ish window with title 'Hello, world' should now pop-up and be active. If you reactivate the PowerShell window and enter:
$a.ShowMessage('Stack overflow')
The Window's title should change to 'Stack overflow' and the window should be active again.
To stop working with our window, dispose the object:
$a.Dispose()
This solution works as expected in both Windows XP SP3, x86 and Windows Vista SP1, x64. If there are question about how this solution works I can update this entry with detailed discussion. For now I'm hoping the code if self-explanatory.
8,36732742
Huge thanks people.
I think I've made it a bit shorter, here's what I put on a seperate thread and seems to be working ok.
private static void StatusChecking()
IntPtr iActiveForm = IntPtr.Zero, iCurrentACtiveApp = IntPtr.Z
Int32 iMyProcID = Process.GetCurrentProcess().Id, iCurrentProcID = 0;
IntPtr iTmp = (IntPtr)1;
while (bIsRunning)
Thread.Sleep(45);
if (Form.ActiveForm != null)
iActiveForm = Form.ActiveForm.H
iTmp = GetForegroundWindow();
if (iTmp == IntPtr.Zero)
GetWindowThreadProcessId(iTmp, ref iCurrentProcID);
if (iCurrentProcID == 0)
iCurrentProcID = 1;
if (iCurrentProcID != iMyProcID)
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, IntPtr.Zero, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, IntPtr.Zero, SPIF_SENDCHANGE);
BringWindowToTop(iActiveForm);
SetForegroundWindow(iActiveForm);
else iActiveForm = iT
catch (Exception ex)
Definitions.UnhandledExceptionHandler(ex, 103106);
I don`t bother repasting the definitions ...
Ivan Arabadzhiev
You shouldn't need to import any win32 functions for this.
If .Focus() isn't enough the form should also have a .BringToFront() method you can use.
If that fails, you can set it's .TopMost property to true.
You don't want to leave it true forever, so then call Application.DoEvents so the form can process that message and set it back to false.
244k88437657
Don't you just want the dialog to be a child of the calling form?
To do that you'll need the pass in the calling window and
use the ShowDialog( IWin32Window owner ) method.
3,04411740
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 qq会话窗口自动关闭 的文章

 

随机推荐