我是通过一个菜单来打开资料文件中的一个窗口,但打开后就只能看见
窗口中的两个<ok> 与<Cancel>按钮,
代码如下:
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "resource.h"#ifndef IDM_NEW
#define IDM_NEW 10 //新建
#define IDM_OPEN 11
#define IDM_CLOSE 12
#define IDM_SAVE 13
#define IDM_SAVEAS 14
#define IDM_ADDMENU 15
#define IDM_DELETEMENU 16
#define IDM_EXIT 17
#define IDM_CUT 18
#define IDM_COPY 19
#define IDM_PASTE 20
#define IDM_DELETE 21
#define IDM_HELP 22
#endif
#define IDM_LINE 23
#define IDM_RECT 24
#define IDM_RRECT 25
#define IDM_ELLIP 26
//////菜单资料的定义//////////////////////////////////
//////////////////////////////////////////////////////HMENU hmenu,hprintmenu ;
HINSTANCE hinst ;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM) ;
bool CALLBACK dlgProc(HWND,UINT,WPARAM,LPARAM) ;int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpszcmdline,
int ncmdshow
)
{
hinst = hinstance ;
HWND hwnd ;
MSG msg ;
HACCEL haccel ;
WNDCLASS wndclass ;
char lpszmenuname[] = "MENUD" ;
char lpszclassname[] = "菜单" ;
char lpsztitle[] = "my--menu--example" ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ;
//wndclass.hCursor = LoadCursor(hinstance,IDC_CROSS) ;
wndclass.hCursor = LoadCursor(hinstance,MAKEINTRESOURCE(IDC_CURSOR1)) ;
wndclass.hIcon = LoadIcon(hinstance,IDI_HAND) ;
wndclass.hInstance = hinstance ;
wndclass.lpfnWndProc =WndProc ;
wndclass.lpszClassName = lpszclassname ;
wndclass.lpszMenuName = MAKEINTRESOURCE(MENUD) ;
wndclass.style = 0 ;if (!RegisterClass(&wndclass))
{
MessageBeep(0) ;
return false ;
};hwnd = CreateWindow(lpszclassname,
lpsztitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hinstance,
NULL
) ;
ShowWindow(hwnd,ncmdshow) ;
UpdateWindow(hwnd) ;//加载加速键资源
haccel = LoadAccelerators(hinstance,lpszmenuname) ;while (GetMessage(&msg,NULL,0,0))
{
//在消息循环中截获加速键消息
if (!TranslateAccelerator(hwnd,haccel,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg) ;
};
};return msg.wParam ;
};LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
switch (message)
{
case WM_COMMAND : //菜单样一类信息
switch (LOWORD(wparam))
{
case IDM_NEW :
DialogBox(hinst,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DLGPROC(dlgProc)) ;
MessageBox(hwnd,"打开菜单","提示",MB_OK);
break ;
case IDM_OPEN :
break ;
case IDM_SAVE :
break ;
case IDM_SAVEAS :
break ;
case IDM_ADDMENU : //增加菜单
hmenu = GetMenu(hwnd) ;//得到窗口菜单句柄
hprintmenu = CreateMenu() ; //创建一个空的菜单句柄
//加入一些菜单项
AppendMenu(hprintmenu,MF_ENABLED,IDM_LINE,"求和");
AppendMenu(hprintmenu,MF_ENABLED,IDM_RECT,"方差");
AppendMenu(hprintmenu,MF_ENABLED,IDM_RRECT,"平均差");
AppendMenu(hprintmenu,MF_ENABLED,IDM_ELLIP,"均方根");
//将创建的菜单插入主菜单中
InsertMenu(hmenu,2,MF_POPUP|MF_BYPOSITION,(UINT)hprintmenu,"统计计算(&c)") ;
//相应改变菜单的有关属性
EnableMenuItem(hmenu,IDM_ADDMENU,MF_GRAYED) ;
EnableMenuItem(hmenu,IDM_DELETEMENU,MF_ENABLED) ;
DrawMenuBar(hwnd) ; //重画菜单
break ;
case IDM_DELETEMENU : //删除菜单
DeleteMenu(hmenu,2,MF_BYPOSITION) ; //删除统计
//重置菜单
EnableMenuItem(hmenu,IDM_ADDMENU,MF_ENABLED) ;
EnableMenuItem(hmenu,IDM_DELETEMENU,MF_GRAYED) ;
DrawMenuBar(hwnd) ;
break ;
case IDM_EXIT :
SendMessage(hwnd,WM_DESTROY,0,0) ;
break ;
default :
MessageBox(hwnd,"默认资讯","提示",MB_OK);
return DefWindowProc(hwnd,message,wparam,lparam) ;
};
break ;
case WM_DESTROY :
PostQuitMessage(0) ;
break ;
default :
//MessageBox(hwnd,"默认资讯-外围","提示",MB_OK);
return DefWindowProc(hwnd,message,wparam,lparam) ;
};
return 0 ;
}; bool CALLBACK dlgProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam) ////对画窗口
{
//MessageBox(hwnd,"dlgproc初始化消息!","提示",MB_OK) ;
switch(message)
{
case WM_INITDIALOG :
MessageBox(hwnd,"窗口初始化消息!","提示",MB_OK) ;
return 1 ;
case WM_CLOSE :
EndDialog(hwnd,1) ;
return 1 ;
};return 1 ;
};运得上面程序,
当单击菜单上<IDM_NEW> 时则运行
DialogBox(hinst,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DLGPROC(dlgProc)) ;
这是只能看见窗口上的控件按钮,且好像没有运行相应窗口过程的
case WM_INITDIALOG :
MessageBox(hwnd,"窗口初始化消息!","提示",MB_OK) ; ///---没有运行初始化信息
return 1 ;
case WM_CLOSE : //-----------按<cancel>不结束窗口
EndDialog(hwnd,1) ;
return 1 ;如果我改为:
DialogBox(hinst,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,NULL) ; 不指定窗口过程, 则可以看到整个画面请高手指点.......
谢天谢地
窗口中的两个<ok> 与<Cancel>按钮,
代码如下:
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "resource.h"#ifndef IDM_NEW
#define IDM_NEW 10 //新建
#define IDM_OPEN 11
#define IDM_CLOSE 12
#define IDM_SAVE 13
#define IDM_SAVEAS 14
#define IDM_ADDMENU 15
#define IDM_DELETEMENU 16
#define IDM_EXIT 17
#define IDM_CUT 18
#define IDM_COPY 19
#define IDM_PASTE 20
#define IDM_DELETE 21
#define IDM_HELP 22
#endif
#define IDM_LINE 23
#define IDM_RECT 24
#define IDM_RRECT 25
#define IDM_ELLIP 26
//////菜单资料的定义//////////////////////////////////
//////////////////////////////////////////////////////HMENU hmenu,hprintmenu ;
HINSTANCE hinst ;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM) ;
bool CALLBACK dlgProc(HWND,UINT,WPARAM,LPARAM) ;int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpszcmdline,
int ncmdshow
)
{
hinst = hinstance ;
HWND hwnd ;
MSG msg ;
HACCEL haccel ;
WNDCLASS wndclass ;
char lpszmenuname[] = "MENUD" ;
char lpszclassname[] = "菜单" ;
char lpsztitle[] = "my--menu--example" ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ;
//wndclass.hCursor = LoadCursor(hinstance,IDC_CROSS) ;
wndclass.hCursor = LoadCursor(hinstance,MAKEINTRESOURCE(IDC_CURSOR1)) ;
wndclass.hIcon = LoadIcon(hinstance,IDI_HAND) ;
wndclass.hInstance = hinstance ;
wndclass.lpfnWndProc =WndProc ;
wndclass.lpszClassName = lpszclassname ;
wndclass.lpszMenuName = MAKEINTRESOURCE(MENUD) ;
wndclass.style = 0 ;if (!RegisterClass(&wndclass))
{
MessageBeep(0) ;
return false ;
};hwnd = CreateWindow(lpszclassname,
lpsztitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hinstance,
NULL
) ;
ShowWindow(hwnd,ncmdshow) ;
UpdateWindow(hwnd) ;//加载加速键资源
haccel = LoadAccelerators(hinstance,lpszmenuname) ;while (GetMessage(&msg,NULL,0,0))
{
//在消息循环中截获加速键消息
if (!TranslateAccelerator(hwnd,haccel,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg) ;
};
};return msg.wParam ;
};LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
switch (message)
{
case WM_COMMAND : //菜单样一类信息
switch (LOWORD(wparam))
{
case IDM_NEW :
DialogBox(hinst,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DLGPROC(dlgProc)) ;
MessageBox(hwnd,"打开菜单","提示",MB_OK);
break ;
case IDM_OPEN :
break ;
case IDM_SAVE :
break ;
case IDM_SAVEAS :
break ;
case IDM_ADDMENU : //增加菜单
hmenu = GetMenu(hwnd) ;//得到窗口菜单句柄
hprintmenu = CreateMenu() ; //创建一个空的菜单句柄
//加入一些菜单项
AppendMenu(hprintmenu,MF_ENABLED,IDM_LINE,"求和");
AppendMenu(hprintmenu,MF_ENABLED,IDM_RECT,"方差");
AppendMenu(hprintmenu,MF_ENABLED,IDM_RRECT,"平均差");
AppendMenu(hprintmenu,MF_ENABLED,IDM_ELLIP,"均方根");
//将创建的菜单插入主菜单中
InsertMenu(hmenu,2,MF_POPUP|MF_BYPOSITION,(UINT)hprintmenu,"统计计算(&c)") ;
//相应改变菜单的有关属性
EnableMenuItem(hmenu,IDM_ADDMENU,MF_GRAYED) ;
EnableMenuItem(hmenu,IDM_DELETEMENU,MF_ENABLED) ;
DrawMenuBar(hwnd) ; //重画菜单
break ;
case IDM_DELETEMENU : //删除菜单
DeleteMenu(hmenu,2,MF_BYPOSITION) ; //删除统计
//重置菜单
EnableMenuItem(hmenu,IDM_ADDMENU,MF_ENABLED) ;
EnableMenuItem(hmenu,IDM_DELETEMENU,MF_GRAYED) ;
DrawMenuBar(hwnd) ;
break ;
case IDM_EXIT :
SendMessage(hwnd,WM_DESTROY,0,0) ;
break ;
default :
MessageBox(hwnd,"默认资讯","提示",MB_OK);
return DefWindowProc(hwnd,message,wparam,lparam) ;
};
break ;
case WM_DESTROY :
PostQuitMessage(0) ;
break ;
default :
//MessageBox(hwnd,"默认资讯-外围","提示",MB_OK);
return DefWindowProc(hwnd,message,wparam,lparam) ;
};
return 0 ;
}; bool CALLBACK dlgProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam) ////对画窗口
{
//MessageBox(hwnd,"dlgproc初始化消息!","提示",MB_OK) ;
switch(message)
{
case WM_INITDIALOG :
MessageBox(hwnd,"窗口初始化消息!","提示",MB_OK) ;
return 1 ;
case WM_CLOSE :
EndDialog(hwnd,1) ;
return 1 ;
};return 1 ;
};运得上面程序,
当单击菜单上<IDM_NEW> 时则运行
DialogBox(hinst,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DLGPROC(dlgProc)) ;
这是只能看见窗口上的控件按钮,且好像没有运行相应窗口过程的
case WM_INITDIALOG :
MessageBox(hwnd,"窗口初始化消息!","提示",MB_OK) ; ///---没有运行初始化信息
return 1 ;
case WM_CLOSE : //-----------按<cancel>不结束窗口
EndDialog(hwnd,1) ;
return 1 ;如果我改为:
DialogBox(hinst,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,NULL) ; 不指定窗口过程, 则可以看到整个画面请高手指点.......
谢天谢地
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
而且默认返回值应该是0,表示让系统继续处理这个消息,你的默认返回是最后return 1;这表示此消息已经处理过了,系统不需要再处理,包括WM_PAINT在内的消息都没有默认处理,你自己又没有画窗口,当然看不到对话框窗口啦,对话框的回调过程这样定义:
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
MessageBox(hwnd,"窗口初始化消息!","提示",MB_OK) ;
return TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
桔皮先生, 你一定是VC高手中的高手,小生很是
佩服VC 程式員, 覺得那才是有挑戰性的工作,
我目前是用PB的正在學VC ,
以後要多指點啊...mail address:
[email protected]
如果我加了
default:
return DefWindowProc(hwnd,message,wparam,lparam) ;
代码如下 :
最后依然写成
return 1 为何也不行,
一定要写成
return 0 窗口才显示出来.
有点不明,难道他没有执行
return DefWindowProc(hwnd,message,wparam,lparam) ; 语句吗?
switch(message)
{
case WM_INITDIALOG :
MessageBox(hwnd,"怠﹍て!","矗ボ",MB_OK) ;
return 1 ;
case WM_CLOSE :
EndDialog(hwnd,1) ;
return false ;
case WM_COMMAND :
switch(LOWORD(wparam))
{
case ID_OK:
//EndDialog(hwnd,0) ;
DestroyWindow(hwnd) ;
return false ;
case IDCANCEL :
//EndDialog(hwnd,0) ;
DestroyWindow(hwnd) ;
return false ;
default:
return DefWindowProc(hwnd,message,wparam,lparam) ;
};
};
return 1 ;return 1 ;
};