关于winforms中的例程的C ++问题 [英] C++ question about routine in winforms

查看:154
本文介绍了关于winforms中的例程的C ++问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我不知道在哪里使用winforms在C ++中启动我的代码。没有main()过程,而是有一个WinMain()过程,所以在哪里准确地放置我的代码而不是。循环在哪里?

我要添加;

while(1){...调用另一种方法;}



Hallo, i am not sure where to start my code in C++ using winforms. There is no main() procedure, instead there is a WinMain() procedure, so where exactly to put my code and where not. Where is the loop?
I want to add;
while(1) {... call another method;}

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)


HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
//......
//......
/* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;
//.....
hwnd = CreateWindowEx (.....)

//.....
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}







LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}





我的尝试:



-------------------------------------------- ------------



What I have tried:

--------------------------------------------------------

推荐答案

Winforms不像一个控制台应用程序,有一个主进程,但它不在哪里你放了大部分代码(如果有的话)。你的意思是没有一个循环!



相反,它处理Windows消息,并在收到特定消息时调用适当的函数 - 并且它坐在循环中永远获取消息并处理它们 - 就是这样。应用程序的实际工作是在为响应收到的消息而调用的函数中完成的(大多数是由用户生成的,因此您主要是对用户发起的事件做出反应)此类事件包括鼠标移动,鼠标按钮正在点击,按下或释放键盘键。



不要只是深入研究这些东西:阅读你的书或课程笔记 - 或者如果你没有书或课程,得到一个 - 它将解释你在哪里做什么。 Windows编码与传统程序代码不同,你要求用户做什么:相反,你对用户所做的事情作出反应,这比你想象的要大得多!
Winforms isn't like a console app, there is a "main" proc, but it's not where you put most of your code (if any). And there isn't a loop in the sense you mean!

Instead it's job it to process Windows messages, and call the appropriate function when a particular message is received - and it "sits" in a loop forever getting messages and processing them - that is all it does. The actual work of you application is done in the functions that are called in response to messages being received (most of them generated by the user, so you are mostly reacting to user initiated events) Such events include the mouse moving, a mouse button being clicked, a keyboard key being pressed or released.

Don't just dive into this stuff: read your book or course notes - or if you don't have a book or course, get one - and it will explain what you do where. Windows coding is not the same as "traditional" procedural code where you ask the user what to do: instead you react to what the user does which is a much bigger difference than you might think!


请参阅 EFNet #Winprog [ ^ ]和 Win32编程 - 功能X [ ^ ]了解一些有用的教程。
See EFNet #Winprog[^] and Win32 Programming - FunctionX[^] for some useful tutorials.


我结束了创建一些事件,我不确定是否是在C ++中使用的方式,但对某些测试很有用。

I ended up creating some events, im not sure if is the way to use in C++ but good for some testing.
bool onLoad;
void on_Load();

bool onClosing;
void on_Closing();


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if(onLoad == false)
    {
        onLoad = true;
        on_Load();
    }

    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            onClosing = true;
            on_Closing();
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


void on_Load()
{
    //FORMS HAS LOADED
    //do stuff
   ofstream myFile;
		myFile.open("C:\\Loading.txt", ios::out | ios::app);
		myFile << "Loading!";
		myFile.close();

}

void on_Closing()
{
    //FORM IS CLOSING
    //do stuff
    ofstream myFile;
		myFile.open("C:\\Closing.txt", ios::out | ios::app);
		myFile << "Closing!";
		myFile.close();
}


这篇关于关于winforms中的例程的C ++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