cyl531207502 2015-04-12 14:41 采纳率: 71.4%
浏览 1730
已采纳

windows程序设计WM_ANISOTROPIC使用问题

#include

#define NUMLINES ((int) (sizeof devcaps / sizeof devcaps [0]))

struct
{
int iIndex ;
TCHAR * szLabel ;
TCHAR * szDesc ;
}
devcaps [] =
{
HORZSIZE, TEXT ("HORZSIZE"), TEXT ("Width in millimeters:"),
VERTSIZE, TEXT ("VERTSIZE"), TEXT ("Height in millimeters:"),
HORZRES, TEXT ("HORZRES"), TEXT ("Width in pixels:"),
VERTRES, TEXT ("VERTRES"), TEXT ("Height in raster lines:"),
BITSPIXEL, TEXT ("BITSPIXEL"), TEXT ("Color bits per pixel:"),
PLANES, TEXT ("PLANES"), TEXT ("Number of color planes:"),
NUMBRUSHES, TEXT ("NUMBRUSHES"), TEXT ("Number of device brushes:"),
NUMPENS, TEXT ("NUMPENS"), TEXT ("Number of device pens:"),
NUMMARKERS, TEXT ("NUMMARKERS"), TEXT ("Number of device markers:"),
NUMFONTS, TEXT ("NUMFONTS"), TEXT ("Number of device fonts:"),
NUMCOLORS, TEXT ("NUMCOLORS"), TEXT ("Number of device colors:"),
PDEVICESIZE, TEXT ("PDEVICESIZE"), TEXT ("Size of device structure:"),
ASPECTX, TEXT ("ASPECTX"), TEXT ("Relative width of pixel:"),
ASPECTY, TEXT ("ASPECTY"), TEXT ("Relative height of pixel:"),
ASPECTXY, TEXT ("ASPECTXY"), TEXT ("Relative diagonal of pixel:"),
LOGPIXELSX, TEXT ("LOGPIXELSX"), TEXT ("Horizontal dots per inch:"),
LOGPIXELSY, TEXT ("LOGPIXELSY"), TEXT ("Vertical dots per inch:"),
SIZEPALETTE, TEXT ("SIZEPALETTE"), TEXT ("Number of palette entries:"),
NUMRESERVED, TEXT ("NUMRESERVED"), TEXT ("Reserved palette entries:"),
COLORRES, TEXT ("COLORRES"), TEXT ("Actual color resolution:")
} ;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("DevCaps1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

 wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
 wndclass.lpfnWndProc   = WndProc ;
 wndclass.cbClsExtra    = 0 ;
 wndclass.cbWndExtra    = 0 ;
 wndclass.hInstance     = hInstance ;
 wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
 wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
 wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
 wndclass.lpszMenuName  = NULL ;
 wndclass.lpszClassName = szAppName ;

 if (!RegisterClass (&wndclass))
 {
      MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                  szAppName, MB_ICONERROR) ;
      return 0 ;
 }

 hwnd = CreateWindow (szAppName, TEXT ("Device Capabilities"),
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, CW_USEDEFAULT,
                      CW_USEDEFAULT, CW_USEDEFAULT,
                      NULL, NULL, hInstance, NULL) ;

 ShowWindow (hwnd, iCmdShow) ;
 UpdateWindow (hwnd) ;

 while (GetMessage (&msg, NULL, 0, 0))
 {
      TranslateMessage (&msg) ;
      DispatchMessage (&msg) ;
 }
 return msg.wParam ;

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar,x,y,cxClient,cyClient ;
TCHAR szBuffer[10] ;
HDC hdc ;
int i ;
PAINTSTRUCT ps ;
TEXTMETRIC tm ;

 switch (message)
 {
 case WM_CREATE:
      hdc = GetDC (hwnd) ;
      x=1000;
      y=1000;
      SetMapMode(hdc,MM_ANISOTROPIC);
      SetWindowExtEx(hdc,x,y,NULL);
      SetViewportExtEx(hdc,cxClient,-cyClient,NULL);
      SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
      GetTextMetrics (hdc, &tm) ;
      cxChar = tm.tmAveCharWidth ;
      cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
      cyChar = tm.tmHeight + tm.tmExternalLeading ;

      ReleaseDC (hwnd, hdc) ;
      return 0 ;
      case WM_KEYDOWN:
     switch(wParam)
     {
 case   VK_LEFT:
     x=x+50;
     y=y+50;
     InvalidateRect(hwnd,NULL,TRUE);
     UpdateWindow(hwnd);
     break;
     case   VK_RIGHT:
     x=x-50;
     y=y-50;
     InvalidateRect(hwnd,NULL,TRUE);
     UpdateWindow(hwnd);
     break;
     }
     return 0;
 case WM_PAINT:
      hdc = BeginPaint (hwnd, &ps) ;
       SetMapMode(hdc,MM_ANISOTROPIC);
      SetWindowExtEx(hdc,x,y,NULL);
      SetViewportExtEx(hdc,cxClient,-cyClient,NULL);
      SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);

      for (i = 0 ; i < NUMLINES ; i++)
      {
           TextOut (hdc, 0, cyChar * i,
                    devcaps[i].szLabel,
                    lstrlen (devcaps[i].szLabel)) ;

           TextOut (hdc, 14 * cxCaps, cyChar * i,
                    devcaps[i].szDesc,
                    lstrlen (devcaps[i].szDesc)) ;

           SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;

           TextOut (hdc, 14 * cxCaps + 35 * cxChar, cyChar * i, szBuffer,
                    wsprintf (szBuffer, TEXT ("%5d"),
                         GetDeviceCaps (hdc, devcaps[i].iIndex))) ;

           SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
      }

      EndPaint (hwnd, &ps) ;
      return 0 ;

 case WM_DESTROY:
      PostQuitMessage (0) ;
      return 0 ;
 }
 return DefWindowProc (hwnd, message, wParam, lParam) ;

}

