Bei der eigenen Form Control-bereich einstellen.

  • C#
  • .NET (FX) 3.0–3.5

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Bei der eigenen Form Control-bereich einstellen.

    Hallo!

    Was mich mal interressiert ist wie ich bei einer Eigenen Form einstellen kann, in welchem bereich der Form ich Controls platieren kann.
    Bei der Normalen Form ist es ja so das man ja keine Controls auf den Rahmen plazieren kann, wie kann ich das umsetzen
    bzw. wie nennt man das?

    MfG Luca
    こんにちわ
    Achte beim stellen von Fragen auf eine genaue Fragestellung, mir passiert das selbst häufig, andere können dir dann nicht so gut helfen.
    Hallo @Eddy

    Danke für deinen Tipp, das einzige Problem ist

    dieser Code
    Spoiler anzeigen

    C#-Quellcode

    1. public partial class MyForm : Form
    2. {
    3. //Window Messages
    4. public const uint WM_NCPAINT = 0x85;
    5. public const uint WM_NCCALCSIZE = 0x83;
    6. public const uint WM_NCHITTEST = 0x84;
    7. //GetDCEx Flags
    8. public const int DCX_WINDOW = 0x00000001;
    9. public const int DCX_CACHE = 0x00000002;
    10. public const int DCX_PARENTCLIP = 0x00000020;
    11. public const int DCX_CLIPSIBLINGS = 0x00000010;
    12. public const int DCX_CLIPCHILDREN = 0x00000008;
    13. public const int DCX_NORESETATTRS = 0x00000004;
    14. public const int DCX_LOCKWINDOWUPDATE = 0x00000400;
    15. public const int DCX_EXCLUDERGN = 0x00000040;
    16. public const int DCX_INTERSECTRGN = 0x00000080;
    17. public const int DCX_INTERSECTUPDATE = 0x00000200;
    18. public const int DCX_VALIDATE = 0x00200000;
    19. //RECT Structure
    20. [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    21. public struct RECT
    22. {
    23. public int left, top, right, bottom;
    24. }
    25. //WINDOWPOS Structure
    26. [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    27. public struct WINDOWPOS
    28. {
    29. public IntPtr hwnd;
    30. public IntPtr hwndinsertafter;
    31. public int x, y, cx, cy;
    32. public int flags;
    33. }
    34. //NCCALCSIZE_PARAMS Structure
    35. [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    36. public struct NCCALCSIZE_PARAMS
    37. {
    38. public RECT rgrc0, rgrc1, rgrc2;
    39. public WINDOWPOS lppos;
    40. }
    41. //SetWindowTheme UXtheme Function
    42. [System.Runtime.InteropServices.DllImport("uxtheme.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
    43. public static extern int SetWindowTheme(
    44. IntPtr hWnd,
    45. String pszSubAppName,
    46. String pszSubIdList);
    47. //GetWindowRect User32 Function
    48. [System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling = true)]
    49. [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
    50. public static extern bool GetWindowRect(
    51. IntPtr hwnd,
    52. out RECT lpRect
    53. );
    54. //GetWindowDC User32 Function
    55. [System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling = true)]
    56. public static extern IntPtr GetWindowDC(
    57. IntPtr hWnd
    58. );
    59. //GetDCEx User32 Function
    60. [System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling = true)]
    61. public static extern IntPtr GetDCEx(
    62. IntPtr hWnd,
    63. IntPtr hrgnClip,
    64. int flags
    65. );
    66. //Window Procedure Hook
    67. protected override void WndProc(ref Message m)
    68. {
    69. //Don't style window in designer...
    70. if (DesignMode)
    71. base.WndProc(ref m);
    72. //Handle Message
    73. switch ((uint)m.Msg)
    74. {
    75. case WM_NCCALCSIZE: WmNCCalcSize(ref m); break;
    76. case WM_NCPAINT: WmNCPaint(ref m); break;
    77. default: base.WndProc(ref m); break;
    78. }
    79. }
    80. //Handle Creation
    81. protected override void OnHandleCreated(EventArgs e)
    82. {
    83. //Base Procedure...
    84. base.OnHandleCreated(e);
    85. //Remove Theme
    86. SetWindowTheme(this.Handle, string.Empty, string.Empty);
    87. }
    88. //WM_NCCALCSIZE
    89. private void WmNCCalcSize(ref Message m)
    90. {
    91. //Get Window Rect
    92. RECT formRect = new RECT();
    93. GetWindowRect(m.HWnd, out formRect);
    94. //Check WPARAM
    95. if (m.WParam != IntPtr.Zero) //TRUE
    96. {
    97. //When TRUE, LPARAM Points to a NCCALCSIZE_PARAMS structure
    98. var nccsp = (NCCALCSIZE_PARAMS)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS));
    99. //We're adjusting the size of the client area here. Right now, the client area is the whole form.
    100. //Adding to the Top, Bottom, Left, and Right will size the client area.
    101. nccsp.rgrc0.top += 30; //30-pixel top border
    102. nccsp.rgrc0.bottom -= 4; //4-pixel bottom (resize) border
    103. nccsp.rgrc0.left += 4; //4-pixel left (resize) border
    104. nccsp.rgrc0.right -= 4; //4-pixel right (resize) border
    105. //Set the structure back into memory
    106. System.Runtime.InteropServices.Marshal.StructureToPtr(nccsp, m.LParam, true);
    107. }
    108. else //FALSE
    109. {
    110. //When FALSE, LPARAM Points to a RECT structure
    111. var clnRect = (RECT)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(RECT));
    112. //Like before, we're adjusting the rectangle...
    113. //Adding to the Top, Bottom, Left, and Right will size the client area.
    114. clnRect.top += 30; //30-pixel top border
    115. clnRect.bottom -= 4; //4-pixel bottom (resize) border
    116. clnRect.left += 4; //4-pixel left (resize) border
    117. clnRect.right -= 4; //4-pixel right (resize) border
    118. //Set the structure back into memory
    119. System.Runtime.InteropServices.Marshal.StructureToPtr(clnRect, m.LParam, true);
    120. }
    121. //Return Zero
    122. m.Result = IntPtr.Zero;
    123. }
    124. //WM_NCPAINT
    125. private void WmNCPaint(ref Message m)
    126. {
    127. //Store HDC
    128. IntPtr HDC = IntPtr.Zero;
    129. Graphics gfx = null;
    130. //Check the WPARAM
    131. if(m.WParam == (IntPtr)1)
    132. {
    133. //For reasons unknown to me, the update region doesn't contain valid data and calling GetDCEx will do nothing.
    134. //So I call GetWindowDC and exclude the area using System.Drawing.Graphics instead.
    135. //Graphics Object from HDC
    136. HDC = GetWindowDC(m.HWnd);
    137. gfx = Graphics.FromHdc(HDC);
    138. //Exclude Client Area
    139. gfx.ExcludeClip(new Rectangle(4, 30, Width - 8, 34)); //Exclude Client Area (GetWindowDC grabs the WHOLE window's graphics handle)
    140. }
    141. else
    142. {
    143. //Graphics Object from HDC
    144. HDC = GetDCEx(m.HWnd, m.WParam, DCX_WINDOW | DCX_INTERSECTRGN);
    145. gfx = Graphics.FromHdc(HDC);
    146. }
    147. //Call Paint
    148. using (PaintEventArgs ncPaintArgs = new PaintEventArgs(gfx, new Rectangle(0, 0, Width, Height)))
    149. MyForm_NCPaint(this, ncPaintArgs);
    150. //Return Zero
    151. m.Result = IntPtr.Zero;
    152. }
    153. public MyForm()
    154. {
    155. InitializeComponent();
    156. }
    157. private void MyForm_NCPaint(object sender, PaintEventArgs e)
    158. {
    159. //Clear
    160. e.Graphics.Clear(Color.Green);
    161. }
    162. }


    funktioniert nichtmehr wenn die Form A) Den Fokus verloren hat B) Vergrößert/Verkleinert wurde

    Dennoch Danke,
    Wie kann ich denn das neuzeichnen des Rehmens erzwingen?

    MfG, Luca
    こんにちわ
    Achte beim stellen von Fragen auf eine genaue Fragestellung, mir passiert das selbst häufig, andere können dir dann nicht so gut helfen.

    LuaX schrieb:

    Wie kann ich denn das neuzeichnen des Rehmens erzwingen?
    Das Invalidaten der Teile der Form muss auf Deienn NC-Code umgeroutet werden.
    Ich hab mal dies probiert:

    C#-Quellcode

    1. private void button2_Click(object sender, EventArgs e)
    2. {
    3. Message m = new Message();
    4. m.WParam = (IntPtr)1;
    5. WmNCPaint(ref m);
    6. }
    Das isses noch nicht, zeigt aber einen Weg auf :!:
    Button1 wäre der Close-Button.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!