鱼C论坛

 找回密码
 立即注册
查看: 4312|回复: 4

萌新请教一个VC++的窗口程序问题

[复制链接]
发表于 2017-1-4 10:16:11 | 显示全部楼层 |阅读模式
20鱼币
大大们,请问我这个程序为什么每次运行只有进程而没有窗口呀,RegisterWindow()里面的lpszClassName和Create()里面的lpszClassName是一样的,而且我也试过把nCmdShow设为SW_SHOW,都没用,求助, 谢谢 么么哒
  1. #include<windows.h>
  2. //=============================
  3. //HINSTANCE hInst;
  4. HINSTANCE hInstance;
  5. char lpszClassName[] = "classclassdf";
  6. char *ShowText;
  7. MSG msg;
  8. //=============================
  9. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  10. void On_LButtonDown(HWND hWnd,
  11.                                         UINT message,
  12.                                         WPARAM wParam,
  13.                                         LPARAM lParam);

  14. void On_Paint(HWND hWnd,
  15.                           UINT message,
  16.                           WPARAM wParam,
  17.                           LPARAM lParam);

  18. void On_Destroy(HWND hWnd,
  19.                                 UINT message,
  20.                                 WPARAM wParam,
  21.                                 LPARAM lParam);
  22. //=============================
  23. class CFrameWnd{
  24. public:
  25.         HWND hWnd;
  26. public:
  27.         int RegisterWindow();
  28.         void Create(LPCTSTR lpClassName, LPCTSTR lpWindowName);
  29.         void ShowWindow(int nCmdShow);
  30.         void UpdateWindow();
  31. };

  32. int CFrameWnd::RegisterWindow(){
  33.         WNDCLASS wc;
  34.         wc.cbClsExtra = 0;
  35.         wc.cbWndExtra = 0;
  36.         wc.style = 0;
  37.         wc.hInstance = hInstance;
  38.         wc.lpszClassName = lpszClassName;
  39.         wc.lpszMenuName = NULL;
  40.         wc.hCursor = LoadCursor(NULL,IDC_ARROW);
  41.         wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
  42.         wc.lpfnWndProc = WndProc;
  43.         wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  44.         return RegisterClass(&wc);
  45. }

  46. void CFrameWnd::Create(LPCTSTR lpClassName, LPCTSTR lpWindowName){
  47.         RegisterWindow();
  48. //        hInst = hInstance;
  49.         hWnd = CreateWindow(lpszClassName,
  50.                                                 lpWindowName,
  51.                                                 WS_OVERLAPPEDWINDOW,
  52.                                                 0,0,800,600,
  53.                                                 NULL,
  54.                                                 NULL,
  55.                                                 hInstance,
  56.                                                 NULL);
  57. }

  58. void CFrameWnd::ShowWindow(int nCmdWnd){
  59.         ::ShowWindow(hWnd,nCmdWnd);
  60. }

  61. void CFrameWnd::UpdateWindow(){
  62.         ::UpdateWindow(hWnd);
  63. }
  64. //=============================================================
  65. class CWinApp{
  66. public:
  67.         CWinApp *m_pCurrentWinApp;
  68. public:
  69.         CWinApp();
  70.         ~CWinApp();
  71. public:
  72.         CFrameWnd *m_pMainWnd;
  73. public:
  74.         virtual BOOL InitInstance(int nCmdShow);
  75.         int Run();
  76. };

  77. CWinApp::CWinApp(){
  78.         m_pCurrentWinApp = this;
  79. }

  80. BOOL CWinApp::InitInstance(int nCmdShow){
  81.         m_pMainWnd = new CFrameWnd;
  82.         m_pMainWnd->Create(NULL,"封装的Windows程序");
  83.         m_pMainWnd->ShowWindow(nCmdShow);
  84.         m_pMainWnd->UpdateWindow();
  85.         return TRUE;
  86. }

  87. int CWinApp::Run(){
  88.         while( GetMessage(&msg, NULL, NULL, NULL) ){
  89.                 TranslateMessage(&msg);
  90.                 DispatchMessage(&msg);
  91.         }
  92.         return msg.wParam;
  93. }

  94. CWinApp::~CWinApp(){
  95.         delete m_pMainWnd;
  96. }
  97. //==================================================
  98. class CMyWnd:public CFrameWnd{
  99. };

  100. //==================================================
  101. class CMyApp:public CWinApp{
  102. public:
  103.         BOOL InitInstance(int nCmdShow);
  104. };
  105. //==================================================
  106. BOOL CMyApp::InitInstance(int nCmdShow){
  107.         CMyWnd * pMainWnd;
  108.         pMainWnd = new CMyWnd;
  109.         pMainWnd->Create(NULL,"应用窗体的派生类的程序");
  110.         pMainWnd->ShowWindow(nCmdShow);
  111.         pMainWnd->UpdateWindow();
  112.         m_pMainWnd = pMainWnd;
  113.         return TRUE;
  114. }

  115. //==================================================
  116. CWinApp MyApp;
  117. //==================================================
  118. CWinApp *AfxGetApp(){
  119.         return MyApp.m_pCurrentWinApp;
  120. }
  121. //==================================================
  122. int APIENTRY WinMain(HINSTANCE hInstance,
  123.                                          HINSTANCE hPrevInstance,
  124.                                          LPSTR           lpCmdLine,
  125.                                          int           nCmdShow)
  126. {
  127.         int ResultCode = -1;
  128.         CWinApp *pApp;
  129.         pApp = AfxGetApp();
  130.         pApp->InitInstance(SW_SHOW);
  131.         return ResultCode = pApp->Run();
  132. }

  133. //==================================================
  134. LRESULT CALLBACK WndProc(HWND hWnd,
  135.                                                  UINT message,
  136.                                                  WPARAM wParam,
  137.                                                  LPARAM lParam)
  138. {
  139.         switch(message){
  140.         case WM_LBUTTONDOWN:
  141.                 On_LButtonDown(hWnd,message,wParam,lParam);
  142.                 break;
  143.         case WM_PAINT:
  144.                 On_Paint(hWnd,message,wParam,lParam);
  145.                 break;
  146.         case WM_DESTROY:
  147.                 On_Destroy(hWnd,message,wParam,lParam);
  148.         default:
  149.                 DefWindowProc(hWnd,message,wParam,lParam);
  150.         }
  151.         return 0;
  152. }

  153. void On_LButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
  154.         ShowText = "Hello!";
  155.         InvalidateRect(hWnd,NULL,1);
  156. }

  157. void On_Paint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
  158.         PAINTSTRUCT ps;
  159.         HDC hdc;
  160.         hdc = BeginPaint(hWnd, &ps);
  161.         TextOut(hdc, 50, 50, ShowText, 6);
  162.         EndPaint(hWnd,&ps);
  163. }

  164. void On_Destroy(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
  165.         PostQuitMessage(0);
  166. }
