顯示具有 Window API 標籤的文章。 顯示所有文章
顯示具有 Window API 標籤的文章。 顯示所有文章

2024年12月25日 星期三

判斷環境處於開發狀態

使用 {$IFDEF DEBUG} 編譯條件

在開發過程中,您可以通過設置 DEBUG 編譯器條件來區分開發模式和執行模式。通常在 IDE 中執行程式時,會啟用 DEBUG 模式。
delphi
複製程式碼
procedure CheckMode;
begin
  {$IFDEF DEBUG}
  ShowMessage('目前是開發模式');
  {$ELSE}
  ShowMessage('目前是執行模式');
  {$ENDIF}
end;
注意:您需要在專案的編譯選項中啟用 DEBUG 定義。


判斷 IsDebuggerPresent(Windows 專用)

在 Windows 系統中,可以使用 Windows API 的 IsDebuggerPresent 函數來判斷程式是否被除錯工具附加,這可以間接反映是否處於開發模式。
delphi
複製程式碼
uses
  Winapi.Windows;
function IsDevelopmentMode: Boolean;
begin
  Result := IsDebuggerPresent;
end;
procedure CheckMode;
begin
  if IsDevelopmentMode then
    ShowMessage('目前是開發模式')
  else
    ShowMessage('目前是執行模式');
end;


判斷設計時期元件的 ComponentState

如果您正在開發元件,您可以檢查元件的 ComponentState 是否包含 csDesigning 標誌來判斷是否處於設計時期:
delphi
複製程式碼
function IsDesignTime(AComponent: TComponent): Boolean;
begin
  Result := csDesigning in AComponent.ComponentState;
end;
// 使用範例
if IsDesignTime(Self) then
  ShowMessage('目前是開發模式(設計時期)')
else
  ShowMessage('目前是執行模式');
適用場景
檢查程式是否在 IDE 中執行:使用 IsDebuggerPresent 或 {$IFDEF DEBUG}。
檢查元件是否處於設計時期:使用 csDesigning。
請依據您的需求選擇合適的方式。如果需要更多協助,請提供您的具體需求!

from Char GPT

2023年3月6日 星期一

ScorllBar 系統寬高

 
ScrollBar High
    GetSystemMetrics(SM_CYHSCROLL)


ScrollBar Width
    GetSystemMetrics(SM_CXVSCROLL)

2020年2月20日 星期四

How to download a file from the Internet


uses
  URLMon, ShellApi;

function fn_DownloadFile(ASourceFile, ADestFile: String): Boolean;
begin
  try
    Result := UrlDownloadToFile(nil, PChar(ASourceFile), PChar(ADestFile), 0, nil) = 0;
  except
    Result := False;
  end;
end;


URLDownloadToFile function

https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775123(v=vs.85)?redirectedfrom=MSDN

2019年4月10日 星期三

GetEnvironmentVariable 取系統環境變數

GetEnvironmentVariable


ALLUSERSPROFILE:All Users設定檔的資料夾位置。

APPDATA:目前使用者的Application Data資料夾位置。

CD:目前的工作資料夾。

CLIENTNAME:目前使用者的NETBIOS電腦名稱。(連線到Terminal的電腦名稱)

CMDCMDLINE:處理目前命令提示字元視窗命令的cmd.exe的完整路徑。

CMDEXTVERSION:目前Command Processor Extensions的版本。

COMPUTERNAME:電腦名稱。

COMSPEC::命令提示字元視窗的解譯程式路徑,通常與%CMDCMDLINE%相同。

CommonProgramFiles:Common Files資料夾的路徑。

DATE:目前的系統日期。

ERRORLEVEL:最近執行過的命令的錯誤碼;非零的值表示發生過的錯誤碼。

HOMESHARE:目前使用者共用資料夾的網路路徑。

HomeDrive:使用者目錄的磁碟機。

HomePath:使用者家目錄。

LOGONSEVER:目前使用者所登入的網路控制器名稱。

NUMBER_OF_PROCESSORS:電腦的處理器數量。

OS:作業系統名稱,其值固定為Windows_NT

PATHEXT:作業系統是為執行檔的副檔名。

PROCESSOR_ARCHITECTURE:處理器的架構名稱,例如x86。

PROCESSOR_IDENTFIER:說明處理器的文字(不一定會有此環境變數)。

PROCESSOR_LEVEL:處理器的model number。

PROCESSOR_REVISION:處理器的revision number。

PROMPT:目前解譯程式的命令提示字串。

Path:執行檔的搜尋路徑。

ProgramFiles:應用程式目錄,預設是C:\Program Files。

RANDOM:顯示0到32767之間的十進位整數亂數。

SESSIONNAME:連上終端伺服器的session names。

SystemDirectory:系統目錄,預設是C:\WINNT\System32或C:\WINDOWS\System32。