以上代码是程序代码,此代码大部分来自windows程序设计第四章的DevCaps1程序,我只是把
case WM_CREATE部分增加了代码
x=1000;
y=1000;
SetMapMode(hdc,MM_ANISOTROPIC);
SetWindowExtEx(hdc,x,y,NULL);
SetViewportExtEx(hdc,cxClient,-cyClient,NULL);
SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
用于改变映射方式,另外增加了
case WM_KEYDOWN:
switch(wParam)
{
case VK_LEFT:
x=x+50;
y=y+50;
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
break;
case VK_RIGHT:
x=x-50;
y=y-50;
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
break;
}
return 0;
每次按下左箭头,映射模式下的逻辑坐标范围增加50,同时将整个窗口InvalidateRect,然后updatewindow
立刻更新窗口,右箭头是逻辑坐标范围缩小50,其余效果一样,最后case WM_PAINT在
BeginPaint函数后增加
SetMapMode(hdc,MM_ANISOTROPIC);
SetWindowExtEx(hdc,x,y,NULL);
SetViewportExtEx(hdc,cxClient,-cyClient,NULL);
SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
也就是将减小或者增大的的逻辑坐标应用到此程序中,这个程序的执行情况应该是每次减小逻辑坐标
输出的文字应该变大,增加逻辑坐标输出的问题变小(因为我最开始设置映射模式时默认值为1000,
假如此时 GetTextMetrics 获得的cxChar和cyChar都为10,那么后面逻辑坐标变大并且应用到此程序
中,那么相对的cxChar和cyChar比例就缩小了,因为后面没有再调用GetTextMetrics函数,所以他们
一直为10,同理,逻辑坐标变小后应该放大,但实际运行情况是字体没有变小变大,但是每一段文字之间
的空格变大了,请问这是为什么,请知道的告诉下,谢谢了,这个问题纠结很久了)

  • 写回答

1条回答 默认 最新

  • devmiao 2015-04-12 17:31
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环