| |

VerySource

 Forgot password?
 Register
Search
View: 663|Reply: 4

The program runs in the background, enlighten me!

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-11 02:00:01
| Show all posts |Read mode
Why does the program run in the background? I can't figure it out. I hope my predecessors can enlighten me. Don't think the problem is simple. I just started learning 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, "Do you really want to exit?", "Warning", MB_YESNO))
{
:: DestroyWindow (hwnd);
}
Ranch
break;
case WM_DESTROY:
:: PostQuitMessage (0);
break;
default:
return :: DefWindowProc (hwnd, uMsg, wParam, lParam);
Ranch

}
return 0;
}
Reply

Use magic Report

0

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-15 20:45:01
| Show all posts
Don't understand your problem, please explain in detail. . .
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-15 22:09:01
| Show all posts
That is to say, my intention is to let the program run in the foreground, and the compiled program runs in the background. I have repeatedly looked at the code? I haven't found what the problem is.
Reply

Use magic Report

0

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 United States

Post time: 2020-1-30 12:54:02
| Show all posts
Soon you point it to the front desk with your mouse.
SetForegroundWindow (hWnd); Set your window to the foreground
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-22 15:00:01
| Show all posts
If you don't use the class to encapsulate the m_hWnd name handle, you can show a window.The key is that I want to use a class to encapsulate the m_hWnd handle.Is there a mistake in referring to this class in the function?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list