Author Topic: Lost and Confused  (Read 5253 times)

0 Members and 1 Guest are viewing this topic.

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 595
  • User-Rate: +11/-8
Lost and Confused
« on: March 22, 2016, 07:21:15 PM »
OK this one has me baffeled.

The window shown when the app runs is the same and correct when the c++ source is compiled
with TDM-GCC-64 or NUWEN. It is also the same when compiled as a 64 bit "c" app with PellesC8.
It is not correct when compiled with VS 2015 community.

If I use Borje Hagsten's PbWinSpy on the windows it creates exactly the same code for both
windows.?

In the attachment I include both exe's, the screen captures shown here, the c++ and bc9Basic sources.
I compile from the command line and have included both the TDM and VS batch files.

James


Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Lost and Confused
« Reply #1 on: March 23, 2016, 06:41:00 PM »
I'm not exactly sure what the issue is James.  I looked at all the jpgs and red text is showing up on a white background in tdm and vs.  Likewise with green on black.  Just what exactly is wrong?

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 595
  • User-Rate: +11/-8
Re: Lost and Confused
« Reply #2 on: March 23, 2016, 07:34:01 PM »
Fred,
  The displayed width of the window. No space between the edit fields and the window border on the vs 2015 window.

Here is a discussion I just started on the codeguru forum:
http://forums.codeguru.com/showthread.php?555181-VS-2015-display-issue

James
« Last Edit: March 23, 2016, 07:38:59 PM by James C. Fuller »

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Lost and Confused
« Reply #3 on: March 24, 2016, 12:03:11 AM »
Could that be a DPI phenomenon?  Different versions of user32.dll do seem to change things a bit.  I always have minor (sometimes major) clipping problems when moving from one OS to the next.  Don't think its changes to the window dimensions specified in the CreateWindowEx() call, so much as changes to the window's client area.

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Lost and Confused
« Reply #4 on: March 24, 2016, 02:34:24 AM »
I just compiled and ran ec02.cpp with VC15 (from Visual Studio 2008 - 64 bit) and it looked OK.  The edit controls didn't extend to the edge of the window, anyway.

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Lost and Confused
« Reply #5 on: March 25, 2016, 03:19:37 PM »
Say Jim,

     I just re-coded the app the way I'd typically do it and I used my my DPI code, and its displaying the exact same way in VC15 (Visual Studio 2008) and VC19 (Visual Studio 2015.  I wish they would get their numbers in some kind of synchronized order!!!!).

     The only thing I didn't add from your code was the capability to not allow multiple instances of the app.  I don't typically do that.  Also, I compiled it with TCLib and its coming in 6656 bytes (6100 VC15).  I'll attach everything, but here's ec02b.cpp

Code: [Select]
// cl ec02b.cpp /O1 /Os /MT kernel32.lib user32.lib gdi32.lib
// cl ec02b.cpp /O1 /Os /GS- /link TCLib.lib kernel32.lib user32.lib gdi32.lib
//  6,656 Bytes x64, UNICODE, VC19, TCLib
// 89,088 Bytes Std. VC Compile, MT Linked
// 90,112 Bytes BCX Translated C++ VC19 ansi MT Linked
#ifndef UNICODE
   #define UNICODE
#endif
#ifndef _UNICODE
   #define _UNICODE
#endif
#include <windows.h>
#include "stdio.h"
#include "tchar.h"
#include "Form1.h"


void SetMyProcessDpiAware()
{
 BOOL (__stdcall* pFn)(void);

 HINSTANCE hInstance=LoadLibrary(_T("user32.dll"));
 if(hInstance)
 {
    pFn=(BOOL (__stdcall*)(void))GetProcAddress(hInstance,"SetProcessDPIAware");
    if(pFn)
       pFn();
    FreeLibrary(hInstance);
 }
}


