WinApi - 循环停止我的程序

时间:2014-03-04 05:33:45

标签: c++ winapi loops visual-studio-2012

代码:

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

这本来是一个控制台应用程序,但后来我使用这个模板,只是放置了我的代码......我的循环应该打印出文本,但它不是......它不会使窗口成为现实。请帮助!

1 个答案:

答案 0 :(得分:1)

  

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

我希望事情变得如此简单:)

您无法轻松地从控制台编程切换到事件驱动的编程。也许你应该通过this tutorial

  

我的循环应该打印出来但不是......

winapi我们通常使用MessageBox来显示文字,或者您可以更改static control的文字,甚至可以弹出tooltip。这通常是以响应特定事件,例如按下按钮。

  它不会成为窗户。

这是因为您在致电while之前有无限InitInstance( ... )循环

您的条件while(GetForegroundWindow() == GetForegroundWindow()) 始终为真,因此您永远不会离开while循环,因此永远不会到达创建窗口的代码(InitInstance( ... ))。< / p>

  

请帮助!

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

我对你想要做的事情了解不多,但我会尽力为你提供一些建议:

将初始化代码放在WM_CREATE处理程序中:

case WM_CREATE:
    {
        if( SUSU() != 0 )
            return (LRESULT)-1;
        else
            return (LRESULT)0;
    }

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

case WM_DESTROY:
    {
        SHUT();
    }

您的GDI()应该是活动的一部分,也许是按下按钮 - 我不知道这完全取决于您。 SR()也是如此。它们将在WM_COMMAND处理程序中处理。

我不知道DirectX所以我不知道你的DirX_Screen()放在哪里,但你可以留下精美的图片,并专注于功能

希望这有点帮助。

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

祝你好运!