SystemDrive:系統磁碟機,預設是C:。

SystemRoot:系統根目錄,預設是C:\WINNT或C:\WINDOWS。

TIME:目前的系統時間。

Temp、Tmp:暫存檔目錄。

USERPROFILE:目前使用者的設定檔路徑。Ex: C:\Users\Administrator

UserDomain:包含使用者帳號的網域名稱,或者電腦名稱。

UserName:使用者帳號名稱。

WinDir:Windows目錄,預設是C:\WINNT或C:\WINDOWS。


轉貼至: https://jck11.pixnet.net/blog/post/13459124-windows%E7%B3%BB%E7%B5%B1%E5%85%A7%E5%BB%BA%E7%9A%84%E5%B8%B8%E8%A6%8B%E7%92%B0%E5%A2%83%E8%AE%8A%E6%95%B8


Ex:
  GetEnvironmentVariable('USERPROFILE'); //Ex: c:\users\administrator


2018年5月21日 星期一

WIN API LockWindowUpdate-封鎖重繪視窗內容

The LockWindowUpdate function disables or reenables drawing in the specified window. Only one window can be locked at a time.

BOOL LockWindowUpdate(

    HWND hWndLock     // handle of window to lock 
   );   


Parameters

hWndLock

Specifies the window in which drawing will be disabled. If this parameter is NULL, drawing in the locked window is enabled.



Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero, indicating that an error occurred or another window was already locked.


截錄自 Windows SDK

Ex:
   LockWindowUpdate(Handle);  //封鎖某個視窗重繪
   LockWindowUpdate(0);  //解除

2018年2月2日 星期五

Winapi.ShellAPI.ShellExecute

uses
  ShellAPI;

function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,  Directory: LPWSTR; ShowCmd: Integer): HINST; stdcall;


以下轉貼至:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

Performs an operation on a specified file.
Syntax
HINSTANCE ShellExecute(
  _In_opt_ HWND    hwnd,
  _In_opt_ LPCTSTR lpOperation,
  _In_     LPCTSTR lpFile,
  _In_opt_ LPCTSTR lpParameters,
  _In_opt_ LPCTSTR lpDirectory,
  _In_     INT     nShowCmd
);

Parameters
hwnd [in, optional]
Type: HWND
A handle to the parent window used for displaying a UI or error messages. This value can be NULL if the operation is not associated with a window.
lpOperation [in, optional]
Type: LPCTSTR
A pointer to a null-terminated string, referred to in this case as a verb, that specifies the action to be performed. The set of available verbs depends on the particular file or folder. Generally, the actions available from an object's shortcut menu are available verbs. The following verbs are commonly used:
edit
Launches an editor and opens the document for editing. If lpFile is not a document file, the function will fail.
explore
Explores a folder specified by lpFile.
find
Initiates a search beginning in the directory specified by lpDirectory.
open
Opens the item specified by the lpFile parameter. The item can be a file or folder.
print
Prints the file specified by lpFile. If lpFile is not a document file, the function fails.
NULL
The default verb is used, if available. If not, the "open" verb is used. If neither verb is available, the system uses the first verb listed in the registry.
lpFile [in]
Type: LPCTSTR
A pointer to a null-terminated string that specifies the file or object on which to execute the specified verb. To specify a Shell namespace object, pass the fully qualified parse name. Note that not all verbs are supported on all objects. For example, not all document types support the "print" verb. If a relative path is used for the lpDirectory parameter do not use a relative path for lpFile.
lpParameters [in, optional]
Type: LPCTSTR
If lpFile specifies an executable file, this parameter is a pointer to a null-terminated string that specifies the parameters to be passed to the application. The format of this string is determined by the verb that is to be invoked. If lpFile specifies a document file, lpParameters should be NULL.
lpDirectory [in, optional]
Type: LPCTSTR
A pointer to a null-terminated string that specifies the default (working) directory for the action. If this value is NULL, the current working directory is used. If a relative path is provided at lpFile, do not use a relative path for lpDirectory.
nShowCmd [in]
Type: INT
The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it. These values are defined in Winuser.h.
SW_HIDE (0)
Hides the window and activates another window.
SW_MAXIMIZE (3)
Maximizes the specified window.
SW_MINIMIZE (6)
Minimizes the specified window and activates the next top-level window in the z-order.
SW_RESTORE (9)
Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW (5)
Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT (10)
Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.
SW_SHOWMAXIMIZED (3)
Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED (2)
Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE (7)
Displays the window as a minimized window. The active window remains active.
SW_SHOWNA (8)
Displays the window in its current state. The active window remains active.
SW_SHOWNOACTIVATE (4)
Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL (1)
Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
Return value
Type: HINSTANCE
If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. It can be cast only to an int and compared to either 32 or the following error codes below.
Return code
Description
0
The operating system is out of memory or resources.
ERROR_FILE_NOT_FOUND
The specified file was not found.
ERROR_PATH_NOT_FOUND
The specified path was not found.
ERROR_BAD_FORMAT
The .exe file is invalid (non-Win32 .exe or error in .exe image).
SE_ERR_ACCESSDENIED
The operating system denied access to the specified file.
SE_ERR_ASSOCINCOMPLETE
The file name association is incomplete or invalid.
SE_ERR_DDEBUSY
The DDE transaction could not be completed because other DDE transactions were being processed.
SE_ERR_DDEFAIL
The DDE transaction failed.
SE_ERR_DDETIMEOUT
The DDE transaction could not be completed because the request timed out.
SE_ERR_DLLNOTFOUND
The specified DLL was not found.
SE_ERR_FNF
The specified file was not found.
SE_ERR_NOASSOC
There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable.
SE_ERR_OOM
There was not enough memory to complete the operation.
SE_ERR_PNF
The specified path was not found.
SE_ERR_SHARE
A sharing violation occurred.


