(c++ win32) 尽管它正在运行,但窗口没有显示 [英] (c++ win32) Window not showing up despite it running

查看:51
本文介绍了(c++ win32) 尽管它正在运行,但窗口没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对按钮进行子类化,但我的窗口没有显示,我每次都必须重新启动 Visual Studio 才能运行该应用程序.目前什么都没有显示:当我运行应用程序时它什么也没显示.我认为这与函数RegisterClass1有关.请帮忙

I was trying to subclass a button but my window isn't showing up and I have to restart visual studio every time to run the application. At the moment nothing shows up : it just shows nothing when I run the application. I think it's something to do with the function RegisterClass1.Please help

这是我的代码:

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

#include "framework.h"
#include "CustomButton.h"
#include "SkinClass/skin.h"
#include <commctrl.h>
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
const char g_szClassName[] = "MyClassName";

// 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);
void                RegisterClass1(HINSTANCE);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_CUSTOMBUTTON, szWindowClass, MAX_LOADSTRING);
    //MyRegisterClass(hInstance);
    RegisterClass1(hInstance);










    // Perform application initialization:
    /*if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }*/
    /*HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);*/

    HWND hWnd = CreateWindowExA(WS_EX_LAYERED, g_szClassName, "Scenes", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
        hInstance, NULL);



    CSkin aa(IDR_CR, ID_CIRCLE);
    RECT rect;
    GetWindowRect(hWnd, &rect);

    HWND hButton = CreateWindow(TEXT("BUTTON"), TEXT("START EDITING!"), WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, ((rect.right - rect.left) / 2) - 90, 500, aa.Width(), aa.Height(),
        hWnd, (HMENU)NULL, NULL, NULL);




    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CUSTOMBUTTON));





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










    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    aa.Destroy();
    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW 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_CUSTOMBUTTON));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_CUSTOMBUTTON);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: RegisterClass1()
//
//   PURPOSE: Registers the class
//
//   COMMENTS:
//
//
//
//

void RegisterClass1(HINSTANCE hInstance) {
    WNDCLASSEXA wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = g_szClassName;
    wc.lpszMenuName = "MENU";
    wc.style = CS_HREDRAW | CS_VREDRAW;


}

//
//   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)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

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

   return TRUE;
}

LRESULT CALLBACK ButtonProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (Msg)
    {
    case WM_PAINT: {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        HDC hdc_x = CreateCompatibleDC(0);
        HINSTANCE hInstance = GetModuleHandle(NULL);
        HBITMAP hBitmap = (HBITMAP)LoadBitmap(hInstance, MAKEINTRESOURCE(ID_CIRCLE)); //Load the bitmap
        HBITMAP holdbmp;
        holdbmp=(HBITMAP)SelectObject(hdc_x, hBitmap); //Put the bitmap into the hdc_x
        SelectObject(hdc_x, holdbmp);
        BitBlt(hdc, 0, 0, 180, 50, hdc_x, 0, 0, SRCCOPY); //Draw it.
        EndPaint(hWnd, &ps);
        DeleteObject(hBitmap);
        DeleteDC(hdc_x);
        break;
    }

    default:
        return DefSubclassProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}
















//
//  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)
{
    switch (message)
    {
    case WM_CREATE:

    case WM_COMMAND:
        {
            int wmId = LOWORD(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:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc 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;
}

如果我不重新启动笔记本电脑,visual studio 会给我以下消息:错误 LNK1168 无法打开 C:\Users\Rubel\source\repos\CustomButton\Debug\CustomButton.exe 来编写 CustomButton C:\Users\Rubel\source\repos\CustomButton\CustomButton\LINK 1

If I don't restart the laptop, visual studio gives me this message : Error LNK1168 cannot open C:\Users\Rubel\source\repos\CustomButton\Debug\CustomButton.exe for writing CustomButton C:\Users\Rubel\source\repos\CustomButton\CustomButton\LINK 1

推荐答案

基于分层窗口":

调用CreateWindowEx后,分层窗口不会变成可见,直到 SetLayeredWindowAttributesUpdateLayeredWindow 函数已为此窗口调用.

After the CreateWindowEx call, the layered window will not become visible until the SetLayeredWindowAttributes or UpdateLayeredWindow function has been called for this window.

以下是使分层窗口可见(显示)的示例:

The following is an example to make the layered window visible (show up):

   HWND hWnd = CreateWindowExA(WS_EX_LAYERED, g_szClassName, "Scenes", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
       hInstance, NULL);

   SetLayeredWindowAttributes(hWnd, NULL, 255, LWA_ALPHA);

   if (!hWnd)
   {
      return FALSE;
   }

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

更新:

此问题的另一个原因是 RegisterClassExW 未调用(在 RegisterClass1 中),这将导致 CreateWindow 失败并返回 NULL 窗口句柄.所以错误检查非常重要.

Another cause for this issue is RegisterClassExW is not called (in RegisterClass1), this will cause CreateWindow fails with returning NULL window handle. So error checking is very important.

这篇关于(c++ win32) 尽管它正在运行,但窗口没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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