Skip to content

Instantly share code, notes, and snippets.

@k-takata
Last active February 10, 2023 06:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-takata/78f9b663bc82cd654b5b4be05b10831b to your computer and use it in GitHub Desktop.
Save k-takata/78f9b663bc82cd654b5b4be05b10831b to your computer and use it in GitHub Desktop.
Updated for 9.0.1294 and later
# HG changeset patch
# Parent fde93adeda8d91c54211d002010c4476c27e8dcc
# Parent 04bb907f937cefc1a013ccdaa2e54569944b57a8
implement 'transparency' for Windows GUI
diff --git a/src/gui_w32.c b/src/gui_w32.c
--- a/src/gui_w32.c
+++ b/src/gui_w32.c
@@ -50,6 +50,13 @@ static int gui_mswin_get_menu_height(int
# define gui_mswin_get_menu_height(fix_window) 0
#endif
+/*
+ * For Transparent Window.
+ */
+typedef DWORD (WINAPI *FWINLAYER)(HWND hwnd, DWORD crKey, BYTE bAlpha,
+ DWORD dwFlags);
+static void w32_set_transparency(HWND hwnd, BYTE bAlpha);
+
#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
int
gui_mch_set_rendering_options(char_u *s)
@@ -5358,6 +5365,8 @@ gui_mch_init(void)
if (s_hwnd == NULL)
return FAIL;
+ w32_set_transparency(s_hwnd, p_transparency);
+
if (pGetDpiForWindow != NULL)
{
s_dpi = pGetDpiForWindow(s_hwnd);
@@ -5658,6 +5667,44 @@ gui_mch_set_sp_color(guicolor_T color)
gui.currSpColor = color;
}
+ void
+w32_set_transparency(HWND hwnd, BYTE bAlpha)
+{
+ FWINLAYER pfLayer;
+ HANDLE hDll;
+
+ if (!hwnd)
+ hwnd = s_hwnd;
+
+ // Turn off transpareny
+ if (bAlpha == 255)
+ {
+ SetWindowLong(hwnd, GWL_EXSTYLE, ~WS_EX_LAYERED &
+ GetWindowLong(hwnd, GWL_EXSTYLE));
+ return;
+ }
+
+ // Obtain pointer to function set transparecy rate
+ if (!(hDll = LoadLibrary("user32.dll")))
+ return;
+ pfLayer = (FWINLAYER)GetProcAddress(hDll, "SetLayeredWindowAttributes");
+
+ if (pfLayer)
+ {
+ SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED |
+ GetWindowLong(hwnd, GWL_EXSTYLE));
+ pfLayer(hwnd, 0, bAlpha, LWA_ALPHA);
+ }
+
+ FreeLibrary(hDll);
+}
+
+ void
+gui_mch_set_transparency(int alpha)
+{
+ w32_set_transparency(NULL, (BYTE)alpha);
+}
+
#ifdef FEAT_MBYTE_IME
/*
* Multi-byte handling, originally by Sung-Hoon Baek.
diff --git a/src/option.c b/src/option.c
--- a/src/option.c
+++ b/src/option.c
@@ -4217,6 +4217,23 @@ did_set_textwidth(char *errmsg)
return errmsg;
}
+#if defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
+/*
+ * Process the new 'transparency' option value.
+ */
+ static void
+did_set_transparency(void)
+{
+ if (p_transparency < 1 || p_transparency > 255)
+ p_transparency = 255;
+# ifdef VIMDLL
+ if (gui.in_use)
+# endif
+ gui_mch_set_transparency(p_transparency);
+}
+#endif
+
+
/*
* When some number options are changed, need to take some action.
*/
@@ -4293,6 +4310,10 @@ did_set_num_option(long *pp, long value,
#endif
else if (pp == &curbuf->b_p_tw) // 'textwidth'
errmsg = did_set_textwidth(errmsg);
+#if defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
+ else if (pp == &p_transparency) // 'transparency'
+ did_set_transparency();
+#endif
return errmsg;
}
diff --git a/src/option.h b/src/option.h
--- a/src/option.h
+++ b/src/option.h
@@ -983,6 +983,7 @@ EXTERN long p_titlelen; // 'titlelen'
EXTERN char_u *p_titleold; // 'titleold'
EXTERN char_u *p_titlestring; // 'titlestring'
EXTERN char_u *p_tsr; // 'thesaurus'
+EXTERN long p_transparency; // 'transparency'
EXTERN int p_ttimeout; // 'ttimeout'
EXTERN long p_ttm; // 'ttimeoutlen'
EXTERN int p_tbi; // 'ttybuiltin'
diff --git a/src/optiondefs.h b/src/optiondefs.h
--- a/src/optiondefs.h
+++ b/src/optiondefs.h
@@ -2591,6 +2591,10 @@ static struct vimoption options[] =
{(char_u *)0L, (char_u *)0L}
#endif
SCTX_INIT},
+ {"transparency", "tra", P_NUM|P_VI_DEF,
+ (char_u *)&p_transparency, PV_NONE,
+ {(char_u *)255L, (char_u *)0L}
+ SCTX_INIT},
{"ttimeout", NULL, P_BOOL|P_VI_DEF|P_VIM,
(char_u *)&p_ttimeout, PV_NONE,
{(char_u *)FALSE, (char_u *)0L} SCTX_INIT},
diff --git a/src/proto/gui_w32.pro b/src/proto/gui_w32.pro
--- a/src/proto/gui_w32.pro
+++ b/src/proto/gui_w32.pro
@@ -71,6 +71,7 @@ void gui_mch_set_font(GuiFont font);
void gui_mch_set_fg_color(guicolor_T color);
void gui_mch_set_bg_color(guicolor_T color);
void gui_mch_set_sp_color(guicolor_T color);
+void gui_mch_set_transparency(int alpha);
void im_set_font(LOGFONTW *lf);
void im_set_position(int row, int col);
void im_set_active(int active);
diff --git a/src/testdir/gen_opt_test.vim b/src/testdir/gen_opt_test.vim
--- a/src/testdir/gen_opt_test.vim
+++ b/src/testdir/gen_opt_test.vim
@@ -148,6 +148,7 @@ let test_values = {
\ 'termwintype': [['', 'winpty', 'conpty'], ['xxx']],
\ 'toolbar': [['', 'icons', 'text'], ['xxx']],
\ 'toolbariconsize': [['', 'tiny', 'huge'], ['xxx']],
+ \ 'transparency': [['0', '128', '255'], ['xxx']],
\ 'ttymouse': [['', 'xterm'], ['xxx']],
\ 'ttytype': [[], []],
\ 'varsofttabstop': [['8', '4,8,16,32'], ['xxx', '-1', '4,-1,20']],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment