开发者社区> 问答> 正文

C++ MFC有进程没有界面

LRESULT CALLBACK WinSunPrnc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(

HINSTANCE hInstance,
HINSTANCE hPrcvInstance,
LPSTR IpCmdLine,
int nCmdShow
)

{

WNDCLASS wndcls;
wndcls.cbClsExtra = 0;//类的附加字节数
wndcls.cbWndExtra = 0;//窗口额外附加字节数
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);//背景
wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);//光标
wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);//图标a
wndcls.hInstance = hInstance;//
wndcls.lpfnWndProc = WinSunPrnc;//窗口过程实例
wndcls.lpszClassName = "First MFC";//类名化;
wndcls.lpszMenuName = NULL;//菜单的名字
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);

HWND hwnd;
hwnd = CreateWindow("First MFC 2015","吉林大学", WS_OVERLAPPEDWINDOW, 0, 100, 600, 800, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return 0; 

LRESULT CALLBACK WinSunPrnc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){

switch (uMsg)
{
case WM_CHAR:
    char szChar[20];
    sprintf(szChar, "char is %d", wParam);
    MessageBox(hwnd, szChar, TEXT("First MFC 2015"), 0);
    break;
case WM_LBUTTONDOWN:
    MessageBox(hwnd, TEXT("muse clicked"), TEXT("mause"), 0);
    HDC hdc;
    hdc = GetDC(hwnd);
    TextOut(hdc, 0, 50, TEXT("its amazing"), strlen("its amazing"));
    ReleaseDC(hwnd, hdc);
    break;
case WM_PAINT:
    HDC hDC;
    PAINTSTRUCT ps;
    hDC = BeginPaint(hwnd, &ps);
    TextOut(hDC, 0, 0, TEXT("翟羽翔"), strlen("翟羽翔"));
    EndPaint(hwnd, &ps);
    break;
case WM_CLOSE:
    if (IDYES == MessageBox(hwnd, TEXT("是否真的结束?"), TEXT("First MFC"), MB_YESNO))
    {
        DestroyWindow(hwnd);
    }
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

}
运行之后只有进程没有界面是为什么。。

展开
收起
a123456678 2016-03-20 11:35:00 3106 0
1 条回答
写回答
取消 提交回答
  • #include <Windows.h>                     //Windows程序需要包含Windows的头文件
    #include <stdio.h>
    
    //可以通过MSDN查看回调函数的声明方式
    LRESULT CALLBACK WinQinProc(HWND hwnd,
           UINT uMsg,
           WPARAM wParam,
           LPARAM lParam
    );
    
    
    
    
    //WinMain.通过查看MSDN找到声明方式
    int __stdcall WinMain( HINSTANCE hInstance,
       HINSTANCE hPrevInstance,
       LPSTR lpCmdLine,
       int nCmdShow
    )
    {
     //设计一个窗口类
     WNDCLASS wndcls;
     wndcls.cbClsExtra = 0;
     wndcls.cbWndExtra = 0;
     wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);     //白色的刷子
     wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);      //十字形光标。因为标准的,所以第一个参数为NULL
     wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);   //窗口上面是应用程序的图标
     wndcls.hInstance = hInstance;
     wndcls.lpfnWndProc = WinQinProc;                             //窗口过程函数。回调函数
     wndcls.lpszClassName = "qinwg";                                //类名
     wndcls.lpszMenuName = NULL;                                   //没有菜单
     wndcls.style = CS_HREDRAW | CS_VREDRAW;          //CS:类的样式---水平重绘、垂直重绘
    
     //注册窗口
     RegisterClass(&wndcls);
    
     //创建窗口
     HWND hwnd;
     hwnd = CreateWindow("qinwg", "qinwg's Program",WS_OVERLAPPEDWINDOW,
      50,50,600,400, NULL,NULL,hInstance,NULL);
    
     //显示窗口
     ShowWindow(hwnd, SW_SHOWNORMAL);
     UpdateWindow(hwnd);
    
     //消息循环机制
     MSG msg;
     //GetMessage取到WM_QUIT消息时,返回值为0,退出循环
     while (GetMessage(&msg, NULL, NULL, NULL))    //第二个参数为NULL,表示接受所有窗口消息
     {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
     }
    
     return 0;
    }
    
    
    //窗口过程函数实现过程
    LRESULT CALLBACK WinQinProc(HWND hwnd,
           UINT uMsg,
           WPARAM wParam,
           LPARAM lParam
    )
    {
     HDC hDC;                              //Device Context的句柄
     switch(uMsg)                       //针对不同消息处理过程
     {
     case WM_PAINT:                  //窗口重绘时,触发的消息
      PAINTSTRUCT ps;
      hDC=BeginPaint(hwnd, &ps);              //BeginPaint/EndPaint只能用在WM_PAINT消息中
      TextOut(hDC, 50, 50, "This is first MFC Program!", strlen( "This is first MFC Program!"));
      EndPaint(hwnd, &ps);
      break;
     case WM_CHAR:                 //按下键盘字母键响应消息
      char cArry[20];
      memset(cArry, 0, 20);
      sprintf(cArry, "char is %d", wParam);
      MessageBox(hwnd, cArry, "WM_CHAR", MB_OKCANCEL);
      break;
     case WM_LBUTTONDOWN:
      MessageBox(hwnd, "LeftMouse Click", "WM_LBUTTONDOWN", MB_OKCANCEL);
      hDC = GetDC(hwnd);
      TextOut(hDC, 50, 100, "MFC Program!", strlen( "MFC Program!"));
      ReleaseDC(hwnd,hDC);
      break;
     case WM_CLOSE:
      if (IDOK == MessageBox(hwnd, "是否真的要退出?", "提示", MB_OKCANCEL))
      {
       DestroyWindow(hwnd);                     //销毁窗口,同时发送WM_DESTROY消息
      }
      break;
     case WM_DESTROY:
      PostQuitMessage(0);                            //发送WM_QUIT消息
      break;
     default:
      return DefWindowProc(hwnd,uMsg,wParam,lParam);            //对待不关心的消息,采用默认方式处理
     }
    
     return 0;
    }
    
    2019-07-17 19:09:17
    赞同 展开评论 打赏
问答分类:
C++
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载

相关实验场景

更多