LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 4975|回复: 30

MiniGUI什么语句可获取主窗口句柄,等同于VC中AfxGetApp()->GetMainWnd();

[复制链接]
发表于 2007-4-19 23:58:42 | 显示全部楼层 |阅读模式
MiniGUI什么语句可获取主窗口句柄,等同于VC中AfxGetApp()->GetMainWnd();   
现在想要在一个控件中获取主窗口句柄,请大家指点一下???
发表于 2007-4-20 07:44:28 | 显示全部楼层
要是没记错,好像是 GetParent
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-20 23:32:54 | 显示全部楼层
谢谢~~~~~~~~~~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-21 00:17:32 | 显示全部楼层
谢谢~~~~~~~~~~~~~

我试了一下,还是不能完成功能呀

我在控件窗口中设置了一个定时器

然后定时到达后往主窗口的 MSG_PAINT: 发一个消息

在控件过程函数中,switch语句中我是这样写的

case MSG_TIMER:
   PostMessage(GetParent(),MSG_PAINT,0L,0L)

由于学习的时间不长,不知道问题在那里,希望大家指点一下。
回复 支持 反对

使用道具 举报

发表于 2007-4-21 23:12:21 | 显示全部楼层
GetParent 不需要参数么?记不清了。另外,楼主要实现什么功能?这样突然冒出来一句实在让我摸不到头脑

btw,MSG_PAINT 配上两个为 0 的参数,这是否会在消息处理中被忽略掉我没有作测试,不过也许有这个可能
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-22 00:36:43 | 显示全部楼层
谢谢你的回复

我想做的 就是点击“开始扫描”,然后每隔 1S 图片就刷新一次,显示下一幅图片,点击“停止扫描”就停止显示。

我在控件窗口定义了一个定时器,定时向主窗口发一个消息

