ホームに戻る
 既定以外のプリンターの使用
 
//gcc -mwindows a.cpp -lwinspool

#include <windows.h>
#include <stdio.h>
#include <winspool.h>

#define BMP_W 500 // a.bmp の横サイズ
#define BMP_H 500 // a.bmp の縦サイズ
#define BMP_FILE_NAME "a.bmp" // 印刷する bitmap
#define PRINTER_NAME "Canon iP2700 series XPS" // 印刷に用いるプリンター

char old_printer_name[64];

int bm[BMP_W*BMP_H];

void getbmp(){
  FILE *fp;
  fp = fopen(BMP_FILE_NAME,"rb");
  // bitmapのヘッダを空読み 処理としては良くない
  for(int i=0; i < 54; i++){
    char c = fgetc(fp);
  }
  for(int i=0; i < BMP_W*BMP_H; i++){
    char c0 = fgetc(fp);
    char c1 = fgetc(fp);
    char c2 = fgetc(fp);
    if(c0==0)bm[i]=1;
    else bm[i]=0;
  }
  fclose(fp);
}

HDC GetPrinterDC(void){
  int i;
  int size;

  size = GetProfileString("windows", "device", "", old_printer_name, 64);

  for(i = 0; i <= size; i++){
    if(old_printer_name[i] == ','){
      old_printer_name[i] = '\0';
      break;
    }
  }

  SetDefaultPrinter(PRINTER_NAME);

  return CreateDC(NULL, PRINTER_NAME, NULL, NULL);
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR lpCmdLine, int){
  int i;
  int wdpi, hdpi;
  DOCINFO diInfo = {0};
  HDC hdc;

  getbmp();

  diInfo.cbSize = sizeof (DOCINFO);
  diInfo.lpszDocName = "printer test";

  hdc = GetPrinterDC();

  if(hdc == NULL){
    MessageBox(NULL, TEXT("CreateDC Error"), lpCmdLine, MB_OK);
    return -1;
  }

  if(StartDoc(hdc , &diInfo) > 0 && StartPage(hdc) > 0){
    for(int y = 0; y < BMP_H; y++){
      for(int x = 0; x < BMP_W; x++){
        if(bm[(BMP_H-1-y)*BMP_W+x]==1){
          SetPixel(hdc,x,y,0);
        }  
      }
    }
    EndPage(hdc);
    EndDoc(hdc);
  }

  DeleteDC(hdc);

  SetDefaultPrinter(old_printer_name);

  return 0;
} 
inserted by FC2 system