Вопрос

I was trying to make a function createscalingwindow that calls CreateWindowEx and writes the address's of the HWND used in CreateWindowEx to a vector, followed by it's initial size and position values.

Another function I made called scalewindowsize was supposed to take two given windows and scale the size of one window with the other, but at the moment I can't even match certain HWND's addresses because under certain conditions the address of an HWND passed to scalewindowsize will be entirely different from the address when initialized. This only happens when I call scalewindowsize from a window procedure using the same HWND in the window procedure's parameter as the HWND used in the parameter for scalewindowsize. Below is the relevant code:

#include <windows.h>
#include <stdio.h>
#include <winsock.h>
#include <vector>
#define ID_1 1000
#define ID_2 1001
#define ID_3 1002
HWND mainwin;  
HWND lobbywin;
int zero = 0;
char MENU1;
std::vector<intptr_t> sizevalues;
LRESULT CALLBACK mainproc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK lobbyproc (HWND, UINT, WPARAM, LPARAM);
char mainclassname[] = "main";
char lobbyclassname[] = "lobby";

void createscalingwindow(HWND &cswpara0,DWORD cswpara1,const CHAR* cswpara2,
const CHAR* cswpara3,DWORD cswpara4,int cswpara5,int cswpara6,int cswpara7,
int cswpara8,HWND cswpara9,HMENU cswpara10,HINSTANCE cswpara11,LPVOID cswpara12)
{
  cswpara0 = CreateWindowEx (cswpara1, cswpara2, cswpara3, cswpara4,cswpara5,
  cswpara6,cswpara7,cswpara8,cswpara9,cswpara10,cswpara11,cswpara12);
  char buffer[100];
  sizevalues.push_back((intptr_t)&cswpara0);
  snprintf (buffer, 20,"%d", sizevalues[zero]);
  MessageBox (NULL, buffer, "initialized", NULL);
  zero = (zero + 1);
  return;
}         

void scalewindowsize (HWND &ownerwin, const CHAR* tryz)
{
  char buffer[100];
  int checknumber = 0; 
  while (checknumber < sizevalues.size())
  {
    int compare = sizevalues[checknumber];
    if (sizevalues[checknumber] == (intptr_t)&ownerwin)
    {
      snprintf (buffer, 100,"%d", (intptr_t)&ownerwin);
      MessageBox (NULL, buffer, "value found", NULL);
      break;
    }
    else 
    {
      snprintf (buffer, 100,"%d", (intptr_t)&ownerwin);
      MessageBox (NULL, buffer, tryz, NULL);
      snprintf (buffer, 100,"%d", sizevalues[checknumber]);
      checknumber = (checknumber + 1);
      MessageBox (NULL, buffer, "fail currentcheck", NULL);
    }
  }
  return;
}

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
LPSTR lpszArgument, int nCmdShow)
{
  MSG messages; 
  WNDCLASSEX mainwinclass;
  mainwinclass.hInstance = hThisInstance;
  mainwinclass.lpszClassName = mainclassname;
  mainwinclass.lpfnWndProc = mainproc;
  mainwinclass.style = CS_DBLCLKS;
  mainwinclass.cbSize = sizeof (WNDCLASSEX);
  mainwinclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  mainwinclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  mainwinclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  mainwinclass.lpszMenuName = NULL;
  mainwinclass.cbClsExtra = 0;
  mainwinclass.cbWndExtra = 0;
  mainwinclass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  if (!RegisterClassEx (&mainwinclass))
   return 0;

  WNDCLASSEX lobbywinclass;        
  lobbywinclass.hInstance = hThisInstance;
  lobbywinclass.lpszClassName = lobbyclassname;
  lobbywinclass.lpfnWndProc = lobbyproc;    
  lobbywinclass.style = CS_DBLCLKS;                
  lobbywinclass.cbSize = sizeof (WNDCLASSEX);
  lobbywinclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  lobbywinclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  lobbywinclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  lobbywinclass.lpszMenuName = NULL;                 
  lobbywinclass.cbClsExtra = 0;                     
  lobbywinclass.cbWndExtra = 0;                      
  lobbywinclass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;


  if (!RegisterClassEx (&lobbywinclass))
   return 0;
  HMENU playmenu = CreateMenu();
  HMENU playmenulist = CreatePopupMenu();
  HMENU helpmenulist = CreatePopupMenu();
  AppendMenu(playmenulist, MF_STRING, ID_1, "Join game");
  AppendMenu(playmenulist, MF_STRING, ID_3, "Host game");
  AppendMenu(playmenulist, MF_STRING, ID_2, "Quit");
  InsertMenu(playmenu, 1, MF_POPUP|MF_BYPOSITION, (UINT_PTR)playmenulist, "Play");

  createscalingwindow(mainwin, 0, mainclassname, "mainwindow",
  WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_SYSMENU, 
  CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, NULL, playmenu, hThisInstance, NULL);
  ShowWindow (mainwin, nCmdShow);

  createscalingwindow(lobbywin, 0, lobbyclassname, "Lobby", WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT, 600, 425, NULL, NULL, hThisInstance, NULL);

  while (GetMessage (&messages, NULL, 0, 0))
  {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
  }
  return messages.wParam;
}

