VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 465|回复: 4

程序在后台运行,望达人赐教!

[复制链接]

1

主题

3

帖子

4.00

积分

新手上路

Rank: 1

积分
4.00
发表于 2020-1-11 02:00:01 | 显示全部楼层 |阅读模式
为什么程序会在后台运行啊,实在是想不通,望前辈能赐教,不能嫌问题简单啊,本人刚开始学VC,源码如下:


#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);



class CWnd
{
        public:
                CWnd();
                BOOL CreateEx(
                          DWORD dwExStyle,      // extended window style
                          LPCTSTR lpClassName,  // pointer to registered class name
                          LPCTSTR lpWindowName, // pointer to window name
                          DWORD dwStyle,        // window style
                          int x,                // horizontal position of window
                          int y,                // vertical position of window
                          int nWidth,           // window width
                          int nHeight,          // window height
                          HWND hWndParent,      // handle to parent or owner window
                          HMENU hMenu,          // handle to menu, or child-window identifier
                          HINSTANCE hInstance,  // handle to application instance
                          LPVOID lpParam);      // pointer to window-creation data

                BOOL  ShowWindow(int nCmdShow); // show state of window
                BOOL  UpdateWindow();

        public:
                HWND m_hWnd;

};

CWnd::CWnd()
{
        m_hWnd=NULL;
}

BOOL CWnd::CreateEx(
                          DWORD dwExStyle,      // extended window style
                          LPCTSTR lpClassName,  // pointer to registered class name
                          LPCTSTR lpWindowName, // pointer to window name
                          DWORD dwStyle,        // window style
                          int x,                // horizontal position of window
                          int y,                // vertical position of window
                          int nWidth,           // window width
                          int nHeight,          // window height
                          HWND hWndParent,      // handle to parent or owner window
                          HMENU hMenu,          // handle to menu, or child-window identifier
                          HINSTANCE hInstance,  // handle to application instance
                          LPVOID lpParam)
{
        m_hWnd=::CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,
                                    nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam);
        if(m_hWnd!=NULL)
                return TRUE;
        else
                return FALSE;

}

BOOL CWnd::ShowWindow(int nCmdShow)
{
        return ::ShowWindow(m_hWnd,nCmdShow);

}

BOOL CWnd::UpdateWindow()
{
        return ::UpdateWindow(m_hWnd);
}

int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{
        WNDCLASS   wndcls;
        wndcls.cbClsExtra=0;
        wndcls.cbWndExtra=0;
        wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
        wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
        wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
        wndcls.lpfnWndProc=WinSunProc;
        wndcls.lpszClassName="sun2006";
        wndcls.lpszMenuName=NULL;
        wndcls.style=CS_VREDRAW | CS_HREDRAW;
        RegisterClass(&wndcls);
    CWnd cwnd;
        cwnd.CreateEx(NULL,"sun2006","weixin",WS_OVERLAPPEDWINDOW,0,0,
                           800,600,NULL,NULL,hInstance,NULL);

        cwnd.ShowWindow(SW_SHOWNORMAL);

    cwnd.UpdateWindow();

        MSG msg;
        while(GetMessage(&msg,NULL,0,0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }

return 0;
}       

LRESULT CALLBACK WinSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
        switch(uMsg)
        {
                case WM_CLOSE:
                        if(IDYES==::MessageBox(hwnd,"真的要退出吗?","警告",MB_YESNO))
                        {
                                ::DestroyWindow(hwnd);
                        }
                       
                break;
        case WM_DESTROY:
                ::PostQuitMessage(0);
                break;
        default:
                return ::DefWindowProc(hwnd,uMsg,wParam,lParam);
               

        }
        return 0;
}
回复

使用道具 举报

0

主题

10

帖子

9.00

积分

新手上路

Rank: 1

积分
9.00
发表于 2020-1-15 20:45:01 | 显示全部楼层
不明白你的问题,请详解。。。
回复

使用道具 举报

1

主题

3

帖子

4.00

积分

新手上路

Rank: 1

积分
4.00
 楼主| 发表于 2020-1-15 22:09:01 | 显示全部楼层
就是说我的本意在让程序在前台运行的,可编译后的程序是在后台运行的,本人反复看了代码?也没发现是哪的问题.
回复

使用道具 举报

0

主题

10

帖子

9.00

积分

新手上路

Rank: 1

积分
9.00
发表于 2020-1-30 12:54:02 | 显示全部楼层
你用鼠标把它点到前台不久好了,
SetForegroundWindow(hWnd); 把你的窗口设置到前台
回复

使用道具 举报

1

主题

3

帖子

4.00

积分

新手上路

Rank: 1

积分
4.00
 楼主| 发表于 2020-4-22 15:00:01 | 显示全部楼层
如果不是采用类封装m_hWnd这个名柄,是可以示一个窗口的,关键是我想采用类来封装m_hWnd句柄是不是在函数中引用该类有失误之处,望高人指点.
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

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