(Q1.7) How do I paint a background image on an MDI form?
(A1.7) To do this, you'll need to draw directly to the client window.
More specifically, subclass the client window (access its
handle via the ClientHandle property) and render the image in
response to the WM_ERASEBKGND message.
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
| // in header...
FARPROC NewClientWP;
FARPROC OldClientWP;
void __fastcall MDIClientWndProc(TMessage &Msg);
// in source...
__fastcall TMainForm::TMainForm(TComponent *Owner)
: TForm(Owner)
{
NewClientWP = reinterpret_cast<FARPROC>
(MakeObjectInstance(MDIClientWndProc));
OldClientWP = reinterpret_cast<FARPROC>
(SetWindowLong(ClientHandle, GWL_WNDPROC,
reinterpret_cast<LONG>(NewClientWP)));
}
void __fastcall TMainForm::MDIClientWndProc(TMessage &Msg)
{
switch (Msg.Msg)
{
// draw the image to the device context of the
// client window
case WM_ERASEBKGND:
{
HDC Hdc = reinterpret_cast<HDC>(Msg.WParam);
SelectPalette(Hdc,
Image1->Picture->Bitmap->Palette,
true);
RealizePalette(Hdc);
StretchBlt(Hdc, 0, 0,
Image1->Width, Image1->Height,
Image1->Canvas->Handle,
0, 0,
Image1->Picture->Bitmap->Width,
Image1->Picture->Bitmap->Height,
SRCCOPY);
Msg.Result = 0;
return;
}
// handle the palette changes
case WM_QUERYNEWPALETTE:
{
HDC Hdc = GetDC(ClientHandle);
SelectPalette(Hdc,
Image1->Picture->Bitmap->Palette,
true);
RealizePalette(Hdc);
InvalidateRect(ClientHandle, NULL, true);
ReleaseDC(ClientHandle, Hdc);
Msg.Result = 0;
return;
}
case WM_PALETTECHANGED:
{
if (reinterpret_cast<HWND>(Msg.WParam) != ClientHandle)
{
HDC Hdc = GetDC(ClientHandle);
SelectPalette(Hdc,
Image1->Picture->Bitmap->Palette,
true);
RealizePalette(Hdc);
UpdateColors(Hdc);
ReleaseDC(ClientHandle, Hdc);
}
Msg.Result = 0;
return;
}
// refresh the image upon scrolling
case WM_HSCROLL:
case WM_VSCROLL:
{
InvalidateRect(ClientHandle, NULL, true);
break;
}
// un-subclass the client window
case WM_DESTROY:
{
SetWindowLong(ClientHandle, GWL_WNDPROC,
reinterpret_cast<LONG>(OldClientWP));
FreeObjectInstance(NewClientWP);
}
}
// call the default window procedure
Msg.Result = CallWindowProc(OldClientWP, ClientHandle, Msg.Msg,
Msg.WParam, Msg.LParam);
} |
[
Voor 3% gewijzigd door
LordLarry op 02-02-2003 21:21
]
We adore chaos because we like to restore order - M.C. Escher