如何把用CreateWindowEx创建窗口的程序段改写成RC文件?(50分)

  • 主题发起人 主题发起人 firstrose
  • 开始时间 开始时间
F

firstrose

Unregistered / Unconfirmed
GUEST, unregistred user!
我的意思是:我用CreateWindowEx创建了一个窗口,但现在我想把这个窗口放到RES文件里去。<br>但我把使用CreateWindowEx时的参数搬到RC文件里时,却发现建立的窗口和原先的窗口有很大<br>差别。谁可以给我一个解释,还有解决方法?<br><br>下面是我用的代码:<br><br>&nbsp; &nbsp;hWindow := CreateWindowEx(WS_EX_WINDOWEDGE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Test',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WS_VISIBLE or WS_MINIMIZEBOX or WS_SYSMENU,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 193,150,354,307,0, 0, Inst, nil);<br><br><br>RC:<br><br>KeyGenMain DIALOGEX 193, 150, 354, 307<br>style WS_VISIBLE | WS_MINIMIZEBOX | WS_EX_WINDOWEDGE | WS_SYSMENU<br>CAPTION "Test"<br>FONT 9, "Arial"<br>BEGIN<br>END
 
用VC做RES文件就可以了.
 
可以这样弄吗!!
 
问题是参数的对应关系!
 
KeyGenMain DIALOGEX 193, 150, 354, 307<br>style WS_VISIBLE | WS_MINIMIZEBOX | WS_EX_WINDOWEDGE | WS_SYSMENU<br>CAPTION "Test"<br>FONT 9, "Arial"<br>BEGIN<br>END<br>這樣定義還少一些默認的參數,用VC建RES文件比完整.
 
to amli:<br><br>看来你比较懂一点,但你试试看就知道我为什么问了。
 
因為兩種創建方式的參數不同,所以結果不同<br>一個原因有所使用的語言,另一個有字體,用WM_GETFONT獲取字體的句構后再用TFont.Handle<br>再查看字體的所有信息,是不是一樣,主要的是字集.
 
hmm...实际是这样的:两个对话框的大小不一样。查了资料说是dialog unit和device unit的差别。资料里也给了转换公式。可我怎么也无法验证出正确结果。下面是我的实验代码。amli你可以帮我看看吗?<br><br>Var<br>&nbsp; Rule:Longword;<br>&nbsp; baseunitX,baseunitY:Word;<br>&nbsp; s:String;<br>&nbsp; dialogunitX,dialogunitY:Word;<br>&nbsp; pixelX,pixelY:Word; <br>&nbsp; Code:Integer;<br><br>function DialogUnitsToPixelsX(DlgUnits: Word): Word;<br>begin<br>&nbsp; Result := (DlgUnits * LoWord(GetDialogBaseUnits)) div 4;<br>end;<br><br>function DialogUnitsToPixelsY(DlgUnits: Word): Word;<br>begin<br>&nbsp; Result := (DlgUnits * HiWord(GetDialogBaseUnits)) div 8;<br>end;<br><br>function PixelsToDialogUnitsX(PixUnits: Word): Word;<br>begin<br>&nbsp; Result := PixUnits * 4 div LoWord(GetDialogBaseUnits);<br>end;<br><br>function PixelsToDialogUnitsY(PixUnits: Word): Word;<br>begin<br>&nbsp; Result := PixUnits * 8 div HiWord(GetDialogBaseUnits);<br>end;<br><br>procedure TMainForm.SBd2pClick(Sender: TObject);<br>begin<br>&nbsp; s:=EDlgUnitX.Text;<br>&nbsp; Val(s,dialogunitX,Code);<br>&nbsp; pixelX:=DialogUnitsToPixelsX(dialogunitX);<br>&nbsp; Str(pixelX,s);<br>&nbsp; EPixelX.Text:=s;<br><br>&nbsp; s:=EDlgUnitY.Text;<br>&nbsp; Val(s,dialogunitY,Code);<br>&nbsp; pixelY:=DialogUnitsToPixelsY(dialogunitY);<br>&nbsp; Str(pixelY,s);<br>&nbsp; EPixelY.Text:=s;<br>end;<br><br>procedure TMainForm.SBp2dClick(Sender: TObject);<br>begin<br>&nbsp; s:=EPixelX.Text;<br>&nbsp; Val(s,pixelX,Code);<br>&nbsp; dialogunitX:=(pixelX*4) Div baseunitX;<br>&nbsp; Str(dialogunitX,s);<br>&nbsp; EDlgUnitX.Text:=s;<br><br>&nbsp; s:=EPixelY.Text;<br>&nbsp; Val(s,pixelY,Code);<br>&nbsp; dialogunitY:=(pixelY*8) Div baseunitY;<br>&nbsp; Str(dialogunitY,s);<br>&nbsp; EDlgUnitY.Text:=s;<br>end;<br><br>Initialization<br>&nbsp; Rule:=GetDialogBaseUnits;<br>&nbsp; baseunitX:=Rule AND $FFFF;<br>&nbsp; baseunitY:=Rule SHR 16;<br>end.<br>
 
//VC++裡是這樣轉的 我試過是可以的 把這段代碼轉成Delphi就可以.<br>void CDlg::ConvertDialogUnitsToPixels(LPCTSTR pszFontFace, WORD wFontSize,<br> int cxDlg, int cyDlg, SIZE* pSizePixel)<br>{<br> // Attempt to create the font to be used in the dialog box<br> UINT cxSysChar, cySysChar;<br> LOGFONT lf;<br> HDC hDC = ::GetDC(NULL);<br> memset(&amp;lf, 0, sizeof(LOGFONT));<br> lf.lfHeight = -MulDiv(wFontSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);<br> lf.lfWeight = FW_NORMAL;<br> lf.lfCharSet = DEFAULT_CHARSET;<br> lstrcpy(lf.lfFaceName, pszFontFace);<br><br> HFONT hNewFont = CreateFontIndirect(&amp;lf);<br> if (hNewFont != NULL)<br> {<br> HFONT hFontOld = (HFONT)SelectObject(hDC, hNewFont);<br> TEXTMETRIC tm;<br> GetTextMetrics(hDC, &amp;tm);<br> cySysChar = tm.tmHeight + tm.tmExternalLeading;<br> SIZE size;<br> ::GetTextExtentPoint32(hDC,<br> _T("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"), 52,<br> &amp;size);<br> cxSysChar = (size.cx + 26) / 52;<br> SelectObject(hDC, hFontOld);<br> DeleteObject(hNewFont);<br> }<br> else<br> {<br> // Could not create the font so just use the system's values<br> cxSysChar = LOWORD(GetDialogBaseUnits());<br> cySysChar = HIWORD(GetDialogBaseUnits());<br> }<br> ::ReleaseDC(NULL, hDC);<br><br> // Translate dialog units to pixels<br> pSizePixel-&gt;cx = MulDiv(cxDlg, cxSysChar, 4);<br> pSizePixel-&gt;cy = MulDiv(cyDlg, cySysChar, 8);<br>}<br><br>//Delphi<br>procedure ConvertDialogUnitsToPixels(FontFace : PChar; wFontSize : WORD;<br> cxDlg, cyDlg : Integer; var SizePixel : TSize);<br>var<br>&nbsp; cxSysChar, cySysChar : UINT;<br>&nbsp; lf : LOGFONT;<br>&nbsp; hFontOld, hNewFont, hDC : THandle;<br>&nbsp; tm : TTextMetric;<br>&nbsp; size : TSize;<br>begin<br> hDC := GetDC(0);<br> FillChar(lf, sizeof(LOGFONT), 0);<br> lf.lfHeight := -MulDiv(wFontSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);<br> lf.lfWeight := FW_NORMAL;<br> lf.lfCharSet:= DEFAULT_CHARSET;<br> lstrcpy(lf.lfFaceName, FontFace);<br><br> hNewFont := CreateFontIndirect(lf);<br> if hNewFont &lt;&gt; 0 then<br> begin<br> hFontOld := SelectObject(hDC, hNewFont);<br> GetTextMetrics(hDC, tm);<br> cySysChar := tm.tmHeight + tm.tmExternalLeading;<br> GetTextExtentPoint32(hDC,<br> 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 52,<br> size);<br> cxSysChar := (size.cx + 26) div 52;<br> SelectObject(hDC, hFontOld);<br> DeleteObject(hNewFont);<br> end<br> else<br> begin<br> cxSysChar := LOWORD(GetDialogBaseUnits());<br> cySysChar := HIWORD(GetDialogBaseUnits());<br> end;<br> ReleaseDC(0, hDC);<br><br> SizePixel.cx := MulDiv(cxDlg, cxSysChar, 4);<br> SizePixel.cy := MulDiv(cyDlg, cySysChar, 8);<br>end;
 
我按照你的方法试了,比我的代码好,但还是有差别。我用ResEd建立了RC文件,然后和CreateWindowEx的参数比较,结果如下:<br><br>RC:<br>MainForm DIALOGEX 6,6,230,168<br>FONT 8,"MS Sans Serif"<br>STYLE 0x10CF0000<br>EXSTYLE 0x00000000<br><br>&nbsp; with WinClass do<br>&nbsp; begin<br>&nbsp; &nbsp; style &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= CS_CLASSDC or CS_PARENTDC;<br>&nbsp; &nbsp; lpfnWndProc &nbsp; &nbsp; &nbsp; &nbsp;:= @WindowProc;<br>&nbsp; &nbsp; hInstance &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= Inst;<br>&nbsp; &nbsp; hbrBackground &nbsp; &nbsp; &nbsp;:= color_btnface +1;<br>&nbsp; &nbsp; lpszClassname &nbsp; &nbsp; &nbsp;:= 'OCG';<br>&nbsp; &nbsp; hIcon &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= 0;//<br>&nbsp; &nbsp; hCursor &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= LoadCursor(0, IDC_ARROW);<br>&nbsp; end; { with }<br><br>&nbsp; WinClass.hIcon:=LoadIcon(Inst, 'MAINICON');<br>&nbsp; RegisterClass(WinClass);<br><br>&nbsp; hFont1:=CreateFont(-11,0,0,0,0,0,0,0,DEFAULT_CHARSET,0,0,0,0,'MS Sans Serif');<br>&nbsp; SelectObject(PaintDC,hFont1);<br><br>&nbsp; // Create Main Window =========<br>&nbsp; &nbsp;hWindow := CreateWindowEx(WS_EX_WINDOWEDGE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'OCG',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'KeyGen for XXXXX v1.0',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WS_VISIBLE or WS_MINIMIZEBOX or WS_SYSMENU,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 193,150,354,307,0, 0, Inst, nil);<br><br><br>用你给的代码转换结果是:<br>pixelX:460<br>pixelY:336<br><br>这好象挺奇怪的。
 
我用GetClientRect試過<br>Width = Rect.Right - Rect.Left<br>Height = Rect.Bottom-Rect.Top<br><br>結果是完全一樣<br><br>用GetWindowRect就要差邊框.<br>
 
//我的測試源程序<br>http://home.pchome.com.tw/club/amlionline/win.exe
 
你的程序真的起作用。虽然我自己没有试出来。分先给了。如果你有CSDN的帐号,去<br>http://expert.csdn.net/Expert/TopicView1.asp?id=2732609<br>留个名,那里的分数也给你。如果还有问题,希望你指教。
 
后退
顶部