本文介绍了常用对话框中不显示按钮窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用API​​CreateWindowEx在常用对话框上创建了按钮窗口,它已成功创建。 PageSetupDialog对象创建并调用ShowDialog()函数。 来自其他线程的我调用的方法是使用以下语法创建按钮窗口。 我使用以下语法: IntPtr childwind = CreateWindowEx(WindowStylesEx.WS_EX_LEFT,BUTTON,Print, WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD, 0,0,width,height,hwnd,IntPtr.Zero,IntPtr.Zero,IntPtr.Zero); 我使用GetChildwindow检查并且窗口计数增加了1,但是对话框中没有显示按钮窗口。 我缺少任何重要的一步?请告诉我相同的解决方案。 附加信息: 当我使用相同的语法并向Form添加窗口时,它工作正常。 编辑:回答的代码移到这里 你好SAKryukov, 我和你分享我的代码。 public partial class Form1:Form { [DllImport( user32.dll,SetLastError = true )] public static extern IntPtr FindWindow( string strClassName, string strWindowName); [DllImport( user32.dll,SetLastError = true )] 静态 extern IntPtr CreateWindowEx( WindowStylesEx dwExStyle, string lpClassName, string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); [DllImport( user32.dll,SetLastError = true )] 静态 extern int ShowWindow( IntPtr Hwnd, int iCmdShow); [DllImport( user32.dll,SetLastError = true )] 静态 extern bool UpdateWindow( IntPtr hWnd); 线程mythread; PageSetupDialog dlg; public Form1() { InitializeComponent(); dlg = new PageSetupDialog(); } private void button1_Click( object sender,EventArgs e) { dlg.Document = new System.Drawing.Printing.PrintDocument (); mythread = new 线程( new ThreadStart(TestMethod)); mythread.Start(); dlg.ShowDialog(); } void TestMethod() { IntPtr hWndPageSetup = FindWindow( null ,dlg.ToString()); Boolean finPageSetupHandle = false ; while (!finPageSetupHandle&& hWndPageSetup == IntPtr .Zero) { System.Threading.Thread.Sleep( 500 ); hWndPageSetup = FindWindow( null , 页面设置); finPageSetupHandle = true ; } IntPtr childwind = CreateWindowEx(WindowStylesEx.WS_EX_LEFT, BUTTON, Print, WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD, 0 , 0 , 50 , 50 ,hWndPageSetup, IntPtr .Zero, IntPtr .Zero, IntPtr .Zero); ShowWindow(childwind, 1 ); UpdateWindow(childwind); } } 解决方案 正如其他人告诉你的那样,这是一个坏主意。不要这样做。停止。重新考虑你的设计。 因为你要继续前进并且无论如何都要这样做。 您需要重新编写代码。你正在创建一个线程,为什么你这样做仍然是一个谜,并创建按钮。确实正在创建按钮,正在创建一些东西。当代码到达TestMethod的末尾时,按钮的句柄超出了范围。 * poof * no按钮然后显示对话框。 如果需要自定义对话框,则更好地创建一个新窗口作为对话框。您可能会在不同版本的Windows上或在框架更改时遇到麻烦。一个新的对话框应该不是什么大问题。 请看我对问题和所有其他答案的评论,它们是非常合理的。而不是做你正在做的事情,开发一个表单类并使用它。如果需要将其显示为模式对话框,请使用以下方法: http: //msdn.microsoft.com/en-us/library/c7ykbedk%28v=vs.110%29.aspx [ ^ ]。 如果你需要将其显示为常规表单,使用 System.Windows.Forms.Application 的一部分使用/en-us/library/system.windows.forms.form.show%28v=vs.110%29.aspx\">http://msdn.microsoft.com/en-us/library/system.windows.forms。 form.show%28v = vs.110%29.aspx [ ^ ]。 使用P / Invoke时并不是非常需要创建其他问题,其中之一是 - 破坏代码的平台兼容性。 - SA Hi,I created button window on common dialog using API "CreateWindowEx" , it is created successfully.PageSetupDialog object created and called ShowDialog() function.from other thread I call method which is creating button window using following syntax.I used following syntax: IntPtr childwind = CreateWindowEx(WindowStylesEx.WS_EX_LEFT, "BUTTON", "Print", WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD, 0, 0, width, height, hwnd, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);I checked using GetChildwindow and window count is increase by one , but button window is not displayed on dialog.I am missing any important step ? Please let me know solution for same.Additional Info:when I use same syntax and add window to Form , it is working fine.Edit: Code from answer moved hereHi SAKryukov, I am sharing my code with you. public partial class Form1 : Form { [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr FindWindow(string strClassName, string strWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr CreateWindowEx( WindowStylesEx dwExStyle, string lpClassName, string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); [DllImport("user32.dll", SetLastError = true)] static extern int ShowWindow(IntPtr Hwnd, int iCmdShow); [DllImport("user32.dll", SetLastError = true)] static extern bool UpdateWindow(IntPtr hWnd); Thread mythread; PageSetupDialog dlg; public Form1() { InitializeComponent(); dlg = new PageSetupDialog();} private void button1_Click(object sender, EventArgs e) { dlg.Document = new System.Drawing.Printing.PrintDocument(); mythread = new Thread(new ThreadStart(TestMethod)); mythread.Start(); dlg.ShowDialog(); } void TestMethod() { IntPtr hWndPageSetup = FindWindow(null, dlg.ToString()); Boolean finPageSetupHandle = false; while (!finPageSetupHandle && hWndPageSetup == IntPtr.Zero) { System.Threading.Thread.Sleep(500); hWndPageSetup = FindWindow(null, "Page Setup"); finPageSetupHandle = true; } IntPtr childwind = CreateWindowEx(WindowStylesEx.WS_EX_LEFT, "BUTTON", "Print", WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD, 0, 0, 50, 50, hWndPageSetup, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); ShowWindow(childwind, 1); UpdateWindow(childwind);}} 解决方案 As others have told you, this is a bad idea. Don't do it. Stop. Rethink your design.And since you are going to go right ahead and do it anyway.You need to rework your code. You are creating a thread, why you are doing that remains a mystery, and creating the button. The button is indeed being created, well something is being created. When the code gets to the end of the "TestMethod", the handle to the button is going out of scope. *poof* no button and then the dialog is being shown.If you need customization of the dialog, much better to create a new window as the dialog. You may get yourself into trouble on different versions of Windows, or when the Framework changes. A new dialog should not be a big deal.Please see my comment to the question and all other answers, they are quite reasonable. Instead of doing what you are doing, develop a form class and use it. If you need to show it a a modal dialog, use this method: http://msdn.microsoft.com/en-us/library/c7ykbedk%28v=vs.110%29.aspx[^].If you need to show it as a regular form, use it as a part of System.Windows.Forms.Application using http://msdn.microsoft.com/en-us/library/system.windows.forms.form.show%28v=vs.110%29.aspx[^].Using P/Invoke when it is not really badly needed create additional problems, one of them — compromising platform compatibility of your code.—SA 这篇关于常用对话框中不显示按钮窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 13:01