复制代码

最佳答案

查看完整内容

虽然还没学到VC++写的窗口程序 ,但是也看了下小甲鱼老师的SDK视频,我大概猜想下 ,你能不能把 WINMAIN头放在程序前面,把处理程序过程放在后面,在运行下程序看行不,如果不行那就要好好检查程序内容了 如果成功希望奖励下鱼币
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-1-4 10:16:12 | 显示全部楼层
虽然还没学到VC++写的窗口程序 ,但是也看了下小甲鱼老师的SDK视频,我大概猜想下 ,你能不能把 WINMAIN头放在程序前面,把处理程序过程放在后面,在运行下程序看行不,如果不行那就要好好检查程序内容了  如果成功希望奖励下鱼币
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-1-9 12:34:51 | 显示全部楼层
谦虚求学 发表于 2017-1-4 10:16
虽然还没学到VC++写的窗口程序 ,但是也看了下小甲鱼老师的SDK视频,我大概猜想下 ,你能不能把 WINMAIN头 ...

感谢回复,不过好像不是WINAPI ENTRY WINMAIN的位置问题呢。我再试试,谢谢啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-19 09:50:43 | 显示全部楼层
  1. int APIENTRY WinMain(HINSTANCE hInstance,
  2.                                          HINSTANCE hPrevInstance,
  3.                                          LPSTR           lpCmdLine,
  4.                                          int           nCmdShow)
  5. {
复制代码

  1. hInstance//这个是系统传进去的,相当于系统分配的
  2. //而你
  3. wc.hInstance = hInstance;//和
  4. hWnd = CreateWindow(lpszClassName,
  5. lpWindowName,
  6. WS_OVERLAPPEDWINDOW,
  7. 0,0,800,600,
  8. NULL,
  9. NULL,
  10. hInstance,
  11. NULL);
  12. //都是你的全局变量,HINSTANCE hInstance;,并没有分配实际的句柄
复制代码


msdn的例子

  1. #include <windows.h>

  2. // Global variable

  3. HINSTANCE hinst;

  4. // Function prototypes.

  5. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
  6. InitApplication(HINSTANCE);
  7. InitInstance(HINSTANCE, int);
  8. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

  9. // Application entry point.

  10. int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
  11.     LPSTR lpCmdLine, int nCmdShow)
  12. {
  13.     MSG msg;

  14.     if (!InitApplication(hinstance))
  15.         return FALSE;

  16.     if (!InitInstance(hinstance, nCmdShow))
  17.         return FALSE;

  18.     BOOL fGotMessage;
  19.     while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
  20.     {
  21.         TranslateMessage(&msg);
  22.         DispatchMessage(&msg);
  23.     }
  24.     return msg.wParam;
  25.         UNREFERENCED_PARAMETER(lpCmdLine);
  26. }

  27. BOOL InitApplication(HINSTANCE hinstance)
  28. {
  29.     WNDCLASSEX wcx;

  30.     // Fill in the window class structure with parameters
  31.     // that describe the main window.

  32.     wcx.cbSize = sizeof(wcx);          // size of structure
  33.     wcx.style = CS_HREDRAW |
  34.         CS_VREDRAW;                    // redraw if size changes
  35.     wcx.lpfnWndProc = MainWndProc;     // points to window procedure
  36.     wcx.cbClsExtra = 0;                // no extra class memory
  37.     wcx.cbWndExtra = 0;                // no extra window memory
  38.     wcx.hInstance = hinstance;         // handle to instance
  39.     wcx.hIcon = LoadIcon(NULL,
  40.         IDI_APPLICATION);              // predefined app. icon
  41.     wcx.hCursor = LoadCursor(NULL,
  42.         IDC_ARROW);                    // predefined arrow
  43.     wcx.hbrBackground = GetStockObject(
  44.         WHITE_BRUSH);                  // white background brush
  45.     wcx.lpszMenuName =  "MainMenu";    // name of menu resource
  46.     wcx.lpszClassName = "MainWClass";  // name of window class
  47.     wcx.hIconSm = LoadImage(hinstance, // small class icon
  48.         MAKEINTRESOURCE(5),
  49.         IMAGE_ICON,
  50.         GetSystemMetrics(SM_CXSMICON),
  51.         GetSystemMetrics(SM_CYSMICON),
  52.         LR_DEFAULTCOLOR);

  53.     // Register the window class.

  54.     return RegisterClassEx(&wcx);
  55. }

  56. BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
  57. {
  58.     HWND hwnd;

  59.     // Save the application-instance handle.

  60.     hinst = hinstance;

  61.     // Create the main window.

  62.     hwnd = CreateWindow(
  63.         "MainWClass",        // name of window class
  64.         "Sample",            // title-bar string
  65.         WS_OVERLAPPEDWINDOW, // top-level window
  66.         CW_USEDEFAULT,       // default horizontal position
  67.         CW_USEDEFAULT,       // default vertical position
  68.         CW_USEDEFAULT,       // default width
  69.         CW_USEDEFAULT,       // default height
  70.         (HWND) NULL,         // no owner window
  71.         (HMENU) NULL,        // use class menu
  72.         hinstance,           // handle to application instance
  73.         (LPVOID) NULL);      // no window-creation data

  74.     if (!hwnd)
  75.         return FALSE;

  76.     // Show the window and send a WM_PAINT message to the window
  77.     // procedure.

  78.     ShowWindow(hwnd, nCmdShow);
  79.     UpdateWindow(hwnd);
  80.     return TRUE;

  81. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-19 09:51:22 | 显示全部楼层
识海君 发表于 2017-1-9 12:34
感谢回复,不过好像不是WINAPI ENTRY WINMAIN的位置问题呢。我再试试,谢谢啦

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 04:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表