touchscript layers and user32.dll window pos/offset/resize

Random discussions.

touchscript layers and user32.dll window pos/offset/resize

Postby bomanden » Mon Nov 27, 2017 3:53 pm

Hi
I have an application I would like to change resolution to, and stretch across two monitors,
When I do so width (see script below):
Code: Select all
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, uint uFlags);

The touch overlay get out sync with the new resolution,
I'm sure there is an easy fix to this, I can you disable / enable the canvas in your example scene Valentin , so guess I need to do something similar to the touchlayer(s).
I'm running unity 2017.2 , with the newest touchscript (asset store)

Try to import my quickly made script below, to have an idea of what I'm struggling with.
So basically updating the touch layers , to the new resolutions.

Couldn't attach the script , see below:
Code: Select all
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Linq;
using UnityEngine.UI;

public class ScreenResPosUtil : MonoBehaviour
{
   #region DLL Imports
   [SerializeField]
   private Canvas canvas;

   [SerializeField]
   private CanvasScaler canvasScaler;

   [SerializeField]
   private GameObject refTouchManager;



   [SerializeField]
   private Vector2 res1 = new Vector2(1920,1080);
   [SerializeField]
   private Vector2 res2 = new Vector2(2550,1440);

   private const string UnityWindowClassName = "UnityWndClass";


   const uint SWP_SHOWWINDOW = 0x0040;
   const int GWL_STYLE = -16;
   const int WS_BORDER = 1;

   const int HWND_TOPMOST = -1;
   const int HWND_NOTOPMOST = -2;
   const int HWND_TOP = 0;
   const int HWND_BOTTOM = 1;

   [DllImport("user32.dll", EntryPoint = "FindWindow")]
   public static extern IntPtr FindWindow(System.String className, System.String windowName);

   [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
   private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, uint uFlags);

   [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
   private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

   private void SetBorderPosAndSize(IntPtr hwnd, int x, int y, int resX, int resY)
   {
      SetWindowLong(hwnd, GWL_STYLE, WS_BORDER);
      SetWindowPos(hwnd, HWND_TOP, x, y, resX, resY, SWP_SHOWWINDOW);
   }

   [DllImport("kernel32.dll")]
   static extern uint GetCurrentThreadId();

   [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
   static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

   public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   static extern bool EnumThreadWindows(uint dwThreadId, EnumWindowsProc lpEnumFunc, IntPtr lParam);
   #endregion

   #region Private fields
   private static IntPtr windowHandle = IntPtr.Zero;
   private static IntPtr windowHandle2 = IntPtr.Zero;
   #endregion

   #region Monobehavior implementation
   /// <summary>
   /// Called when this component is initialized
   /// </summary>
   void Start()
   {
      uint threadId = GetCurrentThreadId();
      EnumThreadWindows(threadId, (hWnd, lParam) =>
      {
         var classText = new StringBuilder(UnityWindowClassName.Length + 1);
         GetClassName(hWnd, classText, classText.Capacity);
         if (classText.ToString() == UnityWindowClassName)
         {
            windowHandle = hWnd;
            return false;
         }
         return true;
      }, IntPtr.Zero);

      if (windowHandle != IntPtr.Zero)
      {
         SetNewResPos(0, 0, (int)res1.x, (int)res1.y);
      }
   }

   void SetNewResPos(int posX, int posY, int resX, int resY)
   {
      #if UNITY_STANDALONE_WIN
      if (windowHandle != IntPtr.Zero)
      {
         SetBorderPosAndSize(windowHandle, posX, posY, resX, resY);
         StartCoroutine(ResetCanvas());
      }
      #endif
   }

   void Update()
   {
      if (Input.GetKeyDown(KeyCode.UpArrow))
      {
         SetNewResPos(0, 0, (int)res1.x, (int)res1.y);
      }

      if (Input.GetKeyDown(KeyCode.DownArrow))
      {
         SetNewResPos(2560, 0, (int)res2.x, (int)res2.y);
      }


   }

   IEnumerator ResetCanvas()
   {
      canvas.enabled = false;
      //refTouchManager.SetActive(canvas.enabled);
      yield return new WaitForSeconds(1f);
      canvas.enabled = !canvas.enabled;
      //refTouchManager.SetActive(canvas.enabled);
   }

   #endregion
}
#endif
bomanden
 
Posts: 4
Joined: Mon Nov 27, 2017 3:36 pm

Re: touchscript layers and user32.dll window pos/offset/resi

Postby valyard » Mon Nov 27, 2017 11:08 pm

There's this http://touchscript.github.io/docs/html/ ... lution.htm
But I'm not sure that thus will help in your case. Can you pack the scene you are testing with in a sample project so I could test it on my machine?
valyard
Site Admin
 
Posts: 433
Joined: Mon Sep 08, 2014 11:57 pm

Re: touchscript layers and user32.dll window pos/offset/resi

Postby bomanden » Tue Nov 28, 2017 9:51 am

Hi
I attached a project and the build _bin. https://wetransfer.com/downloads/217fc64bf7c2cc553843c8e627e21c3a20171128064720/02c7199da751677b2481b56affa6039620171128064720/6d3563?utm_campaign=WT_email_tracking&utm_content=general&utm_medium=download_button&utm_source=notify_recipient_email" we transfer link

Instructions :
Change resolution with the btns on screen , or numpad 0 , 1, or 2. (see how the overlay get out of sync)
bomanden
 
Posts: 4
Joined: Mon Nov 27, 2017 3:36 pm

Re: touchscript layers and user32.dll window pos/offset/resi

Postby bomanden » Mon Dec 04, 2017 1:10 pm

Hi
did you have a change to look at the example code I sent ?
If the wetransfer link is not valid ( valid for 7 days ) , I uploaded another
https://wetransfer.com/downloads/3f9068 ... 712/87de9b
bomanden
 
Posts: 4
Joined: Mon Nov 27, 2017 3:36 pm

Re: touchscript layers and user32.dll window pos/offset/resi

Postby valyard » Mon Dec 04, 2017 6:29 pm

I downloaded the project but haven't looked at it because I assumed I'd need 2 displays for this.
Do I need 2 displays or I can test with one? Sorry for the confusion.
valyard
Site Admin
 
Posts: 433
Joined: Mon Sep 08, 2014 11:57 pm

Re: touchscript layers and user32.dll window pos/offset/resi

Postby bomanden » Sun Dec 10, 2017 1:17 pm

Hi Valyard

yes you are right switches to the 2. screen, but you can just change line 102 - 116 in 'ScreenResPosUtil.cs'
Please change it and try it out, let me know if you any ideas on how to fix it / reset / update the touchscript settings.

Code: Select all
   public void ChangeResTest(int resIndex)
   {
      switch (resIndex)
      {
         case 0:
            SetNewResPos(0, 0, 800, 600);
            break;
         case 1:
            SetNewResPos(300, 100, 1280, 720);
            break;
         case 2:
            SetNewResPos(1920, 0, 3640, 2160);
            break;
      }
   }
bomanden
 
Posts: 4
Joined: Mon Nov 27, 2017 3:36 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest