| 
 
| 赞 | 14 |  
| VIP | 0 |  
| 好人卡 | 0 |  
| 积分 | 23 |  
| 经验 | 18023 |  
| 最后登录 | 2025-5-31 |  
| 在线时间 | 328 小时 |  
 Lv3.寻梦者 
	梦石0 星屑2275 在线时间328 小时注册时间2012-5-8帖子99 
 | 
5楼
 
 
 楼主|
发表于 2019-10-28 02:35:03
|
只看该作者 
| 本帖最后由 condir 于 2019-10-28 06:57 编辑 
 14.将宋体15号字线性放大两倍作为像素字体
 效果图:
 
 ![]() 
 #加载DLLmodule OutsideFunc  Push_character =       Win32API.new('.\\System\\test.dll', 'set_16bit_char', 'p', 'v')  Get_character_pixel =       Win32API.new('.\\System\\test.dll', 'get_16bit_pixel', 'ii', 'i')  end #重写文本输出的部分class Window_Base < Window   include OutsideFunc   def line_height    return Font.default_size  end   def draw_text(*args)    #统一参数形式    if (args.size == 2||args.size == 3)      x = args[0].x      y = args[0].y      h = args[0].height      w = args[0].width      str = args[1]      align = (args[2] != nil ? args[2] : 0)    else      x = args[0]      y = args[1]      w = args[2]      h = args[3]      str = args[4]      align = (args[5] != nil ? args[5] : 0)    end    str = str.to_s unless str.is_a?(String)     #对齐格式:0-左 1-中 2-右    if (align == 1)      x = x + (w - text_size(str).width) / 2    elsif (align == 2)      x = x + w - text_size(str).width    end     #拆解为单个字符输出    for n in 0 ... str.size      c = str[n]      w = text_size(c).width      draw_one_character(x, y, w, h, c) unless c == nil      x += w    end  end   #输出单个字符:以点阵方式描绘  def draw_one_character(*args)    Push_character.call(args[4].encode("UTF-16LE"))    for x in 0 ... args[2] / 2 - 1      for y in 0 .. 14        if Get_character_pixel.call(x, y) == 0 then          px = args[0] + x * 2          py = args[1] + y * 2          contents.set_pixel(px, py, contents.font.color)          contents.set_pixel(px + 1, py, contents.font.color)          contents.set_pixel(px, py + 1, contents.font.color)          contents.set_pixel(px + 1, py + 1, contents.font.color)        end      end    end  endend
#加载DLL 
module OutsideFunc 
  Push_character =  
      Win32API.new('.\\System\\test.dll', 'set_16bit_char', 'p', 'v') 
  Get_character_pixel =  
      Win32API.new('.\\System\\test.dll', 'get_16bit_pixel', 'ii', 'i')   
end 
  
#重写文本输出的部分 
class Window_Base < Window 
  
  include OutsideFunc 
  
  def line_height 
    return Font.default_size 
  end 
  
  def draw_text(*args) 
    #统一参数形式 
    if (args.size == 2||args.size == 3) 
      x = args[0].x 
      y = args[0].y 
      h = args[0].height 
      w = args[0].width 
      str = args[1] 
      align = (args[2] != nil ? args[2] : 0) 
    else 
      x = args[0] 
      y = args[1] 
      w = args[2] 
      h = args[3] 
      str = args[4] 
      align = (args[5] != nil ? args[5] : 0) 
    end 
    str = str.to_s unless str.is_a?(String) 
  
    #对齐格式:0-左 1-中 2-右 
    if (align == 1) 
      x = x + (w - text_size(str).width) / 2 
    elsif (align == 2) 
      x = x + w - text_size(str).width 
    end 
  
    #拆解为单个字符输出 
    for n in 0 ... str.size 
      c = str[n] 
      w = text_size(c).width 
      draw_one_character(x, y, w, h, c) unless c == nil 
      x += w 
    end 
  end 
  
  #输出单个字符:以点阵方式描绘 
  def draw_one_character(*args) 
    Push_character.call(args[4].encode("UTF-16LE")) 
    for x in 0 ... args[2] / 2 - 1 
      for y in 0 .. 14 
        if Get_character_pixel.call(x, y) == 0 then 
          px = args[0] + x * 2 
          py = args[1] + y * 2 
          contents.set_pixel(px, py, contents.font.color) 
          contents.set_pixel(px + 1, py, contents.font.color) 
          contents.set_pixel(px, py + 1, contents.font.color) 
          contents.set_pixel(px + 1, py + 1, contents.font.color) 
        end 
      end 
    end 
  end 
