绘制图片到窗口
ALT如何制作自定义控件?不知道怎么绘制图片到自己new出来的窗口上....只会这种,感觉没啥用,连消息循环机制都没有。。。
#include<graphics.h> //图形界面
class button
{
public:
int x;
int y;
int width;
int height;
COLORREF color;
char* pStr;
static button* pButton;
};
//创建按钮
button* createButton(int x/*X位置*/, int y/*Y位置*/, int width/*宽*/, int height/*长*/, COLORREF color/*背景色*/, const char* text/*按钮标题*/)
{
button* pButton = new button;
pButton->x = x;
pButton->y = y;
pButton->width = width;
pButton->height = height;
pButton->color = color;
pButton->pStr = (char*)malloc(strlen(text) + 1);
strcpy(pButton->pStr, text);
return pButton;
}
//释放按钮
void releasebutton(button* pb)
{
button* Temp = pb;
pb = NULL;
delete Temp;
}
//绘制按钮
void drawButton(struct button* pb)
{
settextstyle(30, 0, "宋体");//设置字体
setfillcolor(pb->color); //设置颜色
setlinecolor(BLACK); //设置矩形边框颜色为黑色
settextcolor(BLACK); //设置字体颜色为黑色
setbkmode(TRANSPARENT); //设置背景为透明
fillrectangle(pb->x, pb->y, pb->x + pb->width, pb->y + pb->height); //绘制矩形
outtextxy(pb->x + 25, pb->y + 5, pb->pStr); //显示字体 位置
}
//判断鼠标是否在按钮内
bool mouseInButton(struct button* pb/*按钮*/, MOUSEMSG m/*鼠标*/)
{
if ((pb->x <= m.x) && (m.x <= pb->x + pb->width))
{
if ((pb->y <= m.y) && (m.y <= pb->y + pb->height))
{
pb->color = RED;//设置背景为红色
return true;
}
}
pb->color = YELLOW; //设置字体为黄色
return false;
}
//是否点击按钮
void clickButton(button* pb, MOUSEMSG mouse)
{
if (mouseInButton(pb, mouse) && mouse.uMsg == WM_LBUTTONDOWN)
{
MessageBox(NULL, _T("左键按下"), _T("触发"), MB_SYSTEMMODAL);
}
else if (mouseInButton(pb, mouse) && mouse.uMsg == WM_RBUTTONDOWN)
{
MessageBox(NULL, _T("右键按下"), _T("触发"), MB_SYSTEMMODAL);
}
}
int main()
{
HWND win = initgraph(800, 600); //创建窗口
IMAGE img;
loadimage(&img, "E:\\img\\psc (1).png", 700, 600); //加载图片
button* pb = createButton(300, 200, 200, 40, YELLOW, "好好学习");
button* pb2 = createButton(300, 250, 200, 40, YELLOW, "天天向上");
button* pb3 = createButton(300, 300, 200, 40, YELLOW, "羊了个羊");
button* pb4 = createButton(300, 350, 200, 40, YELLOW, "喵了个咪");
while (true)
{
BeginBatchDraw();
putimage(50, 10, &img); //显示图片
//显示按钮
drawButton(pb);
drawButton(pb2);
drawButton(pb3);
drawButton(pb4);
MOUSEMSG m = GetMouseMsg();
if (mouseInButton(pb, m))
{
clickButton(pb, m);
}
else if (mouseInButton(pb2, m))
{
clickButton(pb2, m);
}
else if (mouseInButton(pb3, m))
{
clickButton(pb3, m);
}
else if (mouseInButton(pb4, m))
{
clickButton(pb4, m);
}
EndBatchDraw();
}
releasebutton(pb);
releasebutton(pb2);
releasebutton(pb3);
releasebutton(pb4);
closegraph();
}
void clickButton(button* pb, MOUSEMSG mouse)
{
if (mouseInButton(pb, mouse) && mouse.uMsg == WM_LBUTTONDOWN)
{
MessageBox(NULL, _T("左键按下"), _T("触发"), MB_SYSTEMMODAL);/////////////////////这里调用想调用的函数不就行了吗?要什么消息过二遍手?
}
else if (mouseInButton(pb, mouse) && mouse.uMsg == WM_RBUTTONDOWN)
{
MessageBox(NULL, _T("右键按下"), _T("触发"), MB_SYSTEMMODAL);
}
} jhq999 发表于 2022-9-25 07:44
这种简单的,只是随便写写,没想太多,我想在自己new出来的窗口上进行绘制
页:
[1]