2018年1月17日 星期三

取得系統語系 & 使用者自訂語系

type
  TUserDefaultLang = (langEnglish, langChinese_Simp, langChinese_Trad);

function fn_GetUserDefaultLangID:TUserDefaultLang;
begin
  //取得使用者自訂的系統語系 GetUserDefaultLangID
 //取得系統語系 GetSystemDefaultLangID
  case GetUserDefaultLangID of
    1028: Result := langChinese_Trad;    //1028 繁體中文
    1033: Result := langEnglish;                //1033 英文
    2052: Result := langChinese_Simp;  //2052 簡體中文
  end;
end;

2017年9月7日 星期四

解決Delphi程式在非中文系統下亂碼

解决Delphi程序在非中文系统下乱码
在工程文件头加入 Windows引用

在begin后加入

SetThreadLocale(
    DWORD(Word(SORT_DEFAULT) shl 16) or
    DWORD(Word(SUBLANG_CHINESE_SIMPLIFIED) shl 10) or
    DWORD(Word(LANG_CHINESE))
    );

原理:设置程序的所在区域

方法来自互联网,备忘~



轉貼至:http://www.cnblogs.com/mikemao/archive/2011/01/15/1936459.html

設定系統資料型態的預設格式

//設定日期型態的預設格式
  Application.UpdateFormatSettings := False;
  FormatSettings.ShortDateFormat := 'yyyy/mm/dd';
  FormatSettings.LongDateFormat := 'yyyy/mm/dd';
  FormatSettings.ShortTimeFormat := 'hh:nn:ss';
  FormatSettings.LongTimeFormat := 'hh:nn:ss';

可變更設定的項目:

FormatSettings.CurrencyString //貨幣符號
FormatSettings.Currencyformat //貨幣格式
FormatSettings.negcurrformat
FormatSettings.ThousandSeparator //千分位數分隔符號
FormatSettings.DecimalSeparator //小數點符號
FormatSettings.CurrencyDecimals  //貨幣小數位數
FormatSettings.DateSeparator //日期分隔符號
FormatSettings.ShortDateFormat //短日期格式
FormatSettings.LongDateFormat //長日期格式
FormatSettings.TimeSeparator //時間 分隔符號
FormatSettings.ShortTimeFormat //短時間格式
FormatSettings.LongTimeFormat //長時間格式
FormatSettings.TimeAMString //上午
FormatSettings.TimePMString //下午

WinAPI: GetTempPath - 获取临时文件夹路径

//声明:
GetTempPath(
  nBufferLength: DWORD; {缓冲区大小}
  lpBuffer: PChar       {缓冲区}
): DWORD;               {返回实际长度}


//举例:
var
  arr: array[0..MAX_PATH] of Char;
  num: DWORD;
begin
  num := GetTempPath(MAX_PATH, arr);
  ShowMessage(arr);           {C:\DOCUME~1\wy\LOCALS~1\Temp\}
  ShowMessage(IntToStr(num)); {29}
end;

轉貼至:http://www.cnblogs.com/del/archive/2008/02/07/1065772.html

檢查啟動狀態 (Debug Mode)

System.DebugHook


在Delphi程式設計中,System.DebugHook是一個全域變數,用於檢測應用程式是否在調試器下運行。其值可以是以下之一:

0:應用程式未在調試器下運行。
1:應用程式正在IDE調試器中運行。
2:應用程式在外部調試器中運行。

你可以使用System.DebugHook來在調試模式和發佈模式下執行不同的代碼。例如:

if System.DebugHook <> 0 then
  ShowMessage('Debugging Mode')
else
  ShowMessage('Release Mode');

這種用法幫助開發者在調試和部署過程中進行不同的操作。