end 
 再使用VS2019,建立一个DLL项目,
 设置静态编译(配置属性>C/C++>代码生成>运行库>多线程调试/MTd)
 
 // test.cpp : 定义 DLL 应用程序的导出函数。#include <windows.h>#include <stdio.h> int hasInit = FALSE;HFONT hfBuffer;        HDC hdcBuffer;HBITMAP hBitmapBuffer, hOldBitmapBuffer; //第五版:测试版字体放大程序 void init() {        //获取缓冲区句柄        hdcBuffer = CreateCompatibleDC(NULL);        //获取缓冲区画刷        hBitmapBuffer = CreateCompatibleBitmap(hdcBuffer, 16, 16);        //关联缓冲区句柄和画刷        hOldBitmapBuffer = (HBITMAP)SelectObject(hdcBuffer, hBitmapBuffer);         //创建字体        hfBuffer = ::CreateFont(                15,                 //字体高度                0,                  //字体宽度                0,                  //字体显示角度                0,                  //字体的角度                0,                  //字体的磅数                FALSE,              //斜体字体                FALSE,              //带下划线的字体                0,                  //带删除线的字体                GB2312_CHARSET,     //所需的字符集                OUT_DEFAULT_PRECIS, //输出的精度                CLIP_DEFAULT_PRECIS,//裁剪的精度                DEFAULT_QUALITY,    //逻辑字体与输入设备实际字体之间的精度                DEFAULT_PITCH,      //字体间距和字体集                L"宋体"             //字体名称                );         //关联字体画刷与临时缓冲区句柄        SelectObject(hdcBuffer, hfBuffer);         //初始化完成        hasInit = TRUE;} extern "C" _declspec(dllexport) void set_16bit_char(wchar_t *str) {        //没有初始化则进行初始化        if (hasInit == FALSE) init();        //设置缓冲区字体颜色为黑色        SetTextColor(hdcBuffer, 0x000000);        //输出该字到缓冲区        TextOut(hdcBuffer, 0, 0, str, 1);} extern "C" _declspec(dllexport) int get_16bit_pixel(int x, int y) {        return GetPixel(hdcBuffer, x, y);}
// test.cpp : 定义 DLL 应用程序的导出函数。 
#include <windows.h> 
#include <stdio.h> 
  
int hasInit = FALSE; 
HFONT hfBuffer;         
HDC hdcBuffer; 
HBITMAP hBitmapBuffer, hOldBitmapBuffer; 
  
//第五版:测试版字体放大程序 
  
void init() { 
        //获取缓冲区句柄 
        hdcBuffer = CreateCompatibleDC(NULL); 
        //获取缓冲区画刷 
        hBitmapBuffer = CreateCompatibleBitmap(hdcBuffer, 16, 16); 
        //关联缓冲区句柄和画刷 
        hOldBitmapBuffer = (HBITMAP)SelectObject(hdcBuffer, hBitmapBuffer); 
  
        //创建字体 
        hfBuffer = ::CreateFont( 
                15,                 //字体高度 
                0,                  //字体宽度 
                0,                  //字体显示角度 
                0,                  //字体的角度 
                0,                  //字体的磅数 
                FALSE,              //斜体字体 
                FALSE,              //带下划线的字体 
                0,                  //带删除线的字体 
                GB2312_CHARSET,     //所需的字符集 
                OUT_DEFAULT_PRECIS, //输出的精度 
                CLIP_DEFAULT_PRECIS,//裁剪的精度 
                DEFAULT_QUALITY,    //逻辑字体与输入设备实际字体之间的精度 
                DEFAULT_PITCH,      //字体间距和字体集 
                L"宋体"             //字体名称 
                ); 
  
        //关联字体画刷与临时缓冲区句柄 
        SelectObject(hdcBuffer, hfBuffer); 
  
        //初始化完成 
        hasInit = TRUE; 
} 
  
extern "C" _declspec(dllexport) void set_16bit_char(wchar_t *str) { 
        //没有初始化则进行初始化 
        if (hasInit == FALSE) init(); 
        //设置缓冲区字体颜色为黑色 
        SetTextColor(hdcBuffer, 0x000000); 
        //输出该字到缓冲区 
        TextOut(hdcBuffer, 0, 0, str, 1); 
} 
  
extern "C" _declspec(dllexport) int get_16bit_pixel(int x, int y) { 
        return GetPixel(hdcBuffer, x, y); 
} 
 | 
 |