![](/img/trans.png)
[英]How to color blend (colorize by specified alpha value) the canvas area using pure GDI?
[英]How to change a color in an area of a canvas
我试图在TCanvas上单击并拖动,并在向下拖动时将背景颜色更改为clHighlight,如果要反转拖动方向,则将其更改回clWhite。 如果您单击您正在阅读的文本并向下或向上拖动,无论如何都会发生几乎所有的事情。 我已经从这个链接中收集了
自从我第一次发布此代码以来,我已经对下面的代码进行了广泛的编辑,下面的代码现在可以执行我打算要做的事情,除了如果您不以均匀的速度拖动时所选内容中的文本可能会弄乱,这就是不能接受的。 如果您确实真正快速地上下移动鼠标,则肯定会弄乱它。 如果您以稳定的速度移动鼠标,则效果很好,这使我担心可能无法解决该问题。 如果有人有什么好建议,我会继续研究。 我还遇到了几个“资源不足”错误,不确定是怎么回事,我正在释放位图,没有其他我知道的资源参与其中。
以下代码位于MouseMove事件中。srect是TImage画布中的选定矩形。 drect与转换为0,0的相同srect。 bm1包含使用CopyRect从TImage的画布复制的选定矩形。 bm2包含bm1,并且使用BrushCopy将clWhite(背景)更改为clHighlight(或相反)。 然后使用BitBlt将bm2复制回原始TImage的选定矩形。
// vp is derived from TImage
if (Y > sel_data->_last_y)
{
TRect srect = Rect(sel_data->_rect.Left,sel_data->_last_y,sel_data->_rect.Right, Y);
TRect drect = Rect(sel_data->_rect.Left,0,sel_data->_rect.Right, sel_data->_rect.Height() - 1);
Graphics::TBitmap* bm1 = new Graphics::TBitmap;
bm1->Width = srect.Width();
bm1->Height = srect.Height();
Graphics::TBitmap* bm2 = new Graphics::TBitmap;
bm2->Width = srect.Width();
bm2->Height = srect.Height();
bm1->Canvas->CopyRect(drect, vp->Canvas, srect);
bm2->Canvas->Brush->Color = clHighlight;
bm2->Canvas->BrushCopy(drect, bm1, drect, clWindow);
BitBlt(vp->Canvas->Handle, srect.Left, srect.Top, srect.Width(), srect.Height(),
bm2->Canvas->Handle, 0, 0, SRCCOPY);
vp->Refresh();
delete bm1;
delete bm2;
}
else if (Y < sel_data->_last_y)
{
TRect srect = Rect(sel_data->_rect.Left, Y,sel_data->_rect.Right, sel_data->_last_y);
TRect drect = Rect(sel_data->_rect.Left,0,sel_data->_rect.Right, sel_data->_rect.Height() - 1);
Graphics::TBitmap* bm1 = new Graphics::TBitmap;
bm1->Width = srect.Width();
bm1->Height = srect.Height();
Graphics::TBitmap* bm2 = new Graphics::TBitmap;
bm2->Width = srect.Width();
bm2->Height = srect.Height();
bm1->Canvas->CopyRect(drect, vp->Canvas, srect);
bm2->Canvas->Brush->Color = clWhite;
bm2->Canvas->BrushCopy(drect, bm1, drect, clHighlight);
int w = srect.Width();
int h = srect.Height();
BitBlt(vp->Canvas->Handle, srect.Left, srect.Top, w, h, bm2->Canvas->Handle, 0, 0, SRCCOPY);
vp->Refresh();
delete bm1;
delete bm2;
}
sel_data->_last_y = Y;
}
这就是我最终得到的结果,据我所知,所有故障均已修复,其中包括整个MouseMove事件处理程序。 上面提到的链接使我很困惑,因为我需要一个CopyRect,一个BrushCopy和一个BitBlt。
void __fastcall Tviewer_ui::viewerPageMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
if (_selecting)
{
TColor to_color = 0;
TColor from_color = 0;
Tviewer_page* vp = static_cast<Tviewer_page *>(Sender);
Tsel_data* sel_data = &vp->_sel_datas[vp->_c_sel];
// Scroll window if needed
int sb_pos = _scrollBox->VertScrollBar->Position;
if (Y > sb_pos - vp->_top + _scrollBox->Height - 10)
_scrollBox->VertScrollBar->Position += 10;
else
if (Y < sb_pos - vp->_top + 10)
_scrollBox->VertScrollBar->Position -= 10;
int y1 = 0;
int y2 = 0;
if (Y > sel_data->_start_y)
{
if (Y > sel_data->_last_y)
{
y1 = sel_data->_last_y;
y2 = Y;
from_color = clWhite;
to_color = clHighlight;
}
else if (Y < sel_data->_last_y)
{
y1 = Y;
y2 = sel_data->_last_y;
from_color = clHighlight;
to_color = clWhite;
}
sel_data->_rect.Bottom = Y;
}
else if (Y < sel_data->_start_y)
{
if (Y < sel_data->_last_y)
{
y1 = Y;
y2 = sel_data->_last_y;
from_color = clWhite;
to_color = clHighlight;
}
else if (Y > sel_data->_last_y)
{
y1 = sel_data->_last_y;
y2 = Y;
from_color = clHighlight;
to_color = clWhite;
}
sel_data->_rect.Top = Y;
}
int height = abs(y1 - y2);
if (height > 0)
{
TRect srect = Rect(sel_data->_rect.Left, y1, sel_data->_rect.Right, y2);
int width = srect.Width();
TRect drect = Rect(sel_data->_rect.Left,0,sel_data->_rect.Right, height);
Graphics::TBitmap* bm1 = new Graphics::TBitmap;
bm1->Width = width;
bm1->Height = height;
Graphics::TBitmap* bm2 = new Graphics::TBitmap;
bm2->Width = width;
bm2->Height = height;
bm1->Canvas->CopyRect(drect, vp->Canvas, srect);
bm2->Canvas->Brush->Color = to_color;
bm2->Canvas->BrushCopy(drect, bm1, drect, from_color);
BitBlt(vp->Canvas->Handle, srect.Left, srect.Top, width, height, bm2->Canvas->Handle, 0, 0, SRCCOPY);
vp->Refresh();
delete bm1;
delete bm2;
}
sel_data->_last_y = Y;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.