#include <windows.h>
#include <cstdio> // sprintf
#define id_Btn_shift 0x00
#define id_Btn_alpha 0x01
#define id_Btn_menu 0x02
#define id_Btn_on 0x03
#define id_Btn_up 0x04
#define id_Btn_down 0x05
#define id_Btn_left 0x06
#define id_Btn_right 0x07
#define id_Btn_optn 0x08
#define id_Btn_calc 0x09
#define id_Btn_integ 0x0a
#define id_Btn_var_x 0x0b
#define id_Btn_frac 0x0c
#define id_Btn_sqrt 0x0d
#define id_Btn_sqr 0x0e
#define id_Btn_pow 0x0f
#define id_Btn_log 0x10
#define id_Btn_ln 0x11
#define id_Btn_minus 0x12
#define id_Btn_dms 0x13
#define id_Btn_recip 0x14
#define id_Btn_sin 0x15
#define id_Btn_cos 0x16
#define id_Btn_tan 0x17
#define id_Btn_sto 0x18
#define id_Btn_eng 0x19
#define id_Btn_lbrack 0x1a
#define id_Btn_rbrack 0x1b
#define id_Btn_s_d 0x1c
#define id_Btn_mplus 0x1d
#define id_Btn_num_0 0x1e
#define id_Btn_num_1 0x1f
#define id_Btn_num_2 0x20
#define id_Btn_num_3 0x21
#define id_Btn_num_4 0x22
#define id_Btn_num_5 0x23
#define id_Btn_num_6 0x24
#define id_Btn_num_7 0x25
#define id_Btn_num_8 0x26
#define id_Btn_num_9 0x27
#define id_Btn_dot 0x28
#define id_Btn_10pow_x 0x29
#define id_Btn_op_plus 0x2a
#define id_Btn_op_minus 0x2b
#define id_Btn_op_multi 0x2c
#define id_Btn_op_div 0x2d
#define id_Btn_op_equal 0x2e
#define id_Btn_del 0x2f
#define id_Btn_ac 0x30
#define id_Btn_ans 0x31
void test_draw_line(HDC hdc, int x1, int y1, int x2, int y2, COLORREF color) ;
void test_draw_rect(HDC hdc,int x1,int y1,int x2,int y2,COLORREF color);
WINBOOL clicked_in_region(int x1,int y1,int x2,int y2,int Pos_x,int Pos_y);
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void show_click_pt(int Pos_x,int Pos_y);
int Btn_clicked_id(int Pos_x,int Pos_y);
int Btn_clicked_id(int xp,int yp,HWND hwnd)
{
if(clicked_in_region(54,249,78,273,xp,yp))
{
MessageBoxA(hwnd,TEXT("shift"),TEXT("clicked"),MB_OK);
return id_Btn_shift;
}
else if(clicked_in_region(92,250,116,272,xp,yp))
{
MessageBoxA(hwnd,TEXT("alpha"),TEXT("clicked"),MB_OK);
return id_Btn_alpha;
}
else if(clicked_in_region(237,252,260,271,xp,yp))
{
MessageBoxA(hwnd,TEXT("menu"),TEXT("clicked"),MB_OK);
return id_Btn_menu;
}
else if(clicked_in_region(277,249,301,272,xp,yp))
{
MessageBoxA(hwnd,TEXT("on"),TEXT("clicked"),MB_OK);
return id_Btn_on;
}
else if(clicked_in_region(162,247,190,269,xp,yp))
{
MessageBoxA(hwnd,TEXT("up"),TEXT("clicked"),MB_OK);
return id_Btn_up;
}
else if(clicked_in_region(164,289,192,306,xp,yp))
{
MessageBoxA(hwnd,TEXT("down"),TEXT("clicked"),MB_OK);
return id_Btn_down;
}
else if(clicked_in_region(133,264,153,291,xp,yp))
{
MessageBoxA(hwnd,TEXT("left"),TEXT("clicked"),MB_OK);
return id_Btn_left;
}
else if(clicked_in_region(202,265,222,293,xp,yp))
{
MessageBoxA(hwnd,TEXT("right"),TEXT("clicked"),MB_OK);
return id_Btn_right;
}
else if(clicked_in_region(49,301,84,320,xp,yp))
{
MessageBoxA(hwnd,TEXT("optn"),TEXT("clicked"),MB_OK);
return id_Btn_optn;
}
else if(clicked_in_region(92,301,128,321,xp,yp))
{
MessageBoxA(hwnd,TEXT("calc"),TEXT("clicked"),MB_OK);
return id_Btn_calc;
}
else if(clicked_in_region(227,301,262,319,xp,yp))
{
MessageBoxA(hwnd,TEXT("integ"),TEXT("clicked"),MB_OK);
return id_Btn_integ;
}
else if(clicked_in_region(271,303,305,320,xp,yp))
{
MessageBoxA(hwnd,TEXT("var_x"),TEXT("clicked"),MB_OK);
return id_Btn_var_x;
}
else if(clicked_in_region(49,337,84,358,xp,yp))
{
MessageBoxA(hwnd,TEXT("frac"),TEXT("clicked"),MB_OK);
return id_Btn_frac;
}
else if(clicked_in_region(94,338,172,355,xp,yp))
{
MessageBoxA(hwnd,TEXT("sqrt"),TEXT("clicked"),MB_OK);
return id_Btn_sqrt;
}
else if(clicked_in_region(136,338,172,355,xp,yp))
{
MessageBoxA(hwnd,TEXT("sqr"),TEXT("clicked"),MB_OK);
return id_Btn_sqr;
}
else if(clicked_in_region(183,337,218,357,xp,yp))
{
MessageBoxA(hwnd,TEXT("pow"),TEXT("clicked"),MB_OK);
return id_Btn_pow;
}
else if(clicked_in_region(227,339,261,356,xp,yp))
{
MessageBoxA(hwnd,TEXT("log"),TEXT("clicked"),MB_OK);
return id_Btn_log;
}
else if(clicked_in_region(270,337,305,356,xp,yp))
{
MessageBoxA(hwnd,TEXT("ln"),TEXT("clicked"),MB_OK);
return id_Btn_ln;
}
else if(clicked_in_region(49,375,84,391,xp,yp))
{
MessageBoxA(hwnd,TEXT("minus"),TEXT("clicked"),MB_OK);
return id_Btn_minus;
}
else if(clicked_in_region(94,375,129,391,xp,yp))
{
MessageBoxA(hwnd,TEXT("dms"),TEXT("clicked"),MB_OK);
return id_Btn_dms;
}
else if(clicked_in_region(138,375,173,391,xp,yp))
{
MessageBoxA(hwnd,TEXT("recip"),TEXT("clicked"),MB_OK);
return id_Btn_recip;
}
else if(clicked_in_region(182,375,216,391,xp,yp))
{
MessageBoxA(hwnd,TEXT("sin"),TEXT("clicked"),MB_OK);
return id_Btn_sin;
}
else if(clicked_in_region(228,375,261,391,xp,yp))
{
MessageBoxA(hwnd,TEXT("cos"),TEXT("clicked"),MB_OK);
return id_Btn_cos;
}
else if(clicked_in_region(271,375,306,391,xp,yp))
{
MessageBoxA(hwnd,TEXT("tan"),TEXT("clicked"),MB_OK);
return id_Btn_tan;
}
else if(clicked_in_region(48,410,83,429,xp,yp))
{
MessageBoxA(hwnd,TEXT("sto"),TEXT("clicked"),MB_OK);
return id_Btn_sto;
}
else if(clicked_in_region(94,410,127,429,xp,yp))
{
MessageBoxA(hwnd,TEXT("eng"),TEXT("clicked"),MB_OK);
return id_Btn_eng;
}
else if(clicked_in_region(137,409,172,429,xp,yp))
{
MessageBoxA(hwnd,TEXT("lbrack"),TEXT("clicked"),MB_OK);
return id_Btn_lbrack;
}
else if(clicked_in_region(182,409,218,429,xp,yp))
{
MessageBoxA(hwnd,TEXT("rbrack"),TEXT("clicked"),MB_OK);
return id_Btn_rbrack;
}
else if(clicked_in_region(227,409,261,429,xp,yp))
{
MessageBoxA(hwnd,TEXT("s_d"),TEXT("clicked"),MB_OK);
return id_Btn_s_d;
}
else if(clicked_in_region(271,409,306,429,xp,yp))
{
MessageBoxA(hwnd,TEXT("mplus"),TEXT("clicked"),MB_OK);
return id_Btn_mplus;
}
else if(clicked_in_region(48,580,92,611,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_0"),TEXT("clicked"),MB_OK);
return id_Btn_num_0;
}
else if(clicked_in_region(48,535,91,567,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_1"),TEXT("clicked"),MB_OK);
return id_Btn_num_1;
}
else if(clicked_in_region(102,536,145,566,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_2"),TEXT("clicked"),MB_OK);
return id_Btn_num_2;
}
else if(clicked_in_region(157,537,200,566,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_3"),TEXT("clicked"),MB_OK);
return id_Btn_num_3;
}
else if(clicked_in_region(48,492,91,524,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_4"),TEXT("clicked"),MB_OK);
return id_Btn_num_4;
}
else if(clicked_in_region(101,492,146,522,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_5"),TEXT("clicked"),MB_OK);
return id_Btn_num_5;
}
else if(clicked_in_region(155,493,198,521,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_6"),TEXT("clicked"),MB_OK);
return id_Btn_num_6;
}
else if(clicked_in_region(47,447,91,476,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_7"),TEXT("clicked"),MB_OK);
return id_Btn_num_7;
}
else if(clicked_in_region(101,448,145,476,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_8"),TEXT("clicked"),MB_OK);
return id_Btn_num_8;
}
else if(clicked_in_region(156,447,198,477,xp,yp))
{
MessageBoxA(hwnd,TEXT("num_9"),TEXT("clicked"),MB_OK);
return id_Btn_num_9;
}
else if(clicked_in_region(101,580,146,611,xp,yp))
{
MessageBoxA(hwnd,TEXT("dot"),TEXT("clicked"),MB_OK);
return id_Btn_dot;
}
else if(clicked_in_region(157,580,199,611,xp,yp))
{
MessageBoxA(hwnd,TEXT("10pow_x"),TEXT("clicked"),MB_OK);
return id_Btn_10pow_x;
}
else if(clicked_in_region(210,537,253,567,xp,yp))
{
MessageBoxA(hwnd,TEXT("op_plus"),TEXT("clicked"),MB_OK);
return id_Btn_op_plus;
}
else if(clicked_in_region(263,537,307,567,xp,yp))
{
MessageBoxA(hwnd,TEXT("op_minus"),TEXT("clicked"),MB_OK);
return id_Btn_op_minus;
}
else if(clicked_in_region(210,492,253,521,xp,yp))
{
MessageBoxA(hwnd,TEXT("op_multi"),TEXT("clicked"),MB_OK);
return id_Btn_op_multi;
}
else if(clicked_in_region(264,493,307,521,xp,yp))
{
MessageBoxA(hwnd,TEXT("op_div"),TEXT("clicked"),MB_OK);
return id_Btn_op_div;
}
else if(clicked_in_region(264,493,307,612,xp,yp))
{
MessageBoxA(hwnd,TEXT("op_equal"),TEXT("clicked"),MB_OK);
return id_Btn_op_equal;
}
else if(clicked_in_region(210,447,254,477,xp,yp))
{
MessageBoxA(hwnd,TEXT("del"),TEXT("clicked"),MB_OK);
return id_Btn_del;
}
else if(clicked_in_region(263,447,306,475,xp,yp))
{
MessageBoxA(hwnd,TEXT("ac"),TEXT("clicked"),MB_OK);
return id_Btn_ac;
}
else if(clicked_in_region(209,581,252,610,xp,yp))
{
MessageBoxA(hwnd,TEXT("ans"),TEXT("clicked"),MB_OK);
return id_Btn_ans;
}
return 0xff;
}
void test_draw_line(HDC hdc, int x1, int y1, int x2, int y2, COLORREF color)
{
HPEN hPen = CreatePen(PS_SOLID, 1, color); // 创建画笔对象
HPEN hOldPen = static_cast<HPEN>(SelectObject(hdc, hPen)); // 选择画笔对象
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x2, y2);
SelectObject(hdc, hOldPen); // 恢复原来的画笔对象
DeleteObject(hPen); // 删除创建的画笔对象
}
void test_draw_rect(HDC hdc,int x1,int y1,int x2,int y2,COLORREF color)
{
test_draw_line(hdc,x1,y1,x2,y1,color);
test_draw_line(hdc,x1,y2,x2,y2,color);
test_draw_line(hdc,x1,y1,x1,y2,color);
test_draw_line(hdc,x2,y1,x2,y2,color);
}
void show_click_pt(int Pos_x,int Pos_y)
{
char str[114]={'\0'};
sprintf(str,"click_pos:( %d , %d )",Pos_x,Pos_y);
MessageBoxA(NULL,str,TEXT("notice"),MB_OK);
}
WINBOOL clicked_in_region(int x1,int y1,int x2,int y2,int Pos_x,int Pos_y)//after message WM_LBUTTONDOWN
{
return PtInRegion(CreateRectRgn(x1,y1,x2,y2),Pos_x,Pos_y);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSA wc = {0}; // 使用 WNDCLASSA 结构体
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "MyWindowClass"; // 使用多字节字符串
RegisterClassA(&wc); // 使用 RegisterClassA 函数
HWND hwnd = CreateWindowA("MyWindowClass", "Virtual CASIO fx-991CN X", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 380, 700, NULL, NULL, hInstance, NULL);
HWND Btn_num_0=CreateWindow(
"BUTTON"," 0 ",
WS_CHILD|WS_VISIBLE,
100,100,100,30,
hwnd,
(HMENU)1,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
if(lParam and HIWORD(wParam)==BN_CLICKED)
{
if(HWND(lParam)==GetDlgItem(hwnd,1))
{
MessageBox(hwnd,"button_num_0 pressed","notice",MB_OK);
}
}
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 贴图
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "casio_image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (hBitmap)
{
BITMAP bitmap;
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
int bmpWidth = bitmap.bmWidth;
int bmpHeight = bitmap.bmHeight;
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap);
BitBlt(hdc, 0, 0, bmpWidth, bmpHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hOldBitmap);
DeleteDC(hdcMem);
DeleteObject(hBitmap);
}
//test_draw_line/rect
test_draw_rect(hdc,40,50,90,100,RGB(255,0,0));
test_draw_line(hdc,11,45,14,19,RGB(255,0,0));
test_draw_line(hdc,234,121,485,321,RGB(0,0,255));
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_LBUTTONDOWN:
{
int xp=LOWORD(lParam),yp=HIWORD(lParam);
//show_click_pt(xp,yp);
Btn_clicked_id(xp,yp,hwnd);
return 0;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}