这样吧  我把代码发上来  这样也许会清楚一些

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-22 00:44:36 | 显示全部楼层

  1. #include <stdio.h>

  2. #include <minigui/common.h>     
  3. #include <minigui/minigui.h>
  4. #include <minigui/gdi.h>
  5. #include <minigui/window.h>
  6. #include <minigui/control.h>

  7. #define IDTIMER          0
  8. #define IDC_DISP1        1000
  9. #define ID_OK            1001
  10. #define ID_CANCEL        1002

  11. static DLGTEMPLATE  MyDlg =
  12. {   WS_VISIBLE | WS_BORDER,
  13.     WS_EX_NONE,
  14.     512, 0, 128 ,480,
  15.     "",
  16.     0, 0,
  17.     3,
  18.     NULL,
  19.     0
  20. };

  21. static CTRLDATA CtrlInitData [] =
  22. {   
  23.   
  24.   {  "button",
  25.     WS_VISIBLE | WS_TABSTOP | BS_DEFPUSHBUTTON,
  26.     20, 50, 80,25,
  27.     ID_OK,
  28.     "开始扫描",
  29.     0,
  30.     WS_EX_NONE,
  31.   },

  32.   {  "button",
  33.     WS_VISIBLE | WS_TABSTOP,
  34.     20, 100, 80 ,25,
  35.     ID_CANCEL,
  36.     "停止扫描",
  37.     0,
  38.     WS_EX_NONE,
  39.   },

  40. {  "static",
  41.     WS_VISIBLE | SS_SIMPLE,
  42.     10, 300, 110 ,16,
  43.     IDC_DISP1,
  44.     "欢迎使用MiniGUI" ,
  45.     0,
  46.     WS_EX_NONE
  47.   }

  48. };

  49. static int MyDlgProc(HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
  50. {   
  51.     HWND hWnd;
  52.     switch (message)
  53.        {
  54.         case MSG_INITDIALOG:
  55.              return 1;

  56.         case MSG_COMMAND:
  57.              switch (wParam)
  58.                {
  59.                     

  60.                    case ID_OK:
  61.                          SetTimer(hDlg, IDTIMER, 100);
  62.                          break;
  63.         

  64.      
  65.                    case  ID_CANCEL:
  66.                          KillTimer(hDlg, IDTIMER);

  67.                          break;
  68.                    case MSG_TIMER:
  69.                //       PostMessage(GetParent, MSG_TIMER,0L, (LPARAM)IDTIMER);
  70.                //       PostMessage(GetParent(), MSG_ERASEBKGND,0L, (LPARAM)IDTIMER);
  71.                //       PostMessage(GetParent(HWND hWnd), MSG_ERASEBKGND,0L, 0L);
  72.               //        PostMessage(GetMainWindowHandle(HWND hWnd), MSG_ERASEBKGND,0L, 0L);
  73.                
  74.                         PostMessage(GetMainWindowHandle(GetParent()), MSG_ERASEBKGND,0L, 0L);

  75. /*  各种表达方式都试过了,还是不成功 */

  76.                    default:
  77.                          break;
  78.                }
  79.         break;

  80.       
  81.         default:
  82.             return DefaultDialogProc(hDlg, message, wParam, lParam);
  83.     }
  84.     return 0;
  85. }

  86. static void InitDialogBox(HWND hWnd)
  87. {   MyDlg.controls = CtrlInitData;
  88.     DialogBoxIndirectParam(&MyDlg, hWnd, MyDlgProc, 0L);
  89. }

  90. static int WinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
  91. {
  92.     HDC hdc;
  93.     static BITMAP s_bmp;
  94.     static char bmpno[] = "00.jpg";

  95.     RECT rc = {0,0,512,480};

  96.     switch (message) {

  97.         case MSG_CREATE:
  98.             LoadBitmapFromFile(HDC_SCREEN, &s_bmp, bmpno);
  99.             hdc = BeginPaint (hWnd);
  100.             FillBoxWithBitmap (hdc, 0, 0, 512, 480, &s_bmp);
  101.             EndPaint(hWnd, hdc);            
  102.             InitDialogBox(hWnd);
  103.             return 0;
  104.   
  105.        case MSG_ERASEBKGND:
  106.             if(bmpno[1] == '9')

  107.             {  bmpno[0] = bmpno[0]+1;
  108.                bmpno[1] = '0';
  109.              }
  110.             else
  111.             {bmpno[1] = bmpno[1]+1;}
  112.   
  113.             LoadBitmapFromFile(HDC_SCREEN, &s_bmp, bmpno);
  114.             hdc = BeginPaint (hWnd);
  115.             FillBoxWithBitmap (hdc, 0, 0, 512, 480, &s_bmp);
  116.             EndPaint(hWnd, hdc);
  117.             
  118.             if(bmpno[0] == '3' && bmpno[1] == '1')
  119.               {
  120.             bmpno[0] = '0';
  121.             bmpno[1] = '0';
  122.             }
  123.             
  124.             return 0;
  125.       
  126.         default:

  127.     return DefaultMainWinProc(hWnd, message, wParam, lParam);
  128. }
  129.   return 0;
  130. }

  131. int MiniGUIMain (int argc, const char* argv[])
  132. {
  133.     MSG Msg;
  134.     HWND hMainWnd;
  135.     MAINWINCREATE CreateInfo;

  136. #ifdef _LITE_VERSION
  137.     SetDesktopRect(0, 0, 800, 600);
  138. #endif

  139.     CreateInfo.dwStyle = WS_VISIBLE | WS_BORDER;
  140.     CreateInfo.dwExStyle = WS_EX_NONE;
  141.     CreateInfo.spCaption = "";
  142.     CreateInfo.hMenu = 0;
  143.     CreateInfo.hCursor = GetSystemCursor(0);
  144.     CreateInfo.hIcon = 0;
  145.     CreateInfo.MainWindowProc = WinProc;
  146.     CreateInfo.lx = 0;
  147.     CreateInfo.ty = 0;
  148.     CreateInfo.rx = 640;
  149.     CreateInfo.by = 480;
  150.     CreateInfo.iBkColor = PIXEL_lightwhite;
  151.     CreateInfo.dwAddData = 0;
  152.     CreateInfo.hHosting = HWND_DESKTOP;
  153.    
  154.     hMainWnd = CreateMainWindow (&CreateInfo);
  155.    
  156.     if (hMainWnd == HWND_INVALID)
  157.         return -1;
  158.     ShowWindow(hMainWnd, SW_SHOWNORMAL);

  159.     while (GetMessage(&Msg, hMainWnd)) {
  160.         TranslateMessage(&Msg);
  161.         
  162.         DispatchMessage(&Msg);
  163.     }

  164.     MainWindowThreadCleanup (hMainWnd);

  165.     return 0;
  166. }

  167. #ifndef _LITE_VERSION
  168. #include <minigui/dti.c>
  169. #endif

复制代码


这个代码进行图片的循环显示什么的都没有问题

问题只有一个

就是不能把消息发到主窗口

我想问题出在标注汉字的地方吧
回复 支持 反对

使用道具 举报

发表于 2007-4-22 11:16:42 | 显示全部楼层
楼主的这个可以编译通过吗?我说过,GetParent 是需要参数的

btw,莫非最近几个Minigui相关的帖子都是楼主提出来的?其实我认为楼主的想法有些别扭。

直接重绘主窗口我想并没有什么不好的地方。而且,在 SetTimer 的时候,直接把 Timer 给主窗口,这不是更方便吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-23 00:44:18 | 显示全部楼层
我思路改变了一下

就象你说的那样,把 TIMER 放在主函数过程函数中

自己定义了一个 ID_RECEIVE 来接受控件窗口的消息

但是还是不能成功

代码能编译成功,也能运行,但就是接收不到传过来的消息

标注的各种情况都试了




  1. #define IDTIMER          0
  2. #define IDC_DISP1        1000
  3. #define ID_OK            1001
  4. #define ID_CANCEL        1002
  5. #define ID_RECEIVE       1003

  6. static int MyDlgProc(HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
  7. {   
  8.   switch (message)
  9.    {
  10.       case MSG_INITDIALOG:         

  11.                return 1;

  12.       case MSG_COMMAND:
  13.              switch (wParam)
  14.               {
  15.                     

  16.               case ID_OK:
  17.           //            PostMessage(GetMainWindowHandle(GetParent(hWnd)), ID_RECEIVE,0L, 0L);
  18.           //            PostMessage(GetParent(hDlg), ID_RECEIVE,0L, (LPARAM)ID_OK);
  19.           //            PostMessage(GetMainWindowHandle(hWnd), ID_RECEIVE,0L, 0L);
  20.           //            PostMessage(GetMainWindowHandle(hDlg), ID_RECEIVE,0L, (LPARAM)ID_OK);
  21.                      
  22.                          PostMessage(GetMainWindowHandle(hWnd), ID_RECEIVE,0L, (LPARAM)ID_OK);
  23.                         
  24.                          break;
  25.         

  26.      
  27.                 case  ID_CANCEL:
  28.                          break;

  29.                 default:
  30.                          break;
  31.                }
  32.         break;

  33.       
  34.         default:
  35.             return DefaultDialogProc(hDlg, message, wParam, lParam);
  36.     }
  37.     return 0;
  38. }


  39. static int WinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
  40. {
  41.     HDC hdc;
  42.     static BITMAP s_bmp;
  43.     static char bmpno[] = "00.jpg";

  44.     RECT rc = {0,0,512,480};

  45.     switch (message) {

  46.         case MSG_CREATE:
  47.             LoadBitmapFromFile(HDC_SCREEN, &s_bmp, bmpno);
  48.             hdc = BeginPaint (hWnd);
  49.             FillBoxWithBitmap (hdc, 0, 0, 512, 480, &s_bmp);
  50.             EndPaint(hWnd, hdc);
  51.             printf("this is a test1\n");
  52.             InitDialogBox(hWnd);
  53.             return 0;
  54.        case ID_RECEIVE:
  55.                SetTimer(hWnd, IDTIMER, 20);
  56.                printf("this is a test7\n");

  57.                return 0;
  58.             
  59.        case MSG_TIMER:
  60.                 printf("this is a test4\n");
  61.               if(LOWORD(wParam) == IDTIMER)
  62.               InvalidateRect (hWnd, &rc, TRUE);
  63.   
  64.       case MSG_ERASEBKGND:
  65.             if(bmpno[1] == '9')

  66.             {  bmpno[0] = bmpno[0]+1;
  67.                bmpno[1] = '0';
  68.              }
  69.             else
  70.             {bmpno[1] = bmpno[1]+1;}
  71.               LoadBitmapFromFile(HDC_SCREEN, &s_bmp, bmpno);
  72.             hdc = BeginPaint (hWnd);
  73.             FillBoxWithBitmap (hdc, 0, 0, 512, 480, &s_bmp);
  74.             EndPaint(hWnd, hdc);
  75.             
  76.             if(bmpno[0] == '3' && bmpno[1] == '1')
  77.               {
  78.             bmpno[0] = '0';
  79.             bmpno[1] = '0';
  80.             }
  81.             

  82.             return 0;

  83.          
  84.         default:

  85.     return DefaultMainWinProc(hWnd, message, wParam, lParam);
  86. }
  87.   return 0;
  88. }

复制代码


我把用到的API的说明也发上来,麻烦你看看是否是我理解错了,谢谢你



  1. =====================================
  2. [b]
  3. int PostMessage  (  HWND  hWnd,  
  4.   int  iMsg,  
  5.   WPARAM  wParam,  
  6.   LPARAM  lParam
  7. )   
  8. [/b]
  9.    Posts a message into the message queue of a window and returns immediatly.

  10. This functions posts a message into the message queue of the window hWnd and returns immediately.


  11. Parameters:
  12. hWnd  The handle to the window.  
  13. iMsg  The identifier of the message.  
  14. wParam  The first parameter of the message.  
  15. lParam  The second parameter of the message.

  16. Returns:
  17. ERR_OK on success, < 0 on errors.
  18. Return values:
  19. ERR_OK  Post message successfully.  
  20. ERR_QUEUE_FULL  The message queue is full.  
  21. ERR_INV_HWND  Invalid window handle.

  22. ==================================
  23. [b]HWND GUIAPI GetParent  (  HWND  hWnd   ) [/b]  

  24.    Retrieves the handle to a child window's parent window.

  25. This function retrieves the handle to the specified child window's parent window.


  26. Parameters:
  27. hWnd  The handle to the child window.

  28. Returns:
  29. The handle to the parent, HWND_INVALID indicates an error.
  30. Note:
  31. For a main window, this function always returns 0. For HWND_DESKTOP or an invalid window handle, HWND_INVALID will be returned.
  32. See also:
  33. GetMainWindowHandle  
  34. ===================================

  35. [b]HWND GUIAPI GetMainWindowHandle  (  HWND  hWnd   )   [/b]

  36.    Retrives the handle to the main window contains a window.

  37. This function retrives the handle to the main window which contains the specified window hWnd.


  38. Parameters:
  39. hWnd  The handle to the window.  

  40. Returns:
  41. The handle to the main window, HWND_INVALID indicates an error.
  42. Note:
  43. For a main window, this function always returns the handle to itself. For HWND_DESKTOP, HWND_DESKTOP is returned.
  44. See also:
  45. GetParent  
  46. ============================
复制代码
回复 支持 反对

使用道具 举报

发表于 2007-4-23 10:08:27 | 显示全部楼层
在 WinPro 中CREATE的时候直接把 hWnd 保存到全局变量 h_MainWnd。
PostMessage(h_MainWnd, ID_RECEIVE,0L, (LPARAM)ID_OK);
试试。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表