LRESULT CALLBACK mainproc(HWND mainwin, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)         
  {
    case WM_SIZE:
    {
      scalewindowsize (mainwin, "mainwin");
      scalewindowsize (lobbywin, "lobbywin");
      break;
    }
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
  } 
  switch (wParam)
  {
    case ID_3:
    EnableWindow (mainwin, false);
    ShowWindow (mainwin, false);
    ShowWindow (lobbywin, true);              
    break;
    default:  
    return DefWindowProc (mainwin, message, wParam, lParam); 
    break;
  }  
  return 0;
}    
LRESULT CALLBACK lobbyproc(HWND lobbywin, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
    case WM_SIZE:
    {
      scalewindowsize (mainwin, "mainwin");
      scalewindowsize (lobbywin, "lobbywin");
      break;
    }
    break;
  }
  switch (wParam)
  {
    case SC_CLOSE:
    {
      EnableWindow (mainwin, true);
      ShowWindow (mainwin, true);
      ShowWindow (lobbywin, false);
      EnableWindow (lobbywin, false);
      break;
    }
    default:
    return DefWindowProc (lobbywin, message, wParam, lParam);
    break;
  }
}

If I run this program, click on host game, go back to the mainwindow and resize it slightly (setting off case WM_SIZE: in mainrpoc) then exit the program, I get the following messageboxes in chronological order:

1[title: Initialized |text: 4231184]
2[title: mainwin |text: 2292968]
3[title: fail currentcheck |text: 4231184]
4[title: lobbywin |text: 4231188]
5[title: fail currentcheck |text: 4231184]
6[title: initialized |text: 4231188] (no more boxes until I click on host game)
7[title: value found: |text: 4231184]
8[title: lobbywin |text: 2292676]
9[title: fail currentcheck |text: 4231184]
10[title: lobbywin |text: 2292676]
11[title: fail currentcheck |text: 4231188] (no more boxes until I go back to the mainwindow and resize it slightly)
12[title: mainwin |text: 2291164]
13[title: fail currentcheck |text: 4231184]
14[title: mainwin |text: 2291164]
15[title: fail currentcheck |text: 4231188]
16[title: lobbywin |text: 4231188]
17[title: fail currentcheck |text: 4231184]
18[title: value found |text: 4231188]

I'm very confused right now, I hope someone reading this can explain to me why the address is changing, thank you in advance for taking the time to read my question.

UPDATE: for anyone who's reading this I wanted to add in addition to whoz craigs answer that the way to call a global variable instead of a local in c++ is to add :: before the variable.

Это было полезно?

Решение

In both window procedures your sending the parameter HWND to scalewindow; not the global. Thus they're different addresses. Name resolution uses the "closest" variable, which in both cases is the HWND param, not the HWND global you're trying to use.

Example:

LRESULT CALLBACK mainproc(HWND mainwin, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)         
  {
    case WM_SIZE:
    {
      scalewindowsize (mainwin, "mainwin");   // passes parameter mainwin by reference
      scalewindowsize (lobbywin, "lobbywin"); // passes global lobbywin by reference
      break;
    }

Thus the references will have different address resolution. Another example:

LRESULT CALLBACK lobbyproc(HWND lobbywin, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
    case WM_SIZE:
    {
      scalewindowsize (mainwin, "mainwin");   // passes global mainwin by reference
      scalewindowsize (lobbywin, "lobbywin"); // passes parameter lobbywin by reference
      break;
    }
    break;

Другие советы

While WhozCraig already answered the question you asked, I'll explain how to get what you really wanted. It all boils down to your decision to store the address of a HWND being the wrong decision. Simply store the HWND - it is already pointer-sized so you're not wasting any space or performing costly copy operations.

Your container should be:

std::vector<HWND> sizevalues;

Change your createscalingwindow function to this (pseudo code):

void createscalingwindow( ... )
{
    cswpara0 = CreateWindowEx( .... );
    sizevalues.push_back(cswpara0);
    ...
}

And your scalewindowsize function should be modified as follows (again, pseudo code, with only the parts that require changes):

void scalewindowsize(HWND ownerwin, ...)
{
    char buffer[100];
    int checknumber = 0;
    while (checknumber < sizevalues.size())
    {
        if (sizevalues[checknumber] == ownerwin)
        {
            ...
        }
        else 
        {
            ...
        }
    }
}

This should fix your problems.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top