Skip to content

Instantly share code, notes, and snippets.

@raa0121
Created May 25, 2023 15:24
Show Gist options
  • Save raa0121/047191731d0b23057d870de0ae623f4e to your computer and use it in GitHub Desktop.
Save raa0121/047191731d0b23057d870de0ae623f4e to your computer and use it in GitHub Desktop.
# 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,24 @@ did_set_textwidth(char *errmsg)
return errmsg;
}
+#if defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
+/*
+ * Process the new 'transparency' option value.
+ */
+ char *
+did_set_transparency(optset_T *args UNUSED)
+{
+ if (p_transparency < 1 || p_transparency > 255)
+ p_transparency = 255;
+# ifdef VIMDLL
+ if (gui.in_use)
+# endif
+ gui_mch_set_transparency(p_transparency);
+ return NULL;
+}
+#endif
+
+
/*
* When some number options are changed, need to take some action.
*/
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, did_set_transparency,
+ {(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/proto/optionstr.pro b/src/proto/optionstr.pro
--- a/src/proto/optionstr.pro
+++ b/src/proto/optionstr.pro
@@ -108,6 +108,7 @@ char *did_set_termwintype(optset_T *args);
char *did_set_titlestring(optset_T *args);
char *did_set_toolbar(optset_T *args);
char *did_set_toolbariconsize(optset_T *args);
+char *did_set_transparency(optset_T *args);
char *did_set_ttymouse(optset_T *args);
char *did_set_varsofttabstop(optset_T *args);
char *did_set_vartabstop(optset_T *args);
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