C++ MFC有进程没有界面-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

C++ MFC有进程没有界面

a123456678 2016-03-20 11:35:00 2157

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);
}

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

C++
c++进程
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:09:17
    #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;
    }
    
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

相似问题
最新问题
推荐课程