一个关于在MFC中用WIN32API编程的问题,请大家帮忙!
我建立一个MFC工程,然后在界面上加了一个按钮,我想用WIN32API的函数创建一个窗口,编译成功了,但在点击按钮时想要得窗口不出现,不知什麽原因,请大家给与指点,多谢!代码如下:
void CFire06Dlg::OnButton1()
{
HINSTANCE hInst=NULL;
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,GetModuleHandle(NULL), LoadIcon(hInst, MAKEINTRESOURCE(IDR_MAINFRAME)), NULL, NULL, NULL,CLASSNAME, NULL };
// Create the application 's window
HWND hWnd = CreateWindow( CLASSNAME, CLASSNAME,
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL );
::ShowWindow( hWnd, SW_SHOWDEFAULT );
::UpdateWindow( hWnd );
}
[解决办法]
给个完整的例子,在mfc中最好不要用sdk代码,这样难以维护和管理
#include <windows.h>
// Global variable
HINSTANCE hinst;
// Function prototypes.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
InitApplication(HINSTANCE);
InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if (!InitApplication(hinstance))
return FALSE;
if (!InitInstance(hinstance, nCmdShow))
return FALSE;
while (GetMessage(&msg, (HWND) NULL, 0, 0) != 0 && GetMessage(&msg, (HWND) NULL, 0, 0) != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASSEX wcx;
// Fill in the window class structure with parameters
// that describe the main window.
wcx.cbSize = sizeof(wcx); // size of structure
wcx.style = CS_HREDRAW |
CS_VREDRAW; // redraw if size changes
wcx.lpfnWndProc = MainWndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = hinstance; // handle to instance
wcx.hIcon = LoadIcon(NULL,
IDI_APPLICATION); // predefined app. icon
wcx.hCursor = LoadCursor(NULL,
IDC_ARROW); // predefined arrow
wcx.hbrBackground = GetStockObject(
WHITE_BRUSH); // white background brush
wcx.lpszMenuName = "MainMenu "; // name of menu resource
wcx.lpszClassName = "MainWClass "; // name of window class
wcx.hIconSm = LoadImage(hinstance, // small class icon
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
// Register the window class.
return RegisterClassEx(&wcx);
}
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
HWND hwnd;
// Save the application-instance handle.
hinst = hinstance;
// Create the main window.
hwnd = CreateWindow(
"MainWClass ", // name of window class
"Sample ", // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND) NULL, // no owner window
(HMENU) NULL, // use class menu
hinstance, // handle to application instance
(LPVOID) NULL); // no window-creation data
if (!hwnd)
return FALSE;
// Show the window and send a WM_PAINT message to the window
// procedure.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
[解决办法]
楼主的代码有意思```在MFC的按钮事情CreateWindow`哈哈
楼主是不是少了注册窗体
我跑了你的代码`发现是可以的
LRESULT APIENTRY MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void CFfffffffffffffffffffyyyyyyyyyyyyyyyyyyyyyDlg::OnButton1()
{
// TODO: Add your control notification handler code here
HINSTANCE hInst=NULL;
WNDCLASSEX wc = { sizeof(WNDCLASSEX),
CS_CLASSDC,
MsgProc,
0L,
0L,
GetModuleHandle(NULL),
LoadIcon(hInst, MAKEINTRESOURCE(IDR_MAINFRAME)),
NULL,
NULL,
NULL,
CLASSNAME,
NULL };
ATOM a = RegisterClassEx(&wc); // 我加了这句
// Create the application 's window
HWND hWnd = CreateWindow( CLASSNAME, CLASSNAME,
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL );
::ShowWindow( hWnd, SW_SHOWDEFAULT );
::UpdateWindow( hWnd );
}
[解决办法]
没有不对``很正常``不过你的方式不那么常用``一般是在vc里,创建一个dialog,就是通过vc 来创建``不是代码``点菜单insert-> new form就可以了``然后需要弹出该窗口的地方``就dialog.domodle(忘了是不是domodle)``
[解决办法]
是dialog.Domodal() 选MFC 还要createWindow() -_-#