C++ WinApi-循环停止我的程序

C++ WinApi-循环停止我的程序,c++,winapi,loops,visual-studio-2012,C++,Winapi,Loops,Visual Studio 2012,代码: 这最初是一个控制台应用程序,但后来我使用这个模板,只是把我的代码。。。我的循环应该打印文本,但它不是。。。这不是夏娃做的窗户。请帮忙 这最初是一个控制台应用程序,但后来我使用了这个模板 然后把我的代码放在 我希望事情能这么简单:) 你不能那么容易地从控制台编程切换到事件驱动编程。也许你应该通过 我的循环应该打印文本,但它不是 在winapi中,我们通常用来显示文本,或者您可以更改的文本,甚至弹出一个。这通常是对特定事件的响应,例如按下按钮 这不是夏娃做的窗户 这是因为在调用InitIns

代码:

这最初是一个控制台应用程序,但后来我使用这个模板,只是把我的代码。。。我的循环应该打印文本,但它不是。。。这不是夏娃做的窗户。请帮忙

这最初是一个控制台应用程序,但后来我使用了这个模板 然后把我的代码放在

我希望事情能这么简单:)

你不能那么容易地从控制台编程切换到事件驱动编程。也许你应该通过

我的循环应该打印文本,但它不是

winapi
中,我们通常用来显示文本,或者您可以更改的文本,甚至弹出一个。这通常是对特定事件的响应,例如按下按钮

这不是夏娃做的窗户

这是因为在调用
InitInstance(…)
之前,有一个无限
while
循环

您的条件
while(getForeGroundIndow()==getForeGroundIndow())
始终为true
,因此您永远不会离开
while
循环,从而永远不会到达创建窗口的代码(
InitInstance(…)

请帮忙

读完我上面推荐的教程后,您将能够正确地重新编写应用程序

我不太了解你想做什么,但我会尽力给你一些建议:

将初始化代码放入处理程序:

在您的中,您应该放置清理代码(请注意,有时不会被调用!):

你的
GDI()
应该是一个事件的一部分,也许是一个按钮按下-我不知道这完全取决于你。这同样适用于
SR()
。它们将在处理程序中处理

我不知道
DirectX
,所以我不知道把你的
DirX\u Screen()
放在哪里,但你可以把花哨的图形放在最后,先专注于功能

希望这有点帮助

我强烈建议您阅读该教程并学习阅读
MSDN
文档


祝你好运

while(getforegroughindow()==getforegroughindow()
??所有的大写字母…它会痛…而不是
sususu()//启动
,就像,
StartUp()
。或者
StartUp\u-Up()
,我更喜欢。考虑一下调用代码的可读性。它读起来像英语中的
sususu
吗?
start\u
怎么样?
#include "CST CAL.h"
int SUSU()//Start Up
{
    WSADATA WSAData;
    if (WSAStartup(MAKEWORD(2,2), &WSAData) != 0)
    {
        printf("WSASTATUP Falure: ", WSAGetLastError);
        Sleep(1200);
        return 1;
    }

    return 0;
}

int SR()//Send and Recive Data
{
    return 0;
}
int DirX_Screen()//Render and Buffrer stuff and put on screen
{
    return 0;
}
int SHUT()//ShutDown Program
{
    return 0;
}
int GDI()//Get User Data Imput
{
    return 0;
}

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    //HERE IS CODE
        if (SUSU() != 0)
    {
        return 1;
    }int C = 0;
    do{while(GetForegroundWindow() == GetForegroundWindow()){
        int GeDI = GDI();
        int SeR = SR();
        int DirX = DirX_Screen();
        if( GeDI != 0 || SeR != 0 || DirX != 0){C = 1;}else{printf("FR Suc\n");}
        Sleep(0);
    }}while (C == 0);
    SHUT();
    //HERE IS END OF CODE SPACE
    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WIN32PROJECT1);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
case WM_CREATE:
    {
        if( SUSU() != 0 )
            return (LRESULT)-1;
        else
            return (LRESULT)0;
    }
case WM_DESTROY:
    {
        SHUT();
    }