您的位置:程序门 -> .net技术 -> c#



如何给打印对话框的“确定”按钮发送消息?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何给打印对话框的“确定”按钮发送消息?[已结贴,结贴人:yunsongzh]
发表于:2007-01-04 10:28:14 楼主
我现在使用水晶报表打印,.net   c#中的crystalreportviewer控件有一个printreport()函数,但它不是直接打印,而是弹出打印对话框,当你点击打印对话框的“确定”按钮后才执行打印。但我现在有几百条记录,不可能这样点一次按钮打印一张,想用循环做,但怎么给那个打印对话框的“确定”按钮发送消息?谁能告诉我,谢谢了!
发表于:2007-01-04 12:32:261楼 得分:50
做法:
findwindow抓到dlg
findwindowex抓到button
sendmessage发消息

以上都是win32函数,你查一下便知道了.c#本身作不了,要用这些函数
发表于:2007-01-04 14:20:212楼 得分:0
谢谢老兄了,但能不能给点原码看看,我技术功底太差了
发表于:2007-01-04 20:34:203楼 得分:0
可以啊,首先这三个是api函数,所以你要dllimport
第二
我来介绍一下几个函数把
hwnd   findwindow(
    lpctstr   lpclassname,     //   pointer   to   class   name
    lpctstr   lpwindowname     //   pointer   to   window   name
);
lpclassname是窗体类,你不知道可以不写,null就可以了
lpwindowname是你的窗体title这个你可以找到了把

hwnd   findwindowex(
    hwnd   hwndparent,             //   handle   to   parent   window
    hwnd   hwndchildafter,     //   handle   to   a   child   window
    lpctstr   lpszclass,         //   pointer   to   class   name
    lpctstr   lpszwindow         //   pointer   to   window   name
);

hwndparent:前面一个返回值给它
lpctstr   lpszclass,         //   pointer   to   class   name
lpctstr   lpszwindow         //   pointer   to   window   name
对于你要的button,lpszclass   =   “button”
lpszwindow则是“确定”

sendmessage第一个用上面的hwnd返回,第二个消息可能要查查了
发表于:2007-01-04 21:41:134楼 得分:0
我现在知道你的意思了,但我是在.net   c#中编程的,按照你说的,我写了下面几行代码:
using   system.runtime.interopservices;

[dllimport( "user32.dll ")]
public   static   extern   void   findwindow   (string   strclassname,   string   strwindowsname);

[dllimport( "user32.dll ")]
public   static   extern   int   sendmessage   (intptr   hwnd,   int   wmsg,   intptr   wparam,   intptr   lparam);

但编译时有这样的错误:应输入   class、delegate、enum、interface   或   struct
这是怎么回事呀?应该怎么改?
另外,不知你会不会在c#中使用水晶报表,你知道怎么动态创建水晶报表吗?谢谢了!

发表于:2007-01-04 22:57:445楼 得分:0
还有,标准打印对话框是以   showdialog()   形式出现的,不同于其它以   show()   形式弹出的窗口,向它发送消息它能收到吗?
发表于:2007-01-05 08:43:426楼 得分:0
1)我看你的写法没有错误,估计是其他地方的错,建议:把上面这几个函数放在一个类里面,叫api_func以后调用就api_func.findwindow.你findwindow写错了,w要大写.
findwindow   返回值你也写错了

2)没有做过

3)这个你不用担心.模态的一样能受到,而且肯定能受到.
以前看过一片文章:是这样描述的,模态对话框有自己的消息处理线程,而非模态是用以前的.一开始不明白,现在想想可能会领悟,不过这个你不用管.你sendmessage与postmessage的主要区别是,你强制发过去,它要优先回应,消息是不入栈的.
发表于:2007-01-05 10:42:567楼 得分:0
我模拟了下,你可以参照:
class   api_func
        {
                [dllimport( "user32.dll ")]
                public   static   extern   intptr   findwindow(string   lpclassname,   string   lpwindowname);

                [dllimport( "user32.dll ")]
                public   static   extern   intptr   findwindowex(intptr   hwndparent,   intptr   hwndchildafter,
                                                                                                string   lpszclass,   string   lpszwindow);

                [dllimport( "user32.dll ")]
                public   static   extern   bool   postmessage(intptr   hwnd,   uint   msg,   int   wparam,   int   lparam);
        }
  private   void   button2_click(object   sender,   eventargs   e)
                {
                        intptr   hwnd   =   api_func.findwindow(null,   "form1 ");
                        if   (hwnd   !=   null)
                        {
                                intptr   hbutton   =   api_func.findwindowex(hwnd,   (intptr)null,   null,   "yes ");
                                if   (hbutton   ==   null)
                                        return;

                                api_func.postmessage(hbutton,   wm_lbuttondown,   mk_lbutton,   0x000b0024);
                                api_func.postmessage(hbutton,   wm_lbuttonup,   mk_lbutton,   0x000b0024);
                        }
                }
发表于:2007-01-05 10:43:128楼 得分:0
private   const   uint   wm_lbuttondown   =   0x0201;
                private   const   uint   wm_lbuttonup   =   0x0202;
                private   const   int   mk_lbutton   =   0x0001;


快速检索

最新资讯
热门点击