我是WPF的新手。
我在mfc中实现了代码,它加载位图并在窗口上绘制一些部分 现在想要在WPF中移植类似的功能。
我想在WPF窗口上绘制位图并进行一些操作。窗口没有最小值,最大值,关闭按钮。
下面是我的mfc代码。
LRESULT Mywindow::OnPaint(UINT, WPARAM, LPARAM, BOOL&)
{
CPaintDC dc(this);
CDC cdc;
HDC hdc = dc.GetSafeHdc();
cdc.Attach(hdc);
paintBitmapOnWindow(&cdc);
}
void Mywindow::paintBitmapOnWindow(CDC* pCdc)
{
HBITMAP backgroundBitmap;
backgroundBitmap = (HBITMAP)::LoadImage(0, _T("C:\\temp.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CDC cdcCompatible;
cdcCompatible.CreateCompatibleDC(pCdc);
cdcCompatible.SelectObject(backgroundBitmap);
CRect clientRect;
GetClientRect(clientRect);
int clientHalfWidth = 800 / 2;
CRect clientLeft;
CRect imageLeft;
BITMAP bm;
::GetObject(backgroundBitmap, sizeof(BITMAP), (PSTR)&bm);
clientLeft.SetRect(clientRect.left, clientRect.top, clientRect.left + clientHalfWidth, clientRect.bottom);
int bitmapHalfWidth = bm.bmWidth / 2;
int bitmapLeftOffset = bitmapHalfWidth - 0;
imageLeft.SetRect(bitmapLeftOffset - clientLeft.Width(), 0, bitmapLeftOffset, bm.bmHeight);
pCdc->StretchBlt(clientLeft.left, clientLeft.top, clientLeft.Width(), clientLeft.Height(), &cdcCompatible,
imageLeft.left, imageLeft.top, imageLeft.Width(), imageLeft.Height(), SRCCOPY);
cdcCompatible.DeleteDC();
}
我可以使用哪些类似的函数/类将其转换为WPF?
答案 0 :(得分:1)
如果您想直接将位图blit到窗口,并且您希望直接控制渲染,您可以简单地覆盖OnRender函数。但是有捕获(在代码的注释中注明)......
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Background = Brushes.Transparent; // <-- necessary (strange bug)
// Charles Petzold writes about this at the following link:
// http://social.msdn.microsoft.com/Forums/vstudio/en-US/750e91c2-c370-4f0a-b18e-892afd99bd9b/drawing-in-onrender-beginnerquestion?forum=wpf
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
BitmapSource bitmapSource = new BitmapImage(new Uri("C:\\Temp.png", UriKind.Absolute));
CroppedBitmap croppedBitmap = new CroppedBitmap(bitmapSource, new Int32Rect(20, 20, 100, 100));
drawingContext.DrawImage(croppedBitmap, new Rect(0.0d, 0.0d, this.RenderSize.Width / 2.0d, this.RenderSize.Height));
}
}