简单的HELLO MFC程序运行问题
这是HELLO.H头文件
class CMyApp:public CWinApp
{
public:
virtual bool initinstance();
};
class cmainwindow:public CFrameWnd
{
public:
cmainwindow();
protected:
afx_msg void onpaint();
declare_message_map();
};
这是源代码
# include<afxwin.h>
# include"hello.h"
CMyApp myApp;
/////////////////////////////////////////
bool CMyApp::initinstance()
{
m_pMainWnd=new cmainwindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return true;
}
/////////////////////////////////////////
begin_message_map(cmainwindow,CFrameWnd);
on_wm_paint();
end_message_map();
cmainwindow::cmainwindow()
{
Create(NULL,_T("the hello application"));
}
void cmainwindow::onpaint()
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.DrawText(_T("Hello,MFC!!!!"),-1,&rect,
DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}
我用VC6.0执行该程序,链接编译都没事,就是执行时好像程序会自动结束,不知道为什么
还有就是,看书上的代码里有的是大写有的是小写,那么为什么会有大小写区别,哪几种情况必须大写
两个问题呵呵~~~~·
[解决办法]
你这个程序,没有消息的处理函数 WindowProc()
也就是说,当你要退出的时候,你没有定义如何响应退出的消息,故不能结束。
我给你一个最简单的MFC程序,你可以学一下。
一下文件下载后保存为 .cpp文件。
- C/C++ code
// Ex12_01.cpp Native windows program to display text in a window#include <windows.h>LRESULT WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // Insert code for WinMain() here (Listing OFWIN_1)// Listing OFWIN_1int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ WNDCLASSEX WindowClass; // Structure to hold our window's attributes static LPCTSTR szAppName = L"OFWin"; // Define window class name HWND hWnd; // Window handle MSG msg; // Windows message structure WindowClass.cbSize = sizeof(WNDCLASSEX); // Set structure size // Redraw the window if the size changes WindowClass.style = CS_HREDRAW | CS_VREDRAW; // Define the message handling function WindowClass.lpfnWndProc = WindowProc; WindowClass.cbClsExtra = 0; // No extra bytes after the window class WindowClass.cbWndExtra = 0; // structure or the window instance WindowClass.hInstance = hInstance; // Application instance handle // Set default application icon WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION); // Set window cursor to be the standard arrow WindowClass.hCursor = LoadCursor(0, IDC_ARROW); // Set gray brush for background color WindowClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH)); WindowClass.lpszMenuName = 0; // No menu WindowClass.lpszClassName = szAppName; // Set class name WindowClass.hIconSm = 0; // Default small icon // Now register our window class RegisterClassEx(&WindowClass); // Now we can create the window hWnd = CreateWindow( szAppName, // the window class name L"A Basic Window the Hard Way", // The window title WS_OVERLAPPEDWINDOW, // Window style as overlapped CW_USEDEFAULT, // Default screen position of upper left CW_USEDEFAULT, // corner of our window as x,y... CW_USEDEFAULT, // Default window size CW_USEDEFAULT, // .... 0, // No parent window 0, // No menu hInstance, // Program Instance handle 0 // No window creation data ); ShowWindow(hWnd, nCmdShow); // Display the window UpdateWindow(hWnd); // Cause window client area to be drawn // The message loop while(GetMessage(&msg, 0, 0, 0) == TRUE) // Get any messages { TranslateMessage(&msg); // Translate the message DispatchMessage(&msg); // Dispatch the message } return static_cast<int>(msg.wParam); // End, so return to Windows} // Insert code for WindowProc() here (Listing OFWIN_2)// Listing OFWIN_2LRESULT WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hDC; // Display context handle PAINTSTRUCT PaintSt; // Structure defining area to be drawn RECT aRect; // A working rectangle switch(message) // Process selected messages { case WM_PAINT: // Message is to redraw the window hDC = BeginPaint(hWnd, &PaintSt);// Prepare to draw the window // Get upper left and lower right of client area GetClientRect(hWnd, &aRect); SetBkMode(hDC, TRANSPARENT); // Set text background mode // Now draw the text in the window client area DrawText( hDC, // Device context handle L"But, soft! What light through yonder window breaks?", -1, // Indicate null terminated string &aRect, // Rectangle in which text is to be drawn DT_SINGLELINE| // Text format - single line DT_CENTER| // - centered in the line DT_VCENTER); // - line centered in aRect EndPaint(hWnd, &PaintSt); // Terminate window redraw operation return 0; case WM_DESTROY: // Window is being destroyed PostQuitMessage(0); return 0; default: // Any other message - we don't // want to know, so call // default message processing return DefWindowProc(hWnd, message, wParam, lParam); }}