LRESULT fnWndProc_OnCreate(WndEventArgs& Wea)
{
 BOOL blnDpiAware=FALSE;
 double rxRatio,ryRatio;
 DpiData* pDpiData=NULL;
 HANDLE hHeap=NULL;
 HFONT hFont1=NULL;
 HFONT hFont2=NULL;
 int dpiX,dpiY;
 HDC hDC=NULL;
 HWND hCtl;
 
 Wea.hIns    = ((LPCREATESTRUCT)Wea.lParam)->hInstance;
 hHeap       = GetProcessHeap();
 blnDpiAware = IsProcessDPIAware();
 
 // Deal With DPI
 hDC      = GetDC(NULL);
 dpiX     = GetDeviceCaps(hDC, LOGPIXELSX);
 dpiY     = GetDeviceCaps(hDC, LOGPIXELSY);
 rxRatio  = (dpiX/96);
 ryRatio  = (dpiY/96);
 pDpiData = (DpiData*)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(DpiData));
 if(!pDpiData)
    return -1;
 SetWindowLongPtr(Wea.hWnd,0*sizeof(void*),(LONG_PTR)pDpiData);
 pDpiData->rx=rxRatio, pDpiData->ry=ryRatio;
 ReleaseDC(Wea.hWnd,hDC);
 
 // Create Child Window Objects
 hCtl=CreateWindow(_T("edit"),NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOVSCROLL|ES_WANTRETURN|ES_AUTOHSCROLL|ES_MULTILINE,SizX(15),SizY(15),SizX(295),SizY(112),Wea.hWnd,(HMENU)IDC_EDIT1,Wea.hIns,NULL);
 hFont1=CreateFont(-1*(10*dpiY)/72,0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,_T("Arial"));
 SetWindowLongPtr(Wea.hWnd,1*sizeof(void*),(LONG_PTR)hFont1);
 SendMessage(hCtl, (UINT)WM_SETFONT, (WPARAM)hFont1, (LPARAM)TRUE);
 SetWindowText(hCtl, _T(" Text in this control set to RED ON WHITE"));
 
 hCtl=CreateWindow(_T("edit"),NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOVSCROLL|ES_WANTRETURN|ES_AUTOHSCROLL|ES_MULTILINE,SizX(15),SizY(160),SizX(295),SizY(112),Wea.hWnd,(HMENU)IDC_EDIT2,Wea.hIns,NULL);
 hFont2=CreateFont(-1*(10*dpiY)/72,0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,OUT_TT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,_T("Garamond"));
 SetWindowLongPtr(Wea.hWnd,2*sizeof(void*),(LONG_PTR)hFont2);
 SendMessage(hCtl, (UINT)WM_SETFONT, (WPARAM)hFont2, (LPARAM)TRUE);
 SetWindowText(hCtl, _T(" Text in this control set to Green ON BLACK"));
 
 return 0;
}


LRESULT fnWndProc_OnCtlColorEdit(WndEventArgs& Wea)
{
 int iCtlId=GetDlgCtrlID((HWND)Wea.lParam);
 
 if(iCtlId==IDC_EDIT1)
 {
    SetTextColor((HDC)Wea.wParam, RGB(255, 0, 0));
    SetBkMode((HDC)Wea.wParam,TRANSPARENT);
    return (LRESULT)GetStockObject(WHITE_BRUSH);
 }
 if(iCtlId==IDC_EDIT2)
 {
    SetTextColor((HDC)Wea.wParam, RGB(0, 255, 0));
    SetBkMode((HDC)Wea.wParam,TRANSPARENT);
    return (LRESULT)GetStockObject(BLACK_BRUSH);
 }  
 
 return DefWindowProc(Wea.hWnd, WM_CTLCOLOREDIT, Wea.wParam, Wea.lParam);
}


LRESULT fnWndProc_OnDestroy(WndEventArgs& Wea)
{
 DpiData* pDpiData=NULL;
 HFONT hFont=NULL;
 
 pDpiData=(DpiData*)GetWindowLongPtr(Wea.hWnd,0*sizeof(void*));
 HeapFree(GetProcessHeap(),0,pDpiData);
 hFont=(HFONT)GetWindowLongPtr(Wea.hWnd,1*sizeof(void*));
 DeleteObject(hFont);
 hFont=(HFONT)GetWindowLongPtr(Wea.hWnd,2*sizeof(void*));
 DeleteObject(hFont);
 PostQuitMessage(0);
 
 return 0;
}


LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 WndEventArgs Wea;

 for(unsigned int i=0; i<dim(EventHandler); i++)
 {
     if(EventHandler[i].iMsg==msg)
     {
        Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
        return (*EventHandler[i].fnPtr)(Wea);
     }
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[]=_T("Form1");
 MSG messages;
 WNDCLASS wc;
 HWND hWnd;

 memset(&wc,0,sizeof(WNDCLASS));
 SetMyProcessDpiAware();
 wc.lpszClassName = szClassName,                     wc.lpfnWndProc   = fnWndProc;
 wc.hIcon         = LoadIcon(NULL,IDI_APPLICATION),  wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
 wc.hInstance     = hIns,                            wc.hCursor       = LoadCursor(NULL,IDC_ARROW),         
 wc.cbWndExtra    = 3*sizeof(void*);
 RegisterClass(&wc);
 hWnd=CreateWindow(szClassName,_T("Edit Ctrl Colors and Fonts"),WS_OVERLAPPEDWINDOW,75,75,338,320,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return messages.wParam;
}

Form1.h
Code: [Select]
//Form1.h
#ifndef Form_h
#define Form_h

#define IDC_EDIT1                 2000
#define IDC_EDIT2                 2005
#define dim(x)                    (sizeof(x) / sizeof(x[0]))
#define SizX(x)                   (int)(x * pDpiData->rx)       // For DPI Scaling Calculations And Purposes
#define SizY(y)                   (int)(y * pDpiData->ry)       // For DPI Scaling Calculations And Purposes
extern "C" int                    _fltused=1;
 
struct WndEventArgs
{
 HWND                             hWnd;
 WPARAM                           wParam;
 LPARAM                           lParam;
 HINSTANCE                        hIns;
};

LRESULT fnWndProc_OnCreate        (WndEventArgs& Wea);
LRESULT fnWndProc_OnCtlColorEdit  (WndEventArgs& Wea);
LRESULT fnWndProc_OnDestroy       (WndEventArgs& Wea);

struct EVENTHANDLER
{
 unsigned int                     iMsg;
 LRESULT                          (*fnPtr)(WndEventArgs&);
};

const EVENTHANDLER EventHandler[]=
{
 {WM_CREATE,                      fnWndProc_OnCreate},
 {WM_CTLCOLOREDIT,                fnWndProc_OnCtlColorEdit},
 {WM_DESTROY,                     fnWndProc_OnDestroy}
};

struct DpiData                   
{                                 
 double                           rx;
 double                           ry;
};

#endif

 
« Last Edit: March 25, 2016, 03:34:14 PM by Frederick J. Harris »

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Lost and Confused
« Reply #6 on: March 25, 2016, 03:31:03 PM »
Here's the zip James.

Added later:

Be careful if you compile it as I see above I used "tchar.h" instead of <tchar.h> when compiling with TCLib.lib for the reasons described in my writeup on TCLib.  So you would want to change that if doing a standard build without TCLib.  And "stdio.h" isn't needed except for my debug build with fprintf calls, which I didn't post.

Just had a little bit of a 'runin' with my WinMain declaration.  Don't know why it was acting up on me.  Bedeviled me for about ten minutes very seriously.  Anyway, if it gives you troubles try this...

Code: [Select]
int WINAPI _tWinMain(HINSTANCE hIns, HINSTANCE hPrev, LPTSTR lpCmdLn, int iShow)
« Last Edit: March 25, 2016, 05:07:06 PM by Frederick J. Harris »

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Lost and Confused
« Reply #7 on: March 25, 2016, 05:14:18 PM »
Haven't tried it yet with GCC.  You turned me on to that years ago (the 64 bit TDM versions) and they became my main compilers which I mostly used with Code::Blocks IDE.  Easier to set up projects with CodeBlocks, not so many crazy folders created as Visual Studio, etc.  I'll try it later and report back, or maybe you could.  I really expect it'll be the same and work though.  Thing is, the versions of GCC I was using didn't have support for the SetProcessDPIAware() or IsProcessDPIAware() functions.  That's why I have that SetMyProcessDPIAware() function.  To differentiate between GCC and VS builds.  Maybe they added DPI support.  The latest one I have on any of my machines is GCC 4.8.  I think they're up to 5.1 or 5.2 by now.

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 595
  • User-Rate: +11/-8
Re: Lost and Confused
« Reply #8 on: March 25, 2016, 05:47:46 PM »
Fred,
  Thank you for your effort but it's just way too complicated for my purpose.
Attached is the example I posted to the CodeGuru forum. Much simpler but still the same issue.

The g++ I used is the TDM-GCC compiler from here: http://tdm-gcc.tdragon.net/

The other most up to date is the nuwen distro 64bit only
http://nuwen.net/mingw.html

James
« Last Edit: March 25, 2016, 05:51:41 PM by James C. Fuller »