首页 > 代码库 > MiniGUI鼠标捕获演示程序

MiniGUI鼠标捕获演示程序

默认情况下,MiniGUI系统只向光标热点之下的窗口发送鼠标信息,但是,对窗口设置捕获后,即是窗口不在鼠标下,也可接收鼠标消息,只能同时设置1个窗口为捕获窗口。

MiniGUI自带的鼠标捕获演示程序capture.c很难看出捕获的效果,甚至让不熟悉界面编程的人产生误解,所以我改写了示例程序,鼠标右击下面的窗口可切换下面的窗口是否为捕获窗口。


#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>

#define IDC_MYBUTTON    100
#define IDC_MYBUTTON2    110

/* a simple button control */


static int MybuttonWindowProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    static int status = 0;
    char buff[20]="";
    static int flag=0; 

    switch (message) {

    case MSG_RBUTTONDOWN:
        if(flag==0) {        
        SetCapture (hWnd);
        flag=1;
         }
        else {
        ReleaseCapture();
        flag=0;
        }        
    break;

    case MSG_LBUTTONDOWN:
    	 ++status;
    	 InvalidateRect (hWnd, NULL, TRUE);        
    break;

    case MSG_PAINT:
        hdc = BeginPaint (hWnd);
        if(flag==1)
        sprintf(buff,"capture:%d",status);
        else
        sprintf(buff,"normal:%d",status);
        SetBkMode(hdc, BM_TRANSPARENT);
        TextOut(hdc,10, 0, buff);        
        EndPaint(hWnd, hdc);
        return 0;

    case MSG_DESTROY:
        return 0;
    }

    return DefaultControlProc (hWnd, message, wParam, lParam);
}


//注册自定义控件
BOOL RegisterMybutton (void)
{
    WNDCLASS WndClass;

    WndClass.spClassName = "mybutton";
    WndClass.dwStyle     = 0;
    WndClass.dwExStyle   = 0;
    WndClass.hCursor     = GetSystemCursor(0);
    WndClass.iBkColor    = PIXEL_lightgray;
    WndClass.WinProc     = MybuttonWindowProc;

    return RegisterWindowClass (&WndClass);
}

/* main windoww proc */
static int CaptureWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
    case MSG_CREATE:
        RegisterMybutton();//使用前,注册自定义控件

       	 //创建自定义按钮控件
        CreateWindow ("mybutton", "", WS_VISIBLE | WS_CHILD, IDC_MYBUTTON, 
                10, 95, 200, 20, hWnd, 0);

         //创建标准按钮控件
        CreateWindow (CTRL_BUTTON, "", WS_VISIBLE | WS_CHILD, IDC_MYBUTTON2, 
                10, 65, 200, 20, hWnd, 0);
        SetWindowText(GetDlgItem(hWnd,IDC_MYBUTTON2),"normal:0");
        break;
    case MSG_COMMAND:
    {
        int id   = LOWORD(wParam);
        int nc = HIWORD(wParam);
         static int num=0;
         char buf[20]="";
       
        if(id==IDC_MYBUTTON2 && nc==BN_CLICKED) {			//控件的消息处理
           ++num;
           sprintf(buf,"normal:%d",num);
           SetWindowText(GetDlgItem(hWnd,IDC_MYBUTTON2),buf);
         }
    }
    break;

    case MSG_CLOSE:
        DestroyAllControls (hWnd);
        DestroyMainWindow (hWnd);
        PostQuitMessage (hWnd);
        return 0;
    }

    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}

int MiniGUIMain (int args, const char* arg[])
{
    MSG Msg;
    HWND hMainWnd;
    MAINWINCREATE CreateInfo;

#ifdef _MGRM_PROCESSES
    JoinLayer(NAME_DEF_LAYER , "capture" , 0 , 0);
#endif
    
    CreateInfo.dwStyle = WS_VISIBLE | WS_BORDER | WS_CAPTION;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "using mouse capture demo";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = CaptureWinProc;
    CreateInfo.lx = 0;
    CreateInfo.ty = 0;
    CreateInfo.rx = 320;
    CreateInfo.by = 240;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = http://www.mamicode.com/0;>

MiniGUI鼠标捕获演示程序