一个简单的界面问题,请高手指点
下面这段代码是因该是ok的,可是当我把有关显示的部分移到dialog的回调函数中,就错了
#include <windows.h>
#include "resource.h"
#include <commdlg.h>
#include <windowsx.h>
#include <assert.h> // for assert() macro
// Prototypes for WndProc and Dialog procedures
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK ButtsDlgProc(HWND, UINT, WPARAM, LPARAM);
void ShowBitmap (HDC, HBITMAP, int, int, DWORD);
// Global data
HINSTANCE pInstance; // Handle to instance
HWND hDlgModeless; // Handle to modeless DB
// Variables accessed by several functions
int modeless = 0;
int iIndex;
char szBuffer[] = " ";
char clearStr[] = " ";
/*********************************
WinMain()
********************************/
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "Demo" ;
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
RegisterClassEx (&wndclass) ;
pInstance = hInstance;
hwnd = CreateWindow (szAppName,
"Dialog Box Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
// The message loop must be modified to handle the modless
// dialog box
while (GetMessage (&msg, NULL, 0, 0))
{
if (hDlgModeless == 0 || !IsDialogMessage (hDlgModeless, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return msg.wParam ;
}
/********************************
Windows procedure
*********************************/
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar, cxClient, cyClient, mapMode ;
static int drawMode; // Screen mode control
static HDC hdc;
PAINTSTRUCT ps ;
static HBITMAP nebula;
switch (iMsg)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
nebula = LoadBitmap (pInstance, MAKEINTRESOURCE(IDB_BITMAP2));
return 0 ;
case WM_PAINT :
BeginPaint (hwnd, &ps) ;
if (drawMode)
ShowBitmap (hdc, nebula, 10, 10, SRCCOPY);
EndPaint (hwnd, &ps);
return 0 ;
case WM_COMMAND:
switch (LOWORD (wParam)) {
case ID_DIALOG_BUTTONS:
drawMode = 1;
InvalidateRect (hwnd, NULL, TRUE);
MessageBeep(0);
DialogBox (pInstance, MAKEINTRESOURCE (IDD_DIALOG2),hwnd, (DLGPROC) ButtsDlgProc) ;
return 0 ;
}
break;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
/*******************************************
Buttons dialog box procedure
*******************************************/
BOOL CALLBACK ButtsDlgProc (HWND hwnd, UINT iMsg,
WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_COMMAND :
switch (LOWORD (wParam))
{
case IDOK :
MessageBeep(0);
MessageBox (hwnd,"OK Button Pressed","Dialog Box Action",MB_ICONEXCLAMATION | MB_OK);// Handle to the dialog window
EndDialog (hwnd, 0) ;
return TRUE ;
}
break ;
}
return FALSE ;
}
void ShowBitmap (HDC hdc, HBITMAP hBitmap, int xStart, int yStart,\
DWORD rop3)
{
BITMAP bm; // BITMAP structure
HDC memoryDc; // Handle to memory DC
POINT ptSize; // POINT for DC
POINT ptOrigin; // POINT for memory DC
int mapMode; // Mapping mode
memoryDc = CreateCompatibleDC (NULL);
mapMode = GetMapMode (hdc);
SetMapMode (memoryDc, mapMode);
assert (SelectBitmap (memoryDc, hBitmap));
GetObject (hBitmap, sizeof(BITMAP), (LPVOID) &bm);
ptSize.x = bm.bmWidth;
ptSize.y = bm.bmHeight;
DPtoLP (hdc, &ptSize, 1);
ptOrigin.x = 0;
ptOrigin.y = 0;
DPtoLP (memoryDc, &ptOrigin, 1);
BitBlt( hdc, xStart, yStart, ptSize.x, ptSize.y, memoryDc,ptOrigin.x, ptOrigin.y, rop3);
DeleteDC (memoryDc);
}