我在CALLBACK 函数中想调用WinCreate这个函数来创建一个新的窗体,但是编译有错 不知道怎么去改 望指点一二
还有一个问题就是 我可不可不调用WinCreate这个函数 而直接调用WinMain主函数来创建窗口可以的吗 ??
想法:单击进入这个按钮 进入另外一个窗体 当前窗体消失
#include<windows.h>
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowClass(HINSTANCE hInstance);
BOOL InitWinsow(HINSTANCE hInstance,int nCmdShow);
int WinCreate(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow);BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hwnd;
hwnd=CreateWindow(
"实验",
"实验",
WS_OVERLAPPED|WS_MINIMIZE|WS_SYSMENU|WS_CAPTION|WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
BOOL InitWindowClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WinProc;
wndclass.lpszClassName="实验";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&wndclass);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
if(!InitWindowClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int WinCreate(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
if(!InitWindowClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
int nCtrlId = LOWORD(wparam);
static HWND hStatic;
switch(message)
{
case WM_CREATE:
CreateWindow(
"button",
"进入",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
400,
200,
70,
24,
hwnd,
(HMENU)1050,
((LPCREATESTRUCT)lparam)->hInstance,
NULL
);
CreateWindow(
"button",
"退出",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
500,
200,
70,
24,
hwnd,
(HMENU)1060,
((LPCREATESTRUCT)lparam)->hInstance,
NULL
);
break;
case WM_COMMAND:
switch (nCtrlId)
{
case 1050:
//MessageBox(hwnd, "clicked me", "note", MB_OK);
//SetWindowText(hStatic, "CSDN");
//WinCreate( hInstance, hPrevInstance, lpCmdLine, nCmdShow);
break;
case 1060:
MessageBox(hwnd, "clicked me", "note", MB_OK);
SetWindowText(hStatic, "CSDN");
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wparam,lparam);
}
return 0;
}
还有一个问题就是 我可不可不调用WinCreate这个函数 而直接调用WinMain主函数来创建窗口可以的吗 ??
想法:单击进入这个按钮 进入另外一个窗体 当前窗体消失
#include<windows.h>
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowClass(HINSTANCE hInstance);
BOOL InitWinsow(HINSTANCE hInstance,int nCmdShow);
int WinCreate(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow);BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
HWND hwnd;
hwnd=CreateWindow(
"实验",
"实验",
WS_OVERLAPPED|WS_MINIMIZE|WS_SYSMENU|WS_CAPTION|WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
BOOL InitWindowClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WinProc;
wndclass.lpszClassName="实验";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&wndclass);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
if(!InitWindowClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int WinCreate(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
if(!InitWindowClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
int nCtrlId = LOWORD(wparam);
static HWND hStatic;
switch(message)
{
case WM_CREATE:
CreateWindow(
"button",
"进入",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
400,
200,
70,
24,
hwnd,
(HMENU)1050,
((LPCREATESTRUCT)lparam)->hInstance,
NULL
);
CreateWindow(
"button",
"退出",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
500,
200,
70,
24,
hwnd,
(HMENU)1060,
((LPCREATESTRUCT)lparam)->hInstance,
NULL
);
break;
case WM_COMMAND:
switch (nCtrlId)
{
case 1050:
//MessageBox(hwnd, "clicked me", "note", MB_OK);
//SetWindowText(hStatic, "CSDN");
//WinCreate( hInstance, hPrevInstance, lpCmdLine, nCmdShow);
break;
case 1060:
MessageBox(hwnd, "clicked me", "note", MB_OK);
SetWindowText(hStatic, "CSDN");
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wparam,lparam);
}
return 0;
}
解决方案 »
- HeapAlloc和malloc是工作在一个层次上面么? 到底有什么不同的地方?
- 新手,有点受不了vc啊,写的简单c++程序无法编译啊。
- Toolbar在使用一段时间后错乱
- ansi转unicode问题.使用MultiByteToWideChar
- 我用GetDC(m_hWnd)并没有获得窗口的DC,和GetDC(NULL)的效果一样。。
- 如何弹出控制面板里的数据源对话框
- 一个关于互斥的问题!
- 系统莫明报错!我晕!只得给各位老大送分!求助!帮帮我,我还没吃晚饭呢!分不多是个心意!
- 请问那有“深入解析ATL”(英文名atl internals)的电子书下载?
- 请问各位,ListBox显示的时候,如果让滚动条跟着最下面的一行?
- 如何知道注册表编辑器已经准备就绪?
- 请问一个关于屏幕处理的问题
按PROJECT->属性,在 Configuration Properties -> General ->Character Set 改成 Not Set
这样用你的代码就可以过编译,但还有错误.调用WINCREATE时的hInstance变量是WinMain的局部变量来的.这里用不了.我也再试过一下,就算用全局保存再传过来.也还是有其他问题的.
问题就在你进入RegisterClass注册的是同名的窗口类而返回了0值而使你的WinCreate函数提前返回.
但是把ProjectSetting的characterset,设置为No Set就可以编译通过。