c语言如何检测点击的按钮? - 爱问答

(爱问答)

c语言如何检测点击的按钮?

main.c:
#include <windows.h>
#include "resource.h"
 
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
 
/* Current app instance */
HINSTANCE hInst;
/*  Make the class name into a global variable  */
TCHAR szClassName[] = TEXT("WindowsApp");
 
int WINAPI
WinMain (HINSTANCE hThisInstance,
         HINSTANCE hPrevInstance,
         LPSTR lpszArgument,
         int nFunsterStil)
 
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
 
/* Save this instance */
hInst = hThisInstance;
 
    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);
 
    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE (IDC_12345);
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 
    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;
 
    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           TEXT("12345"), /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           0,       /* where the window ends up on the screen */
           CW_USEDEFAULT,       /* The programs width */
           0,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
 
    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);
 
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }
 
    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}
 
/*  This function is called by the Windows function DispatchMessage()  */
 
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rt;
TCHAR szHello[] = TEXT("今天你打卡了吗?");
 
    switch (message)                  /* handle the messages */
    {
case WM_COMMAND:
    switch (LOword(wParam))
    {
    case IDM_ABOUT:
MessageBox (hwnd, TEXT ("贾智渊制作 "),
                        TEXT ("About"), MB_OK | MB_ICONINFORMATION);
break;
case IDM_EXIT:
DestroyWindow(hwnd);
break;
case IDM_QU:
MessageBox (hwnd, TEXT ("今天你打卡了吗? "),
                        TEXT ("问"), MB_OK | MB_ICONINFORMATION | MB_OKCANCEL | MB_ICONINFORMATION);
      MessageBox (hwnd, TEXT ("去打卡! "),
      TEXT ("温馨提示"), MB_OK | MB_ICONINFORMATION | MB_OKCANCEL | MB_ICONINFORMATION);
default:
return DefWindowProc(hwnd, message, wParam, lParam);   
    }
    break;
    case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
/* TODO: Add any drawing code here... */
 
GetClientRect(hwnd, &rt);
DrawText(hdc, szHello, lstrlen(szHello), &rt, DT_CENTER);
EndPaint(hwnd, &ps);
break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
 
    return 0;
}
12345.rc:
#include "resource.h"
#include <windows.h>
 
/////////////////////////////////////////////////////////////////////////////
//
// Menu
//
 
IDC_12345 MENU 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit",                IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",           IDM_ABOUT
    END
    POPUP "&Q"
    BEGIN
        MENUITEM "&QU",                 IDM_QU
    END
END
resource.h:
#define IDM_EXIT10001
#define IDM_ABOUT10002
#defineIDM_QU 10003
#define IDC_1234510101
#define IDD_ABOUTBOX10102
怎样修改
case IDM_QU:
MessageBox (hwnd, TEXT ("今天你打卡了吗? "),
                        TEXT ("问"), MB_OK | MB_ICONINFORMATION | MB_OKCANCEL | MB_ICONINFORMATION);
      MessageBox (hwnd, TEXT ("去打卡! "),
      TEXT ("温馨提示"), MB_OK | MB_ICONINFORMATION | MB_OKCANCEL | MB_ICONINFORMATION);
部分来确认点的是哪个按钮(确认/取消)

你是用MessageBox上的按钮,所以可直接用它返回值来判断 

你的程序可以这样改

case IDM_QU:
if (MessageBox(hwnd, TEXT("今天你打卡了吗? "), TEXT("问"),
MB_OK | MB_ICONINFORMATION | MB_OKCANCEL | MB_ICONINFORMATION)==IDCANCEL) //按下取消键,未打卡
{
   if (MessageBox(hwnd, TEXT("去打卡! "), TEXT("温馨提示"),
   MB_OK | MB_ICONINFORMATION | MB_OKCANCEL | MB_ICONINFORMATION)==IDOK)
{
//按下了确定
}
 else
 {
              //按下了取消
 }
}
else
{
///确定已打卡
}
default:

另外,c语言win32的开发模式目前只适合于学习了,实际开发肯定要用C++和构件了(不然开发效率太低了)


相关标签:c语言

下一篇:请教大家1个问题

上一篇:按键精灵计数语句

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?