一个简单的界面问题,请高手指点

zhengyaxin_8bit 2007-09-05 09:06:57
下面这段代码是因该是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);
}
...全文
156 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhengyaxin_8bit 2007-09-05
  • 打赏
  • 举报
回复
出来了!谢谢2位!
nebula = LoadBitmap (pInstance, MAKEINTRESOURCE(IDB_BITMAP2));
放在dialog中pInstance,没有给值,会不会有问题?
pInstance在主窗口创建给过值
BUbuWander 2007-09-05
  • 打赏
  • 举报
回复
WM_INITDIALOG 哦 对
chehw 2007-09-05
  • 打赏
  • 举报
回复
对话框回调函数中不处理WM_CREATE. 应将WM_CREATE后面的代码放在WM_INITDIALOG下
zhengyaxin_8bit 2007-09-05
  • 打赏
  • 举报
回复
assert (SelectBitmap (memoryDc, hBitmap));
这个出错
移动后代码如下
BOOL CALLBACK ButtsDlgProc (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 IDOK :
drawMode = 1;
InvalidateRect (hwnd, NULL, TRUE);
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 ;
}
BUbuWander 2007-09-05
  • 打赏
  • 举报
回复
你把显示的移动过去后,你调试下看下哪行抛出异常 什么异常
zhengyaxin_8bit 2007-09-05
  • 打赏
  • 举报
回复
有关显示部分的代码如下:我把这些移到dialog中
static int cxChar, cxCaps, cyChar, cxClient, cyClient, mapMode ;
static int drawMode; // Screen mode control
static HDC hdc;
PAINTSTRUCT ps ;
static HBITMAP nebula;
;-------------------------------------------------------------
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 ;
;-----------------------------------------------------------
drawMode = 1;
InvalidateRect (hwnd, NULL, TRUE);
BUbuWander 2007-09-05
  • 打赏
  • 举报
回复
"有关显示的部分"是?
chehw 2007-09-05
  • 打赏
  • 举报
回复
刚才没看见这一行.
全局变量赋过一次值就可以了.
zhengyaxin_8bit 2007-09-05
  • 打赏
  • 举报
回复
不理解
我在 RegisterClassEx (&wndclass) ;
pInstance = hInstance; 有赋值

在dialog中没有赋值行不行??
chehw 2007-09-05
  • 打赏
  • 举报
回复
int WinMain(...)
{
pInstance=hInstance;

...
RegisterClassEx(...);
...
return 0L;
}

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