如何使用WinAPI创建新窗口? [英] How to create new window with WinAPI?

查看:96
本文介绍了如何使用WinAPI创建新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.代码:

Hi. The code:

// Window.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam);
 
HINSTANCE hInst;
 
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hWnd;
    MSG msg;
    
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "Base";
    wc.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
    
    if (!RegisterClassEx(&wc))
        return 0;
    if (!(hWnd = CreateWindowEx (NULL, "Base", "Програма керування локальними групами користувачів", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, 425, 300, NULL, NULL, hInst, NULL)))
        return (0);
    
    ShowWindow(hWnd, SW_SHOWDEFAULT);
    UpdateWindow(hWnd);
    
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hButton, hListBox, hComboBox, hWindow1;
    
    switch(message)
    {
    case WM_CREATE:
        {
            hButton = CreateWindow ("BUTTON", "Створення локальної групи", WS_CHILD | WS_VISIBLE,
                                    33, 0, 350, 20, hWnd, (HMENU)100, hInst, 0);
			hButton = CreateWindow ("BUTTON", "Визначення інформації про локальну групу", WS_CHILD | WS_VISIBLE,
                                    33, 50, 350, 20, hWnd, (HMENU)100, hInst, 0);
			hButton = CreateWindow ("BUTTON", "Перерахування створених локальних груп", WS_CHILD | WS_VISIBLE,
                                    33, 100, 350, 20, hWnd, (HMENU)100, hInst, 0);
			hButton = CreateWindow ("BUTTON", "Зміна інформації про групу", WS_CHILD | WS_VISIBLE,
                                    33, 150, 350, 20, hWnd, (HMENU)100, hInst, 0);
			hButton = CreateWindow ("BUTTON", "Видалення локальної групи", WS_CHILD | WS_VISIBLE,
                                    33, 200, 350, 20, hWnd, (HMENU)100, hInst, 0);
        }
        break;
    
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case 100:
            {
                //MessageBox (hWnd, "Button", "Другая форма", 0);
            }
        }
        break;
        
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return (0);
            }
        break;
    }
    return (DefWindowProc(hWnd, message, wParam, lParam));
}



那么,如何使用按钮和Editbox等特殊内容创建新窗口?以及如何从该编辑框中获取信息以进行一些计算.我需要单击按钮后,出现新窗口.


P.S .:我有一个新窗口,但是:
1.它类似于main(或parent).
2.如果我关闭它,则所有程序都在关闭.



So, how to create new window with special stuff like buttons and editbox? And how to take the information from that editbox, for some calculation. I need that after clicking the button, new window appears.


P.S.: I get a new window, but:
1. It''s similar to main(or parent).
2. If I close it, all program is closing.

hWindow1 = CreateWindowEx (NULL, "Base", "Програма керування локальними групами користувачів", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    CW_USEDEFAULT, CW_USEDEFAULT, 425, 300, GetParent(hWnd), NULL, hInst, NULL);


推荐答案

您可以在捕获事件的回调函数中使用新对象.也可以使用对话框资源.
请参阅此教程:
http://www.winprog.org/tutorial/modeless_dialogs.html [ http://msdn.microsoft.com/en-us/library/d06h2x6e.aspx [ ^ ]
You can use new objects in a callback function which catches events. Also a resources for dialogs can be used.
Please see this tutorial:
http://www.winprog.org/tutorial/modeless_dialogs.html[^]

The other way is to use MFC.
http://msdn.microsoft.com/en-us/library/d06h2x6e.aspx[^]


现在,该答案由Charles Petzold在他经典的 Programming Windows,第5版中给出.[ Windows应用程序UI开发 [ ^ ],它看起来像Jani Mäkinen写了一篇不错的文章,介绍如何在C中创建您的第一个Windows应用程序: Win32 Window Minimal [ ^ ]

认真地,尽管给自己买了一本Petzolds的书的副本-并不是很令人兴奋,但是他很彻底-为您提供了直接针对Windows API进行编程的基础知识.

Windows通过C/C ++ [ ^ ]也很值得一读.

[更新]
2.如果我关闭它,则所有程序都将关闭.
您需要为第二个窗口使用不同的WndProc,它们不会在WM_DESTROY上调用PostQuitMessage,因为这会导致消息循环终止.

最好的问候
Espen Harlinn
Now, that answer is given by Charles Petzold in his classic Programming Windows, 5th Edition[^]

Another nice source of information is Windows Application UI Development[^], and it looks like Jani Mäkinen wrote a nice article on how to create your first Windows application in C: Win32 Window Minimal[^]

Seriously, though, get yourself a copy of Petzolds'' book - it''s not terribly exciting, but he is thorough - providing you with a solid grounding on the basics of programming directly against the Windows API.

Windows via C/C++[^] by Jeffrey Richter and Christophe Nasarre is also well worth reading.

[Update]
2. If I close it, all program is closing.
You need a different WndProc for the second window that don''t call PostQuitMessage on WM_DESTROY as this causes the message loop to terminate.

Best regards
Espen Harlinn


这篇关于如何使用WinAPI创建新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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