Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 here
Hi SAKryukov,

I am sharing my code with you.
C#
 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); 
} 
}
Posted
Updated 26-Mar-12 13:34pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Mar-12 2:01am    
I'm afraid, there is not enough information. You need to show more complete code sample.
--SA
Somnath T Avhad 26-Mar-12 3:44am    
[For better readability, this code is removed from this comment, as it is already put in the text of the question — SA]
Sergey Alexandrovich Kryukov 29-Dec-13 22:05pm    
Thank you for showing the code. To do something practical, you need to replace your code with pure .NET FCL class library. There is no justification of using P/Invoke even in more obvious questions. Please see all the answers; and I'll add another one.
—SA

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Dec-13 22:11pm    
Agree, a 5; I also added Solution 4 to explain why using P/Invoke is bad, in cases when one can work around without using it.
—SA
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Dec-13 22:11pm    
Agree, a 5; I also added Solution 4 to explain why using P/Invoke is bad, in cases when one can work around without using it.
—SA
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
 
Share this answer
 
Edit: Code added by OP moved to the question
 
Share this answer
 
v2
Comments
Nelek 26-Mar-12 19:33pm    
Next time please add your code using the "improve question" link at the bottom of your message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900